Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / sparky / util / OxmModelLoaderTest.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
7 * All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12
13 *      http://www.apache.org/licenses/LICENSE-2.0
14
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
21
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.openecomp.sparky.util;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotEquals;
30
31 import java.io.File;
32 import java.io.IOException;
33
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.TemporaryFolder;
38 import org.mockito.Mockito;
39 import org.openecomp.sparky.config.oxm.OxmModelLoader;
40
41 /**
42  * The Class OxmModelLoaderTest.
43  */
44 public class OxmModelLoaderTest {
45
46   @Rule
47   public TemporaryFolder folder = new TemporaryFolder();
48
49   OxmModelLoader loader;
50
51   /**
52    * Inits the.
53    *
54    * @throws IOException Signals that an I/O exception has occurred.
55    */
56   @Before
57   public void init() throws IOException {
58
59
60   }
61
62   /**
63    * Test find latest oxm version expectv 9.
64    *
65    * @throws IOException Signals that an I/O exception has occurred.
66    */
67   @Test
68   public void test_findLatestOxmVersion_expectv9() throws IOException {
69     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
70
71     folder.newFile("aai_oxm_v7.xml");
72     folder.newFile("aai_oxm_v8.xml");
73     folder.newFile("aai_oxm_v9.xml");
74     folder.newFile("randomTest.xml");
75
76     loader = Mockito.spy(new OxmModelLoader());
77     Mockito.when(loader.loadOxmFolder()).thenReturn(folder.getRoot());
78
79     String version = loader.findLatestOxmVersion();
80
81     assertEquals("v9", version);
82   }
83
84   /**
85    * Test find latest oxm version expect null when folder is empty.
86    *
87    * @throws IOException Signals that an I/O exception has occurred.
88    */
89   @Test
90   public void test_findLatestOxmVersion_expectNullWhenFolderIsEmpty() throws IOException {
91     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
92
93     loader = Mockito.spy(new OxmModelLoader());
94     Mockito.when(loader.loadOxmFolder()).thenReturn(folder.getRoot());
95
96     String version = loader.findLatestOxmVersion();
97
98     assertEquals(null, version);
99   }
100
101   /**
102    * Test find latest oxm version expect null when files does not match expected pattern.
103    *
104    * @throws IOException Signals that an I/O exception has occurred.
105    */
106   @Test
107   public void test_findLatestOxmVersion_expectNullWhenFilesDoesNotMatchExpectedPattern()
108       throws IOException {
109     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
110
111     folder.newFile("file1.xml");
112     folder.newFile("file2.xml");
113
114     loader = Mockito.spy(new OxmModelLoader());
115     Mockito.when(loader.loadOxmFolder()).thenReturn(folder.getRoot());
116
117     String version = loader.findLatestOxmVersion();
118
119     assertEquals(null, version);
120   }
121
122   /**
123    * Test load model expect success.
124    *
125    * @throws IOException Signals that an I/O exception has occurred.
126    */
127   @Test
128   public void test_loadModel_expectSuccess() throws IOException {
129     String version = "v9";
130     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
131
132     loader = Mockito.spy(new OxmModelLoader());
133     Mockito.when(loader.loadOxmFileName(version)).thenReturn(
134         System.getProperty("AJSC_HOME") + "/bundleconfig-local/oxm/aai_oxm_" + version + ".xml");
135
136     loader.loadModel(version);
137
138     assertNotEquals(null, loader.getOxmModel());
139   }
140
141   /**
142    * Test load model expect oxm data as empty.
143    *
144    * @throws IOException Signals that an I/O exception has occurred.
145    */
146   @Test
147   public void test_loadModel_expectOxmDataAsEmpty() throws IOException {
148     String version = "v8";
149     System.setProperty("AJSC_HOME", new File(".").getCanonicalPath().replace('\\', '/'));
150
151     loader = Mockito.spy(new OxmModelLoader());
152     Mockito.when(loader.loadOxmFileName(version)).thenReturn(
153         System.getProperty("AJSC_HOME") + "/bundleconfig-local/oxm/aai_oxm_" + version + ".xml");
154
155     loader.loadModel(version);
156
157     assertEquals(0, loader.getOxmModel().size());
158     assertEquals(true, loader.getSearchableEntityDescriptors().isEmpty());
159     assertEquals(0, loader.getSearchableOxmModel().size());
160
161
162
163     assertNotEquals(null, loader.getOxmModel());
164   }
165
166 }