Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / AAIGremlinQueryTest.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;
21
22 import com.jayway.jsonpath.JsonPath;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.onap.aai.config.PropertyPasswordConfiguration;
28 import org.onap.aai.exceptions.AAIException;
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 javax.ws.rs.core.Response;
40 import java.io.UnsupportedEncodingException;
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.Map;
44 import java.util.UUID;
45
46 import static org.hamcrest.CoreMatchers.is;
47 import static org.hamcrest.MatcherAssert.assertThat;
48 import static org.junit.Assert.assertNotNull;
49
50 /**
51  * A sample junit test using spring boot that provides the ability to spin
52  * up the application from the junit layer and run rest requests against
53  * SpringBootTest annotation with web environment requires which spring boot
54  * class to load and the random port starts the application on a random port
55  * and injects back into the application for the field with annotation LocalServerPort
56  * <p>
57  *
58  * This can be used to potentially replace a lot of the fitnesse tests since
59  * they will be testing against the same thing except fitnesse uses hbase
60  */
61 @RunWith(SpringRunner.class)
62 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TraversalApp.class)
63 @TestPropertySource(locations = "classpath:application-test.properties")
64 @ContextConfiguration(initializers = PropertyPasswordConfiguration.class)
65 @Import(TraversalTestConfiguration.class)
66 public class AAIGremlinQueryTest {
67
68     private HttpTestUtil httpTestUtil;
69
70     private String pserverUri;
71
72     private String hostname;
73
74     @Autowired
75     RestTemplate restTemplate;
76
77     @LocalServerPort
78     int randomPort;
79
80     private HttpEntity httpEntity;
81
82     private HttpHeaders headers;
83
84     private String baseUrl;
85
86     @Before
87     public void setup() throws Exception {
88
89         httpTestUtil = new HttpTestUtil();
90
91         hostname = UUID.randomUUID().toString();
92
93         pserverUri ="/aai/v11/cloud-infrastructure/pservers/pserver/" + hostname;
94
95         Map<String, String> pserverMap = new HashMap<>();
96         pserverMap.put("hostname", hostname);
97         String payload = PayloadUtil.getTemplatePayload("pserver.json", pserverMap);
98         httpTestUtil.doPut(pserverUri, payload);
99
100         headers = new HttpHeaders();
101
102         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
103         headers.setContentType(MediaType.APPLICATION_JSON);
104         headers.add("Real-Time", "true");
105         headers.add("X-FromAppId", "JUNIT");
106         headers.add("X-TransactionId", "JUNIT");
107
108         baseUrl = "https://localhost:" + randomPort;
109     }
110
111     @Test
112     public void testPserverCount() throws Exception {
113         Map<String, String> gremlinQueryMap = new HashMap<>();
114         gremlinQueryMap.put("gremlin-query", "g.V().has('hostname', '" + hostname + "').count()");
115
116         String payload = PayloadUtil.getTemplatePayload("gremlin-query.json", gremlinQueryMap);
117
118         ResponseEntity responseEntity = null;
119
120         String endpoint = "/aai/v11/query?format=console";
121
122         httpEntity = new HttpEntity(payload, headers);
123         responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class);
124         assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
125
126         String result = JsonPath.read(responseEntity.getBody().toString(), "$.results[0].result");
127         assertThat(result, is("1"));
128     }
129
130     @After
131     public void tearDown() throws UnsupportedEncodingException, AAIException {
132
133         Response response = httpTestUtil.doGet(pserverUri);
134
135         assertNotNull("Expected the response to be returned", response);
136         assertThat(response.getStatus(), is(200));
137
138         String body = response.getEntity().toString();
139         String resourceVersion = JsonPath.read(body, "$.resource-version");
140
141         response = httpTestUtil.doDelete(pserverUri, resourceVersion);
142         assertNotNull("Expected the response to be returned", response);
143         assertThat(response.getStatus(), is(204));
144     }
145 }