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.Matchers.is;
24 import static org.junit.Assert.assertThat;
26 import java.io.UnsupportedEncodingException;
27 import java.util.Base64;
28 import java.util.Collections;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.schemaservice.config.PropertyPasswordConfiguration;
36 import org.onap.aai.util.AAIConfig;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.boot.web.server.LocalServerPort;
40 import org.springframework.context.annotation.Import;
41 import org.springframework.http.HttpEntity;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.HttpMethod;
44 import org.springframework.http.MediaType;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.test.context.ContextConfiguration;
47 import org.springframework.test.context.TestPropertySource;
48 import org.springframework.test.context.junit4.SpringRunner;
49 import org.springframework.web.client.RestTemplate;
52 webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
53 classes = SchemaServiceApp.class)
54 @TestPropertySource(locations = "classpath:application-test.properties")
55 @ContextConfiguration(initializers = PropertyPasswordConfiguration.class)
56 @Import(SchemaServiceTestConfiguration.class)
58 @RunWith(SpringRunner.class)
59 public class SchemaServiceTest {
61 private HttpHeaders headers;
63 private HttpEntity<String> httpEntity;
65 private String baseUrl;
68 private RestTemplate restTemplate;
70 private String authorization;
73 protected int randomPort;
76 public static void setupConfig() throws AAIException {
77 System.setProperty("AJSC_HOME", "./");
78 System.setProperty("BUNDLECONFIG_DIR", "src/main/resources/");
79 System.out.println("Current directory: " + System.getProperty("user.dir"));
83 public void setup() throws AAIException, UnsupportedEncodingException {
86 headers = new HttpHeaders();
88 authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8"));
90 headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
91 headers.setContentType(MediaType.APPLICATION_JSON);
92 headers.add("Real-Time", "true");
93 headers.add("X-FromAppId", "JUNIT");
94 headers.add("X-TransactionId", "JUNIT");
95 headers.add("Authorization", "Basic " + authorization);
96 httpEntity = new HttpEntity<String>(headers);
97 baseUrl = "http://localhost:" + randomPort;
101 public void testGetSchemaAndEdgeRules() {
103 headers = new HttpHeaders();
104 headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
105 headers.setContentType(MediaType.APPLICATION_XML);
106 headers.add("Real-Time", "true");
107 headers.add("X-FromAppId", "JUNIT");
108 headers.add("X-TransactionId", "JUNIT");
109 headers.add("Authorization", "Basic " + authorization);
110 httpEntity = new HttpEntity<String>(headers);
112 ResponseEntity<String> responseEntity;
114 responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/nodes?version=v20",
115 HttpMethod.GET, httpEntity, String.class);
116 assertThat(responseEntity.getStatusCodeValue(), is(200));
118 headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
119 headers.setContentType(MediaType.APPLICATION_JSON);
120 httpEntity = new HttpEntity<String>(headers);
123 restTemplate.exchange(baseUrl + "/aai/schema-service/v1/edgerules?version=v20",
124 HttpMethod.GET, httpEntity, String.class);
126 assertThat(responseEntity.getStatusCodeValue(), is(200));
130 public void testInvalidSchemaAndEdges() {
132 headers = new HttpHeaders();
133 headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
134 headers.setContentType(MediaType.APPLICATION_XML);
135 headers.add("Real-Time", "true");
136 headers.add("X-FromAppId", "JUNIT");
137 headers.add("X-TransactionId", "JUNIT");
138 headers.add("Authorization", "Basic " + authorization);
139 httpEntity = new HttpEntity<String>(headers);
141 ResponseEntity<String> responseEntity;
144 restTemplate.exchange(baseUrl + "/aai/schema-service/v1/nodes?version=blah",
145 HttpMethod.GET, httpEntity, String.class);
146 System.out.println(" " + responseEntity.getBody());
147 assertThat(responseEntity.getStatusCodeValue(), is(400));
149 headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
150 headers.setContentType(MediaType.APPLICATION_JSON);
151 httpEntity = new HttpEntity<String>(headers);
154 restTemplate.exchange(baseUrl + "/aai/schema-service/v1/edgerules?version=blah",
155 HttpMethod.GET, httpEntity, String.class);
157 assertThat(responseEntity.getStatusCodeValue(), is(400));
161 public void testVersions() {
163 ResponseEntity<String> responseEntity;
165 responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/versions",
166 HttpMethod.GET, httpEntity, String.class);
167 assertThat(responseEntity.getStatusCodeValue(), is(200));
172 public void testGetStoredQueriesSuccess() {
174 ResponseEntity<String> responseEntity;
176 responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/stored-queries",
177 HttpMethod.GET, httpEntity, String.class);
178 assertThat(responseEntity.getStatusCodeValue(), is(200));