3 import com.jayway.jsonpath.JsonPath;
4 import org.junit.After;
5 import org.junit.Before;
7 import org.junit.runner.RunWith;
8 import org.onap.aai.config.PropertyPasswordConfiguration;
9 import org.onap.aai.exceptions.AAIException;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.context.embedded.LocalServerPort;
12 import org.springframework.boot.test.context.SpringBootTest;
13 import org.springframework.context.annotation.Import;
14 import org.springframework.http.*;
15 import org.springframework.test.context.ContextConfiguration;
16 import org.springframework.test.context.TestPropertySource;
17 import org.springframework.test.context.junit4.SpringRunner;
18 import org.springframework.web.client.RestTemplate;
20 import javax.ws.rs.core.Response;
21 import java.io.UnsupportedEncodingException;
22 import java.util.Collections;
23 import java.util.HashMap;
25 import java.util.UUID;
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.junit.Assert.assertNotNull;
32 * A sample junit test using spring boot that provides the ability to spin
33 * up the application from the junit layer and run rest requests against
34 * SpringBootTest annotation with web environment requires which spring boot
35 * class to load and the random port starts the application on a random port
36 * and injects back into the application for the field with annotation LocalServerPort
39 * This can be used to potentially replace a lot of the fitnesse tests since
40 * they will be testing against the same thing except fitnesse uses hbase
42 @RunWith(SpringRunner.class)
43 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TraversalApp.class)
44 @TestPropertySource(locations = "classpath:application-test.properties")
45 @ContextConfiguration(initializers = PropertyPasswordConfiguration.class)
46 @Import(TraversalTestConfiguration.class)
47 public class AAIGremlinQueryTest {
49 private HttpTestUtil httpTestUtil;
51 private String pserverUri;
53 private String hostname;
56 RestTemplate restTemplate;
61 private HttpEntity httpEntity;
63 private HttpHeaders headers;
65 private String baseUrl;
68 public void setup() throws Exception {
70 httpTestUtil = new HttpTestUtil();
72 hostname = UUID.randomUUID().toString();
74 pserverUri ="/aai/v11/cloud-infrastructure/pservers/pserver/" + hostname;
76 Map<String, String> pserverMap = new HashMap<>();
77 pserverMap.put("hostname", hostname);
78 String payload = PayloadUtil.getTemplatePayload("pserver.json", pserverMap);
79 httpTestUtil.doPut(pserverUri, payload);
81 headers = new HttpHeaders();
83 headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
84 headers.setContentType(MediaType.APPLICATION_JSON);
85 headers.add("Real-Time", "true");
86 headers.add("X-FromAppId", "JUNIT");
87 headers.add("X-TransactionId", "JUNIT");
89 baseUrl = "https://localhost:" + randomPort;
93 public void testPserverCount() throws Exception {
94 Map<String, String> gremlinQueryMap = new HashMap<>();
95 gremlinQueryMap.put("gremlin-query", "g.V().has('hostname', '" + hostname + "').count()");
97 String payload = PayloadUtil.getTemplatePayload("gremlin-query.json", gremlinQueryMap);
99 ResponseEntity responseEntity = null;
101 String endpoint = "/aai/v11/query?format=console";
103 httpEntity = new HttpEntity(payload, headers);
104 responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class);
105 assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
107 String result = JsonPath.read(responseEntity.getBody().toString(), "$.results[0].result");
108 assertThat(result, is("1"));
112 public void tearDown() throws UnsupportedEncodingException, AAIException {
114 Response response = httpTestUtil.doGet(pserverUri);
116 assertNotNull("Expected the response to be returned", response);
117 assertThat(response.getStatus(), is(200));
119 String body = response.getEntity().toString();
120 String resourceVersion = JsonPath.read(body, "$.resource-version");
122 response = httpTestUtil.doDelete(pserverUri, resourceVersion);
123 assertNotNull("Expected the response to be returned", response);
124 assertThat(response.getStatus(), is(204));