Get rid of old Yang Parser in Controller
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / test / java / org / opendaylight / xsql / test / XSQLTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.xsql.test;
10
11 import java.io.InputStream;
12 import java.sql.SQLException;
13 import java.util.Collections;
14 import java.util.List;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.dom.xsql.XSQLAdapter;
20 import org.opendaylight.controller.md.sal.dom.xsql.XSQLBluePrint;
21 import org.opendaylight.controller.md.sal.dom.xsql.jdbc.JDBCResultSet;
22 import org.opendaylight.controller.md.sal.dom.xsql.jdbc.JDBCServer;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
27
28 public class XSQLTest {
29     private static final String DATASTORE_TEST_YANG = "/sal-persisted-dom-test.yang";
30     private XSQLBluePrint bluePrint = null;
31     //private static SchemaContext schemaContext = null;
32     @BeforeClass
33     public static void loadSchemaContext(){
34         //schemaContext = createTestContext();
35     }
36
37     @Before
38     public void before() {
39         try{
40             InputStream in = this.getClass().getClassLoader().getResourceAsStream("BluePrintCache.dat");
41             if(in!=null){
42                 bluePrint = XSQLBluePrint.load(in);
43                 log("Loaded Blue Print!");
44             }else{
45                 log("Can't find Blue Print!");
46             }
47             in.close();
48         }catch(Exception err){
49             err.printStackTrace();
50         }
51     }
52
53     @Test
54     public void testQueryParsingSimpleNoCriteria() {
55         String sql = "select * from nodes/node;";
56         JDBCResultSet rs = new JDBCResultSet(sql);
57         parseTables(sql,bluePrint, rs);
58         parseFields(sql, bluePrint, rs);
59         JDBCServer.parseCriteria(rs, bluePrint);
60         if(rs.getCriteria().isEmpty()){
61             log("Test Criteria parsing of \""+sql+"\" Passed!");
62             Assert.assertEquals(true, true);
63         }else{
64             log("Test Criteria parsing of \""+sql+"\" Failed!");
65             Assert.assertEquals(false, true);
66         }
67     }
68
69     @Test
70     public void testQueryParsingComplexNoCriteria() {
71         String sql = "select nodes/node.id,nodes/node/node-connector.id,nodes/node/node-connector.hardware-address from nodes/node,nodes/node/node-connector;";
72         JDBCResultSet rs = new JDBCResultSet(sql);
73         parseTables(sql,bluePrint, rs);
74         parseFields(sql, bluePrint, rs);
75         JDBCServer.parseCriteria(rs, bluePrint);
76         if(rs.getCriteria().isEmpty()){
77             log("Test Criteria parsing of \""+sql+"\" Passed!");
78             Assert.assertEquals(true, true);
79         }else{
80             log("Test Criteria parsing of \""+sql+"\" Failed!");
81             Assert.assertEquals(false, true);
82         }
83     }
84
85     @Test
86     public void testQueryParsingComplexWithCriteria() {
87         String sql = "select nodes/node.id,nodes/node/node-connector.id,nodes/node/node-connector.hardware-address from nodes/node,nodes/node/node-connector where hardware-address like 'AB';";
88         JDBCResultSet rs = new JDBCResultSet(sql);
89         parseTables(sql,bluePrint, rs);
90         parseFields(sql, bluePrint, rs);
91         JDBCServer.parseCriteria(rs, bluePrint);
92         if(!rs.getCriteria().isEmpty()){
93             log("Test Criteria parsing of \""+sql+"\" Passed!");
94             Assert.assertEquals(true, true);
95         }else{
96             log("Test Criteria parsing of \""+sql+"\" Failed!");
97             Assert.assertEquals(false, true);
98         }
99     }
100
101     @Test
102     public void testQueryParsingSimpleWithCriteria() {
103         String sql = "select * from nodes/node where nodes/node.id like 'something...';";
104         JDBCResultSet rs = new JDBCResultSet(sql);
105         parseTables(sql,bluePrint, rs);
106         parseFields(sql, bluePrint, rs);
107         JDBCServer.parseCriteria(rs, bluePrint);
108         if(!rs.getCriteria().isEmpty()){
109             log("Test Criteria parsing of \""+sql+"\" Passed!");
110             Assert.assertEquals(true, true);
111         }else{
112             log("Test Criteria parsing of \""+sql+"\" Failed!");
113             Assert.assertEquals(false, true);
114         }
115     }
116
117     private static void parseTables(String sql,XSQLBluePrint bp,JDBCResultSet rs){
118         try{
119             JDBCServer.parseTables(rs, bp);
120             log("Test Table parsing of \""+sql+"\" Passed!");
121             Assert.assertEquals(true,true);
122         }catch(SQLException err){
123             log("Test Table parsing of \""+sql+"\" Failed!");
124             err.printStackTrace();
125             Assert.assertEquals(false,true);
126         }
127     }
128
129     @Test
130     public void testQueryParsingComplexWithCriteriaAndGrouping() {
131
132         String sub_sql = "select nodes/node.id,nodes/node/node-connector.id,nodes/node/node-connector.hardware-address from nodes/node,nodes/node/node-connector where hardware-address like 'AB';";
133
134         String sql = "SELECT DISTINCT"
135                 + "\"LOGICAL_TABLE_1\".\"nodes/node.id\" AS \"COL0\"\n"
136                 + ",\"LOGICAL_TABLE_1\".\"nodes/node.address\" AS \"COL1\"\n"
137                 + ",\"LOGICAL_TABLE_1\".\"nodes/node/node-connector.hardware-address\" AS \"COL2\"\n"
138                 + "FROM\n"
139                 + "("+sub_sql+") \"LOGICAL_TABLE_1\"\n";
140
141
142
143         JDBCResultSet rs = new JDBCResultSet(sql);
144         XSQLAdapter.getInstance().loadBluePrint();
145         try{
146             JDBCServer.checkAndBreakSubQueries(rs, XSQLAdapter.getInstance());
147             if(rs.getSubQueries().isEmpty()){
148                 log("Logical table parsing for "+sql+" Failed!");
149             }else{
150                 JDBCServer.parseExternalQuery(rs);
151                 log("Fields="+rs.getFields().size());
152                 Assert.assertEquals(rs.getFields().size(), 3);
153                 Assert.assertEquals(rs.getTables().size(), 1);
154                 Assert.assertEquals(rs.getTables().get(0).getODLTableName(), "LOGICAL_TABLE_1");
155
156                 JDBCResultSet subRS = rs.getSubQueries().values().iterator().next();
157                 parseTables(sql,bluePrint, subRS);
158                 parseFields(sql, bluePrint, subRS);
159                 JDBCServer.parseCriteria(subRS, bluePrint);
160                 if(!subRS.getCriteria().isEmpty()){
161                     log("Test Criteria parsing of \""+sql+"\" Passed!");
162                     Assert.assertEquals(true, true);
163                 }else{
164                     log("Test Criteria parsing of \""+sql+"\" Failed!");
165                     Assert.assertEquals(false, true);
166                 }
167             }
168         }catch(SQLException err){
169             err.printStackTrace();
170         }
171     }
172
173     private static void parseFields(String sql,XSQLBluePrint bp,JDBCResultSet rs){
174         try{
175             JDBCServer.parseFields(rs, bp);
176             log("Test Fields parsing of \""+sql+"\" Passed!");
177             Assert.assertEquals(true,true);
178         }catch(SQLException err){
179             log("Test Fields parsing of \""+sql+"\" Failed!");
180             err.printStackTrace();
181             Assert.assertEquals(false,true);
182         }
183     }
184
185     private static void log(String str) {
186         System.out.print("*** XSQL Tests -");
187         System.out.println(str);
188     }
189
190     private static InputStream getInputStream() {
191         return XSQLTest.class.getResourceAsStream(DATASTORE_TEST_YANG);
192     }
193
194     public static SchemaContext createTestContext() {
195         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
196         final SchemaContext schemaContext;
197         final List<InputStream> streams = Collections.singletonList(getInputStream());
198
199         try {
200             schemaContext = reactor.buildEffective(streams);
201         } catch (ReactorException e) {
202             throw new RuntimeException("Unable to build schema context from " + streams, e);
203         }
204         return schemaContext;
205     }
206 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.