View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.common.impex.schema.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.junit.Test;
24  import org.kuali.common.impex.model.View;
25  import org.kuali.common.impex.schema.impl.mysql.MySqlViewSqlProducer;
26  
27  import static junit.framework.Assert.assertEquals;
28  import static junit.framework.Assert.assertTrue;
29  import static junit.framework.Assert.fail;
30  
31  public class TestMySqlViewSqlProducer {
32  
33      private static final String[] EXPECTED_SQL = {"DROP VIEW IF EXISTS TEST_V1\n",
34  
35              "CREATE VIEW TEST_V1 AS\n" +
36              "SELECT ID, NAME FROM FOO_T\n" +
37              "WHERE NAME LIKE 'TEST%'\n"};
38  
39      @Test
40      public void testEmptyViewList() {
41          List<View> empty = Collections.emptyList();
42  
43          MySqlViewSqlProducer sqlProducer = new MySqlViewSqlProducer();
44          List<String> results = sqlProducer.getViewsSql(empty);
45  
46          assertTrue(results.isEmpty());
47      }
48  
49      @Test
50      public void testGetViewSql() {
51          List<View> views = Collections.singletonList(MockDataUtil.buildSimpleView());
52  
53          MySqlViewSqlProducer sqlProducer = new MySqlViewSqlProducer();
54          List<String> results = sqlProducer.getViewsSql(views);
55  
56          assertEquals(EXPECTED_SQL.length, results.size());
57  
58          List<String> expected = Arrays.asList(EXPECTED_SQL);
59  
60          List<String> foundExpected = new ArrayList<String>(expected);
61          for (String e : expected) {
62              if(results.contains(e)) {
63                  foundExpected.remove(e);
64              }
65              else {
66                  fail("Expected sql statment **" + e + "** not found in generated statements.\n Generated statements were: " + results.toString());
67              }
68          }
69  
70          if(!foundExpected.isEmpty()) {
71              fail("Following expected sql statements not found in generated statements: \n" + foundExpected.toString());
72          }
73      }
74  
75  }