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=========================================================
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;
39 import javax.ws.rs.core.Response;
40 import java.io.UnsupportedEncodingException;
41 import java.util.Collections;
42 import java.util.HashMap;
44 import java.util.UUID;
46 import static org.hamcrest.CoreMatchers.is;
47 import static org.hamcrest.MatcherAssert.assertThat;
48 import static org.junit.Assert.assertNotNull;
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
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
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 {
68 private HttpTestUtil httpTestUtil;
70 private String pserverUri;
72 private String hostname;
75 RestTemplate restTemplate;
80 private HttpEntity httpEntity;
82 private HttpHeaders headers;
84 private String baseUrl;
87 public void setup() throws Exception {
89 httpTestUtil = new HttpTestUtil();
91 hostname = UUID.randomUUID().toString();
93 pserverUri ="/aai/v11/cloud-infrastructure/pservers/pserver/" + hostname;
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);
100 headers = new HttpHeaders();
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");
108 baseUrl = "https://localhost:" + randomPort;
112 public void testPserverCount() throws Exception {
113 Map<String, String> gremlinQueryMap = new HashMap<>();
114 gremlinQueryMap.put("gremlin-query", "g.V().has('hostname', '" + hostname + "').count()");
116 String payload = PayloadUtil.getTemplatePayload("gremlin-query.json", gremlinQueryMap);
118 ResponseEntity responseEntity = null;
120 String endpoint = "/aai/v11/query?format=console";
122 httpEntity = new HttpEntity(payload, headers);
123 responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class);
124 assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
126 String result = JsonPath.read(responseEntity.getBody().toString(), "$.results[0].result");
127 assertThat(result, is("1"));
131 public void tearDown() throws UnsupportedEncodingException, AAIException {
133 Response response = httpTestUtil.doGet(pserverUri);
135 assertNotNull("Expected the response to be returned", response);
136 assertThat(response.getStatus(), is(200));
138 String body = response.getEntity().toString();
139 String resourceVersion = JsonPath.read(body, "$.resource-version");
141 response = httpTestUtil.doDelete(pserverUri, resourceVersion);
142 assertNotNull("Expected the response to be returned", response);
143 assertThat(response.getStatus(), is(204));