View Javadoc

1   /*
2    * Copyright 2010 Ning, Inc.
3    *
4    * Ning licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * License.  You may obtain a copy of the License at:
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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  //CREATE TABLE `thrift_types_tests` (
33  //   `event_type` varchar(255) DEFAULT NULL,
34  //   `field_id` int(11) DEFAULT NULL,
35  //   `field_type` varchar(255) DEFAULT NULL,
36  //   `field_name` varchar(255) DEFAULT NULL,
37  //   `sql_type` varchar(255) DEFAULT NULL,
38  //   `sql_length` int(11) DEFAULT NULL,
39  //   `sql_scale` int(11) DEFAULT NULL,
40  //   `sql_precision` int(11) DEFAULT NULL,
41  //   `description` varchar(255) DEFAULT NULL,
42  //   `id` int(11) NOT NULL AUTO_INCREMENT,
43  //   PRIMARY KEY (`id`),
44  //   UNIQUE KEY `unique_fields` (`event_type`,`field_id`)
45  //) ENGINE=InnoDB AUTO_INCREMENT=114 DEFAULT CHARSET=utf8
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         // Inserts
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         // Updates
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         // TODO Test for dups
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 }