2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.schemaservice;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.is;
26 import java.io.UnsupportedEncodingException;
27 import java.util.Base64;
28 import java.util.Collections;
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;
48 webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
49 classes = SchemaServiceApp.class)
50 @Import(SchemaServiceTestConfiguration.class)
51 public class SchemaServiceTest {
53 private HttpHeaders headers;
55 private HttpEntity<String> httpEntity;
57 private String baseUrl;
60 private RestTemplate restTemplate;
62 private String authorization;
65 protected int randomPort;
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"));
75 public void setup() throws AAIException, UnsupportedEncodingException {
78 headers = new HttpHeaders();
80 authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8"));
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;
93 public void testGetSchemaAndEdgeRules() {
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);
104 ResponseEntity<String> responseEntity;
106 responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/nodes?version=v20",
107 HttpMethod.GET, httpEntity, String.class);
108 assertThat(responseEntity.getStatusCodeValue(), is(200));
110 headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
111 headers.setContentType(MediaType.APPLICATION_JSON);
112 httpEntity = new HttpEntity<String>(headers);
115 restTemplate.exchange(baseUrl + "/aai/schema-service/v1/edgerules?version=v20",
116 HttpMethod.GET, httpEntity, String.class);
118 assertThat(responseEntity.getStatusCodeValue(), is(200));
122 public void testInvalidSchemaAndEdges() {
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);
133 ResponseEntity<String> 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));
141 headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
142 headers.setContentType(MediaType.APPLICATION_JSON);
143 httpEntity = new HttpEntity<String>(headers);
146 restTemplate.exchange(baseUrl + "/aai/schema-service/v1/edgerules?version=blah",
147 HttpMethod.GET, httpEntity, String.class);
149 assertThat(responseEntity.getStatusCodeValue(), is(400));
153 public void testVersions() {
155 ResponseEntity<String> responseEntity;
157 responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/versions",
158 HttpMethod.GET, httpEntity, String.class);
159 assertThat(responseEntity.getStatusCodeValue(), is(200));
164 public void testGetStoredQueriesSuccess() {
166 ResponseEntity<String> responseEntity;
168 responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/stored-queries",
169 HttpMethod.GET, httpEntity, String.class);
170 assertThat(responseEntity.getStatusCodeValue(), is(200));
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");
180 // Call setDefaultProps
181 SchemaServiceApp.setDefaultProps();
183 // Verify the BUNDLECONFIG_DIR property is set correctly
184 assertEquals("src/main/resources", System.getProperty("BUNDLECONFIG_DIR"));
186 // Simulate directory not containing app name
187 System.setProperty("user.dir", "/path/to/other");
188 System.clearProperty("BUNDLECONFIG_DIR");
189 SchemaServiceApp.setDefaultProps();
191 // Verify the default value when not containing the app name
192 assertEquals("aai-schema-service/src/main/resources", System.getProperty("BUNDLECONFIG_DIR"));
196 public void testSetDefaultPropsWhenNotSet() {
197 // Simulate directory not containing app name
198 System.setProperty("user.dir", "/path/to/other");
199 System.clearProperty("BUNDLECONFIG_DIR");
201 // Call setDefaultProps
202 SchemaServiceApp.setDefaultProps();
204 // Verify the default value when the property was not previously set
205 assertEquals("aai-schema-service/src/main/resources", System.getProperty("BUNDLECONFIG_DIR"));
208 // Test for setDefaultProps with null file.separator
210 public void testSetDefaultPropsWithNullFileSeparator() {
211 // Clear the file.separator property
212 System.clearProperty("file.separator");
214 // Call setDefaultProps to set the default value
215 SchemaServiceApp.setDefaultProps();
217 // Verify that the file.separator system property is set to "/"
218 assertEquals("/", System.getProperty("file.separator"));
222 public void testAJSCHomePropertyWhenNotSet() {
223 // Clear the AJSC_HOME property to simulate it being unset
224 System.clearProperty("AJSC_HOME");
226 // Call setDefaultProps to ensure AJSC_HOME gets set
227 SchemaServiceApp.setDefaultProps();
229 // Verify that the AJSC_HOME property is set to "."
230 assertEquals(".", System.getProperty("AJSC_HOME"));