1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.ning.metrics.goodwill.store;
18
19 import com.ning.metrics.goodwill.access.GoodwillSchema;
20 import com.ning.metrics.goodwill.access.GoodwillSchemaField;
21 import com.ning.metrics.goodwill.dao.DAOBoneCPAccess;
22 import org.testng.Assert;
23 import org.testng.annotations.AfterTest;
24 import org.testng.annotations.BeforeTest;
25 import org.testng.annotations.Test;
26
27 import java.io.IOException;
28 import java.sql.SQLException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class MySQLStoreTest
48 {
49 private MySQLStore store;
50 private DAOBoneCPAccess access;
51
52 private static final String TYPE2_NAME = "Indiana Jones and the Last Crusade";
53 private GoodwillSchema type1;
54 private GoodwillSchema type2;
55 private static final String TYPE1_NAME = "The Shawshank Redemption";
56
57 @BeforeTest(alwaysRun = false, enabled = false)
58 public void setUp() throws SQLException, IOException, ClassNotFoundException
59 {
60 access = new DAOBoneCPAccess("localhost", 3306, "goodwill", "root", "");
61 store = new MySQLStore("thrift_types_tests", access);
62
63 type1 = new GoodwillSchema(TYPE1_NAME, new ArrayList<GoodwillSchemaField>());
64 type1.addThriftField(new GoodwillSchemaField("chair", "i32", (short) 0, null, null, null, null, null));
65 type1.addThriftField(new GoodwillSchemaField("deal", "string", (short) 1, null, null, null, null, null));
66 type1.addThriftField(new GoodwillSchemaField("continent", "string", (short) 2, null, null, null, null, null));
67 type1.addThriftField(new GoodwillSchemaField("egg", "string", (short) 3, null, null, null, null, null));
68 type1.addThriftField(new GoodwillSchemaField("car", "i32", (short) 4, null, null, null, null, null));
69 type1.addThriftField(new GoodwillSchemaField("bear", "string", (short) 5, null, null, null, null, null));
70
71 type2 = new GoodwillSchema(TYPE2_NAME, new ArrayList<GoodwillSchemaField>());
72 type2.addThriftField(new GoodwillSchemaField("arm", "bool", (short) 0, null, null, null, null, null));
73 type2.addThriftField(new GoodwillSchemaField("consonent", "i16", (short) 1, null, null, null, null, null));
74 type2.addThriftField(new GoodwillSchemaField("bank", "bool", (short) 2, null, null, null, null, null));
75 type2.addThriftField(new GoodwillSchemaField("cover", "string", (short) 3, null, null, null, null, null));
76 type2.addThriftField(new GoodwillSchemaField("century", "string", (short) 4, null, null, null, null, null));
77 type2.addThriftField(new GoodwillSchemaField("city", "string", (short) 5, null, null, null, null, null));
78 }
79
80 @AfterTest(alwaysRun = false, enabled = false)
81 public void tearDown() throws SQLException
82 {
83 }
84
85 @Test(enabled = false)
86 public void testInvalidThriftField()
87 {
88 try {
89 new GoodwillSchemaField("test", "string", (short) 1, "", null, 2, 23, 5);
90 Assert.fail();
91 }
92 catch (IllegalArgumentException e) {
93 Assert.assertTrue(true);
94
95 }
96 }
97
98
99 @Test(enabled = false)
100 public void testAddUpdateType() throws Exception
101 {
102 Collection<GoodwillSchema> types = store.getTypes();
103 Assert.assertEquals(types.size(), 0, "You need to cleanup your test db");
104
105
106
107 addThriftType1();
108
109 types = store.getTypes();
110 Assert.assertEquals(types.size(), 1);
111
112 addThriftType2();
113
114 types = store.getTypes();
115 Assert.assertEquals(types.size(), 2);
116
117 Assert.assertNull(store.findByName("Frenchies won 1998 world cup"));
118
119 runAssertsOnFields();
120
121
122
123 type1.addThriftField(new GoodwillSchemaField("foo", "string", (short) 6, null, null, null, null, null));
124
125 Assert.assertEquals(store.findByName(TYPE1_NAME).getSchema().size(), 6);
126 Assert.assertTrue(store.updateType(type1));
127 types = store.getTypes();
128 Assert.assertEquals(store.findByName(TYPE1_NAME).getSchema().size(), 7);
129 Assert.assertEquals(types.size(), 2);
130
131 runAssertsOnFields();
132
133
134 }
135
136 private void runAssertsOnFields()
137 {
138 GoodwillSchema shouldBeType1 = store.findByName(TYPE1_NAME);
139 for (GoodwillSchemaField field : shouldBeType1.getSchema()) {
140 Assert.assertEquals(field.getName(), type1.getFieldByPosition(field.getId()).getName());
141 Assert.assertEquals(field.getType(), type1.getFieldByPosition(field.getId()).getType());
142 Assert.assertEquals(field.getDescription(), type1.getFieldByPosition(field.getId()).getDescription());
143 Assert.assertEquals(field.getSql().getType(), type1.getFieldByPosition(field.getId()).getSql().getType());
144 Assert.assertEquals(field.getSql().getLength(), type1.getFieldByPosition(field.getId()).getSql().getLength());
145 Assert.assertEquals(field.getId(), type1.getFieldByPosition(field.getId()).getId());
146 }
147 }
148
149 private void addThriftType2()
150 {
151 store.addType(type2);
152 }
153
154 private void addThriftType1()
155 {
156 store.addType(type1);
157 }
158 }