Add the queries endpoint and files
[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 package org.onap.aai.schemaservice;
21
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.schemaservice.config.PropertyPasswordConfiguration;
28 import org.onap.aai.util.AAIConfig;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.boot.context.embedded.LocalServerPort;
31 import org.springframework.boot.test.context.SpringBootTest;
32 import org.springframework.context.annotation.Import;
33 import org.springframework.http.*;
34 import org.springframework.test.context.ContextConfiguration;
35 import org.springframework.test.context.TestPropertySource;
36 import org.springframework.test.context.junit4.SpringRunner;
37 import org.springframework.web.client.RestTemplate;
38
39 import java.io.UnsupportedEncodingException;
40 import java.util.Base64;
41 import java.util.Collections;
42
43 import static org.hamcrest.Matchers.is;
44 import static org.junit.Assert.assertThat;
45
46 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SchemaServiceApp.class)
47 @TestPropertySource(locations = "classpath:application-test.properties")
48 @ContextConfiguration(initializers = PropertyPasswordConfiguration.class)
49 @Import(SchemaServiceTestConfiguration.class)
50 @RunWith(SpringRunner.class)
51 public class SchemaServiceTest {
52
53     private HttpHeaders headers;
54
55     private HttpEntity 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     @BeforeClass
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     @Before
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(headers);
89         baseUrl = "https://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(headers);
103
104         ResponseEntity responseEntity;
105
106         responseEntity = restTemplate.exchange(
107             baseUrl + "/aai/schema-service/v1/nodes?version=v15",
108             HttpMethod.GET,
109             httpEntity,
110             String.class
111         );
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 = restTemplate.exchange(
119             baseUrl + "/aai/schema-service/v1/edgerules?version=v15",
120             HttpMethod.GET,
121             httpEntity,
122             String.class
123         );
124
125         assertThat(responseEntity.getStatusCodeValue(), is(200));
126     }
127
128     @Test
129     public void testGetStoredQueriesSuccess(){
130
131         ResponseEntity responseEntity;
132
133         responseEntity = restTemplate.exchange(
134             baseUrl + "/aai/schema-service/v1/stored-queries",
135             HttpMethod.GET,
136             httpEntity,
137             String.class
138         );
139         assertThat(responseEntity.getStatusCodeValue(), is(200));
140
141     }
142 }