Merge "[AAI] Improve test coverage for A&AI component aai-schema-service"
[aai/schema-service.git] / aai-schema-service / src / test / java / org / onap / aai / schemaservice / SchemaServiceTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.schemaservice;
22
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.is;
25
26 import java.io.UnsupportedEncodingException;
27 import java.util.Base64;
28 import java.util.Collections;
29
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import static org.junit.jupiter.api.Assertions.*;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.util.AAIConfig;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.boot.test.web.server.LocalServerPort;
39 import org.springframework.context.annotation.Import;
40 import org.springframework.http.HttpEntity;
41 import org.springframework.http.HttpHeaders;
42 import org.springframework.http.HttpMethod;
43 import org.springframework.http.MediaType;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.web.client.RestTemplate;
46
47 @SpringBootTest(
48     webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
49     classes = SchemaServiceApp.class)
50 @Import(SchemaServiceTestConfiguration.class)
51 public class SchemaServiceTest {
52
53     private HttpHeaders headers;
54
55     private HttpEntity<String> httpEntity;
56
57     private String baseUrl;
58
59     @Autowired
60     private RestTemplate restTemplate;
61
62     private String authorization;
63
64     @LocalServerPort
65     protected int randomPort;
66
67     @BeforeAll
68     public static void setupConfig() throws AAIException {
69         System.setProperty("AJSC_HOME", "./");
70         System.setProperty("BUNDLECONFIG_DIR", "src/main/resources/");
71         System.out.println("Current directory: " + System.getProperty("user.dir"));
72     }
73
74     @BeforeEach
75     public void setup() throws AAIException, UnsupportedEncodingException {
76
77         AAIConfig.init();
78         headers = new HttpHeaders();
79
80         authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8"));
81
82         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
83         headers.setContentType(MediaType.APPLICATION_JSON);
84         headers.add("Real-Time", "true");
85         headers.add("X-FromAppId", "JUNIT");
86         headers.add("X-TransactionId", "JUNIT");
87         headers.add("Authorization", "Basic " + authorization);
88         httpEntity = new HttpEntity<String>(headers);
89         baseUrl = "http://localhost:" + randomPort;
90     }
91
92     @Test
93     public void testGetSchemaAndEdgeRules() {
94
95         headers = new HttpHeaders();
96         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
97         headers.setContentType(MediaType.APPLICATION_XML);
98         headers.add("Real-Time", "true");
99         headers.add("X-FromAppId", "JUNIT");
100         headers.add("X-TransactionId", "JUNIT");
101         headers.add("Authorization", "Basic " + authorization);
102         httpEntity = new HttpEntity<String>(headers);
103
104         ResponseEntity<String> responseEntity;
105
106         responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/nodes?version=v20",
107             HttpMethod.GET, httpEntity, String.class);
108         assertThat(responseEntity.getStatusCodeValue(), is(200));
109
110         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
111         headers.setContentType(MediaType.APPLICATION_JSON);
112         httpEntity = new HttpEntity<String>(headers);
113
114         responseEntity =
115             restTemplate.exchange(baseUrl + "/aai/schema-service/v1/edgerules?version=v20",
116                 HttpMethod.GET, httpEntity, String.class);
117
118         assertThat(responseEntity.getStatusCodeValue(), is(200));
119     }
120
121     @Test
122     public void testInvalidSchemaAndEdges() {
123
124         headers = new HttpHeaders();
125         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
126         headers.setContentType(MediaType.APPLICATION_XML);
127         headers.add("Real-Time", "true");
128         headers.add("X-FromAppId", "JUNIT");
129         headers.add("X-TransactionId", "JUNIT");
130         headers.add("Authorization", "Basic " + authorization);
131         httpEntity = new HttpEntity<String>(headers);
132
133         ResponseEntity<String> responseEntity;
134
135         responseEntity =
136             restTemplate.exchange(baseUrl + "/aai/schema-service/v1/nodes?version=blah",
137                 HttpMethod.GET, httpEntity, String.class);
138         System.out.println("  " + responseEntity.getBody());
139         assertThat(responseEntity.getStatusCodeValue(), is(400));
140
141         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
142         headers.setContentType(MediaType.APPLICATION_JSON);
143         httpEntity = new HttpEntity<String>(headers);
144
145         responseEntity =
146             restTemplate.exchange(baseUrl + "/aai/schema-service/v1/edgerules?version=blah",
147                 HttpMethod.GET, httpEntity, String.class);
148
149         assertThat(responseEntity.getStatusCodeValue(), is(400));
150     }
151
152     @Test
153     public void testVersions() {
154
155         ResponseEntity<String> responseEntity;
156
157         responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/versions",
158             HttpMethod.GET, httpEntity, String.class);
159         assertThat(responseEntity.getStatusCodeValue(), is(200));
160
161     }
162
163     @Test
164     public void testGetStoredQueriesSuccess() {
165
166         ResponseEntity<String> responseEntity;
167
168         responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/stored-queries",
169             HttpMethod.GET, httpEntity, String.class);
170         assertThat(responseEntity.getStatusCodeValue(), is(200));
171
172     }
173
174     @Test
175     public void testSetDefaultProps() {
176         // Simulate directory containing app name
177         System.setProperty("user.dir", "/path/to/aai-schema-service");
178         System.clearProperty("BUNDLECONFIG_DIR");
179
180         // Call setDefaultProps
181         SchemaServiceApp.setDefaultProps();
182
183         // Verify the BUNDLECONFIG_DIR property is set correctly
184         assertEquals("src/main/resources", System.getProperty("BUNDLECONFIG_DIR"));
185
186         // Simulate directory not containing app name
187         System.setProperty("user.dir", "/path/to/other");
188         System.clearProperty("BUNDLECONFIG_DIR");
189         SchemaServiceApp.setDefaultProps();
190
191         // Verify the default value when not containing the app name
192         assertEquals("aai-schema-service/src/main/resources", System.getProperty("BUNDLECONFIG_DIR"));
193     }
194
195     @Test
196     public void testSetDefaultPropsWhenNotSet() {
197         // Simulate directory not containing app name
198         System.setProperty("user.dir", "/path/to/other");
199         System.clearProperty("BUNDLECONFIG_DIR");
200
201         // Call setDefaultProps
202         SchemaServiceApp.setDefaultProps();
203
204         // Verify the default value when the property was not previously set
205         assertEquals("aai-schema-service/src/main/resources", System.getProperty("BUNDLECONFIG_DIR"));
206     }
207
208     // Test for setDefaultProps with null file.separator
209     @Test
210     public void testSetDefaultPropsWithNullFileSeparator() {
211         // Clear the file.separator property
212         System.clearProperty("file.separator");
213
214         // Call setDefaultProps to set the default value
215         SchemaServiceApp.setDefaultProps();
216
217         // Verify that the file.separator system property is set to "/"
218         assertEquals("/", System.getProperty("file.separator"));
219     }
220
221     @Test
222     public void testAJSCHomePropertyWhenNotSet() {
223         // Clear the AJSC_HOME property to simulate it being unset
224         System.clearProperty("AJSC_HOME");
225
226         // Call setDefaultProps to ensure AJSC_HOME gets set
227         SchemaServiceApp.setDefaultProps();
228
229         // Verify that the AJSC_HOME property is set to "."
230         assertEquals(".", System.getProperty("AJSC_HOME"));
231     }
232
233 }