Add instructions to invoke the linter and code formatter plugins to the README and...
[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.*;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.test.context.TestPropertySource;
44 import org.springframework.test.context.junit4.SpringRunner;
45 import org.springframework.web.client.RestTemplate;
46
47 @SpringBootTest(
48     webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
49     classes = SchemaServiceApp.class)
50 @TestPropertySource(locations = "classpath:application-test.properties")
51 @ContextConfiguration(initializers = PropertyPasswordConfiguration.class)
52 @Import(SchemaServiceTestConfiguration.class)
53
54 @RunWith(SpringRunner.class)
55 public class SchemaServiceTest {
56
57     private HttpHeaders headers;
58
59     private HttpEntity httpEntity;
60
61     private String baseUrl;
62
63     @Autowired
64     private RestTemplate restTemplate;
65
66     private String authorization;
67
68     @LocalServerPort
69     protected int randomPort;
70
71     @BeforeClass
72     public static void setupConfig() throws AAIException {
73         System.setProperty("AJSC_HOME", "./");
74         System.setProperty("BUNDLECONFIG_DIR", "src/main/resources/");
75         System.out.println("Current directory: " + System.getProperty("user.dir"));
76     }
77
78     @Before
79     public void setup() throws AAIException, UnsupportedEncodingException {
80
81         AAIConfig.init();
82         headers = new HttpHeaders();
83
84         authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8"));
85
86         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
87         headers.setContentType(MediaType.APPLICATION_JSON);
88         headers.add("Real-Time", "true");
89         headers.add("X-FromAppId", "JUNIT");
90         headers.add("X-TransactionId", "JUNIT");
91         headers.add("Authorization", "Basic " + authorization);
92         httpEntity = new HttpEntity(headers);
93         baseUrl = "http://localhost:" + randomPort;
94     }
95
96     @Test
97     public void testGetSchemaAndEdgeRules() {
98
99         headers = new HttpHeaders();
100         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
101         headers.setContentType(MediaType.APPLICATION_XML);
102         headers.add("Real-Time", "true");
103         headers.add("X-FromAppId", "JUNIT");
104         headers.add("X-TransactionId", "JUNIT");
105         headers.add("Authorization", "Basic " + authorization);
106         httpEntity = new HttpEntity(headers);
107
108         ResponseEntity responseEntity;
109
110         responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/nodes?version=v20",
111             HttpMethod.GET, httpEntity, String.class);
112         assertThat(responseEntity.getStatusCodeValue(), is(200));
113
114         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
115         headers.setContentType(MediaType.APPLICATION_JSON);
116         httpEntity = new HttpEntity(headers);
117
118         responseEntity =
119             restTemplate.exchange(baseUrl + "/aai/schema-service/v1/edgerules?version=v20",
120                 HttpMethod.GET, httpEntity, String.class);
121
122         assertThat(responseEntity.getStatusCodeValue(), is(200));
123     }
124
125     @Test
126     public void testInvalidSchemaAndEdges() {
127
128         headers = new HttpHeaders();
129         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
130         headers.setContentType(MediaType.APPLICATION_XML);
131         headers.add("Real-Time", "true");
132         headers.add("X-FromAppId", "JUNIT");
133         headers.add("X-TransactionId", "JUNIT");
134         headers.add("Authorization", "Basic " + authorization);
135         httpEntity = new HttpEntity(headers);
136
137         ResponseEntity responseEntity;
138
139         responseEntity =
140             restTemplate.exchange(baseUrl + "/aai/schema-service/v1/nodes?version=blah",
141                 HttpMethod.GET, httpEntity, String.class);
142         System.out.println("  " + responseEntity.getBody());
143         assertThat(responseEntity.getStatusCodeValue(), is(400));
144
145         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
146         headers.setContentType(MediaType.APPLICATION_JSON);
147         httpEntity = new HttpEntity(headers);
148
149         responseEntity =
150             restTemplate.exchange(baseUrl + "/aai/schema-service/v1/edgerules?version=blah",
151                 HttpMethod.GET, httpEntity, String.class);
152
153         assertThat(responseEntity.getStatusCodeValue(), is(400));
154     }
155
156     @Test
157     public void testVersions() {
158
159         ResponseEntity responseEntity;
160
161         responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/versions",
162             HttpMethod.GET, httpEntity, String.class);
163         assertThat(responseEntity.getStatusCodeValue(), is(200));
164
165     }
166
167     @Test
168     public void testGetStoredQueriesSuccess() {
169
170         ResponseEntity responseEntity;
171
172         responseEntity = restTemplate.exchange(baseUrl + "/aai/schema-service/v1/stored-queries",
173             HttpMethod.GET, httpEntity, String.class);
174         assertThat(responseEntity.getStatusCodeValue(), is(200));
175
176     }
177 }