SLI parser improvements
[ccsdk/sli/core.git] / sli / common / src / test / java / org / onap / ccsdk / sli / core / sli / ITCaseSvcLogicParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 /**
23  *
24  */
25 package org.onap.ccsdk.sli.core.sli;
26
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.fail;
29
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.net.URL;
35 import java.util.Properties;
36
37 import ch.vorburger.mariadb4j.DB;
38 import ch.vorburger.mariadb4j.DBConfigurationBuilder;
39
40 import org.junit.After;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * @author dt5972
50  *
51  */
52 public class ITCaseSvcLogicParser {
53
54         private static SvcLogicStore store;
55         private static final Logger LOG = LoggerFactory.getLogger(SvcLogicJdbcStore.class);
56
57         @BeforeClass
58         public static void setUpBeforeClass() throws Exception {
59
60                 LOG.info("before class");
61
62                 URL propUrl = ITCaseSvcLogicParser.class.getResource("/svclogic.properties");
63
64                 InputStream propStr = ITCaseSvcLogicParser.class.getResourceAsStream("/svclogic.properties");
65
66                 Properties props = new Properties();
67
68                 props.load(propStr);
69
70
71                 // Start MariaDB4j database
72                 DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
73                 config.setPort(0); // 0 => autom. detect free port
74                 DB db = DB.newEmbeddedDB(config.build());
75                 db.start();
76
77
78                 // Override jdbc URL and database name
79                 props.setProperty("org.onap.ccsdk.sli.jdbc.database", "test");
80                 props.setProperty("org.onap.ccsdk.sli.jdbc.url", config.getURL("test"));
81
82     }
83
84     @AfterClass
85     public static void tearDownAfterClass() throws Exception {
86         LOG.info("after class");
87     }
88
89     @Before
90     public void setUp() throws Exception {
91         LOG.info("before");
92     }
93
94     @After
95     public void tearDown() throws Exception {
96         LOG.info("after");
97     }
98
99     /**
100      * Test method for {@link org.onap.ccsdk.sli.core.sli.SvcLogicParser#parse(java.lang.String)}.
101      */
102     @Test
103     public void testParseValidXml() {
104
105         try {
106             InputStream testStr = getClass().getResourceAsStream("/parser-good.tests");
107             BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
108             String testCaseFile = null;
109             while ((testCaseFile = testsReader.readLine()) != null) {
110
111                 testCaseFile = testCaseFile.trim();
112
113                 if (testCaseFile.length() > 0) {
114                     if (!testCaseFile.startsWith("/")) {
115                         testCaseFile = "/" + testCaseFile;
116                     }
117                     URL testCaseUrl = getClass().getResource(testCaseFile);
118                     if (testCaseUrl == null) {
119                         fail("Could not resolve test case file " + testCaseFile);
120                     }
121
122                     try {
123                         SvcLogicParser.validate(testCaseUrl.getPath(), store);
124                     } catch (Exception e) {
125                         fail("Validation failure [" + e.getMessage() + "]");
126                     }
127                 }
128             }
129         } catch (SvcLogicParserException e) {
130             fail("Parser error : " + e.getMessage());
131         } catch (Exception e) {
132             LOG.error("", e);
133             fail("Caught exception processing test cases");
134         }
135     }
136
137     @Test(expected = SvcLogicException.class)
138     public void testParseInvalidXml() throws SvcLogicException, IOException {
139
140         InputStream testStr = getClass().getResourceAsStream("/parser-bad.tests");
141         BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
142         String testCaseFile;
143         while ((testCaseFile = testsReader.readLine()) != null) {
144
145             testCaseFile = testCaseFile.trim();
146
147             if (testCaseFile.length() > 0) {
148                 if (!testCaseFile.startsWith("/")) {
149                     testCaseFile = "/" + testCaseFile;
150                 }
151                 URL testCaseUrl = getClass().getResource(testCaseFile);
152                 if (testCaseUrl == null) {
153                     fail("Could not resolve test case file " + testCaseFile);
154                 }
155                 SvcLogicParser.load(testCaseUrl.getPath(), store);
156             }
157         }
158     }
159 }