Update traversal from AJSC 2 to Spring Boot
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / AAIGremlinQueryTest.java
1 package org.onap.aai;
2
3 import com.jayway.jsonpath.JsonPath;
4 import org.junit.After;
5 import org.junit.Before;
6 import org.junit.Test;
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;
19
20 import javax.ws.rs.core.Response;
21 import java.io.UnsupportedEncodingException;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import static org.hamcrest.CoreMatchers.is;
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.junit.Assert.assertNotNull;
30
31 /**
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
37  * <p>
38  *
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
41  */
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 {
48
49     private HttpTestUtil httpTestUtil;
50
51     private String pserverUri;
52
53     private String hostname;
54
55     @Autowired
56     RestTemplate restTemplate;
57
58     @LocalServerPort
59     int randomPort;
60
61     private HttpEntity httpEntity;
62
63     private HttpHeaders headers;
64
65     private String baseUrl;
66
67     @Before
68     public void setup() throws Exception {
69
70         httpTestUtil = new HttpTestUtil();
71
72         hostname = UUID.randomUUID().toString();
73
74         pserverUri ="/aai/v11/cloud-infrastructure/pservers/pserver/" + hostname;
75
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);
80
81         headers = new HttpHeaders();
82
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");
88
89         baseUrl = "https://localhost:" + randomPort;
90     }
91
92     @Test
93     public void testPserverCount() throws Exception {
94         Map<String, String> gremlinQueryMap = new HashMap<>();
95         gremlinQueryMap.put("gremlin-query", "g.V().has('hostname', '" + hostname + "').count()");
96
97         String payload = PayloadUtil.getTemplatePayload("gremlin-query.json", gremlinQueryMap);
98
99         ResponseEntity responseEntity = null;
100
101         String endpoint = "/aai/v11/query?format=console";
102
103         httpEntity = new HttpEntity(payload, headers);
104         responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class);
105         assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
106
107         String result = JsonPath.read(responseEntity.getBody().toString(), "$.results[0].result");
108         assertThat(result, is("1"));
109     }
110
111     @After
112     public void tearDown() throws UnsupportedEncodingException, AAIException {
113
114         Response response = httpTestUtil.doGet(pserverUri);
115
116         assertNotNull("Expected the response to be returned", response);
117         assertThat(response.getStatus(), is(200));
118
119         String body = response.getEntity().toString();
120         String resourceVersion = JsonPath.read(body, "$.resource-version");
121
122         response = httpTestUtil.doDelete(pserverUri, resourceVersion);
123         assertNotNull("Expected the response to be returned", response);
124         assertThat(response.getStatus(), is(204));
125     }
126 }