[AAI] Release docker artifact 1.12.4
[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.Matchers.is;
24 import static org.junit.Assert.assertThat;
25
26 import java.io.UnsupportedEncodingException;
27 import java.util.Base64;
28 import java.util.Collections;
29
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;
50
51 @SpringBootTest(
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)
57
58 @RunWith(SpringRunner.class)
59 public class SchemaServiceTest {
60
61     private HttpHeaders headers;
62
63     private HttpEntity<String> httpEntity;
64
65     private String baseUrl;
66
67     @Autowired
68     private RestTemplate restTemplate;
69
70     private String authorization;
71
72     @LocalServerPort
73     protected int randomPort;
74
75     @BeforeClass
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"));
80     }
81
82     @Before
83     public void setup() throws AAIException, UnsupportedEncodingException {
84
85         AAIConfig.init();
86         headers = new HttpHeaders();
87
88         authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8"));
89
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;
98     }
99
100     @Test
101     public void testGetSchemaAndEdgeRules() {
102
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);
111
112         ResponseEntity<String> responseEntity;
113
114         responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/nodes?version=v20",
115             HttpMethod.GET, httpEntity, String.class);
116         assertThat(responseEntity.getStatusCodeValue(), is(200));
117
118         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
119         headers.setContentType(MediaType.APPLICATION_JSON);
120         httpEntity = new HttpEntity<String>(headers);
121
122         responseEntity =
123             restTemplate.exchange(baseUrl + "/aai/schema-service/v1/edgerules?version=v20",
124                 HttpMethod.GET, httpEntity, String.class);
125
126         assertThat(responseEntity.getStatusCodeValue(), is(200));
127     }
128
129     @Test
130     public void testInvalidSchemaAndEdges() {
131
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);
140
141         ResponseEntity<String> responseEntity;
142
143         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));
148
149         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
150         headers.setContentType(MediaType.APPLICATION_JSON);
151         httpEntity = new HttpEntity<String>(headers);
152
153         responseEntity =
154             restTemplate.exchange(baseUrl + "/aai/schema-service/v1/edgerules?version=blah",
155                 HttpMethod.GET, httpEntity, String.class);
156
157         assertThat(responseEntity.getStatusCodeValue(), is(400));
158     }
159
160     @Test
161     public void testVersions() {
162
163         ResponseEntity<String> responseEntity;
164
165         responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/versions",
166             HttpMethod.GET, httpEntity, String.class);
167         assertThat(responseEntity.getStatusCodeValue(), is(200));
168
169     }
170
171     @Test
172     public void testGetStoredQueriesSuccess() {
173
174         ResponseEntity<String> responseEntity;
175
176         responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/stored-queries",
177             HttpMethod.GET, httpEntity, String.class);
178         assertThat(responseEntity.getStatusCodeValue(), is(200));
179
180     }
181 }