* ================================================================================
* Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
+ * Modifications Copyright © 2024 Deutsche Telekom.
+ * ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
package org.onap.aai.rest;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThan;
+import static org.hamcrest.Matchers.samePropertyValuesAs;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
+import org.apache.commons.io.IOUtils;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
+import org.hamcrest.Matcher;
+import org.janusgraph.core.JanusGraph;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.ParseException;
-import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
-import org.onap.aai.AAISetup;
+import org.onap.aai.config.WebClientConfiguration;
+import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.dbmap.AAIGraph;
+import org.onap.aai.entities.PServer;
+import org.onap.aai.entities.PServerListResponse;
import org.onap.aai.exceptions.AAIException;
+import org.onap.aai.setup.SchemaVersions;
import org.onap.aai.util.AAIConfig;
import org.onap.aai.util.AAIConstants;
import org.skyscreamer.jsonassert.JSONAssert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.context.annotation.Import;
import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.test.web.reactive.server.WebTestClient;
-// TODO: Change the following test to use spring boot
-public class LegacyMoxyConsumerTest extends AAISetup {
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
+@Import(WebClientConfiguration.class)
+public class LegacyMoxyConsumerTest {
- protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
- private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
private static final Logger logger = LoggerFactory.getLogger(LegacyMoxyConsumerTest.class.getName());
+ private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
+ protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
static {
VALID_HTTP_STATUS_CODES.add(200);
VALID_HTTP_STATUS_CODES.add(204);
}
- private LegacyMoxyConsumer legacyMoxyConsumer;
+ @Autowired WebTestClient webClient;
+ @Autowired SchemaVersions schemaVersions;
- private HttpHeaders httpHeaders;
+ ObjectMapper mapper = new ObjectMapper();
+ private LegacyMoxyConsumer legacyMoxyConsumer;
+ private HttpHeaders httpHeaders;
private UriInfo uriInfo;
-
private MultivaluedMap<String, String> headersMultiMap;
private MultivaluedMap<String, String> queryParameters;
-
private List<String> aaiRequestContextList;
-
private List<MediaType> outputMediaTypes;
- private boolean initialized = false;
private String defaultSchemaVersion;
-
- @BeforeAll
- public static void setupRest() {
- // AAIGraph.getInstance();
- }
-
@BeforeEach
public void setup() {
- if (!initialized) {
- initialized = true;
+ if(!AAIGraph.isInit()) {
AAIGraph.getInstance();
}
logger.info("Starting the setup for the integration tests of Rest Endpoints");
Mockito.doReturn(null).when(queryParameters).remove(any());
when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
-
}
@Test
public void testResponsePutGetDeleteOnResource() throws JSONException, IOException, AAIException {
-
- String uri = getUri();
String payload = getResourcePayload(getObjectName());
-
- assertNotNull(payload, "Introspector returned invalid string when marshalling the object");
- assertNotNull(uri, "Introspector failed to return a valid uri");
-
- if (uri.length() != 0 && uri.charAt(0) == '/') {
- uri = uri.substring(1);
- }
-
- when(uriInfo.getPath()).thenReturn(uri);
- when(uriInfo.getPath(false)).thenReturn(uri);
-
- MockHttpServletRequest mockReqGet = new MockHttpServletRequest("GET", uri);
- Response response = legacyMoxyConsumer.getLegacy(defaultSchemaVersion, uri, -1, -1,
- false, "all", "false", httpHeaders, uriInfo, mockReqGet);
-
- assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
-
- MockHttpServletRequest mockReq = new MockHttpServletRequest("PUT", uri);
- response = legacyMoxyConsumer.update(payload, defaultSchemaVersion, uri, httpHeaders,
- uriInfo, mockReq);
-
- int code = response.getStatus();
- if (!VALID_HTTP_STATUS_CODES.contains(code)) {
- logger.info("Response Code: " + code + "\tEntity: " + response.getEntity());
- }
-
- assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
-
- queryParameters.add("depth", "10000");
-
- response = legacyMoxyConsumer.getLegacy(defaultSchemaVersion, uri, -1, -1, false,
- "10000", "false", httpHeaders, uriInfo, mockReqGet);
-
- code = response.getStatus();
- if (!VALID_HTTP_STATUS_CODES.contains(code)) {
- logger.info("Response Code: " + code + "\tEntity: " + response.getEntity());
- }
-
- String pserverEntity = response.getEntity().toString();
- JSONObject pserverJsonbject = new JSONObject(pserverEntity);
-
- assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
-
- JSONAssert.assertEquals(payload, pserverEntity, false);
-
- String resourceVersion = pserverJsonbject.getString("resource-version");
-
- queryParameters.add("resource-version", resourceVersion);
-
- mockReq = new MockHttpServletRequest("DELETE", uri);
- response = legacyMoxyConsumer.delete(defaultSchemaVersion, uri, httpHeaders, uriInfo,
- "", mockReq);
-
- code = response.getStatus();
- if (!VALID_HTTP_STATUS_CODES.contains(code)) {
- logger.info("Response Code: " + code + "\tEntity: " + response.getEntity());
- }
-
- assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
-
- response = legacyMoxyConsumer.getLegacy(defaultSchemaVersion, uri, -1, -1, false,
- "all", "false", httpHeaders, uriInfo, mockReqGet);
-
- assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
+ PServer expected = mapper.readValue(payload, PServer.class);
+
+ webClient.get()
+ .uri("/cloud-infrastructure/pservers/pserver/pserver-hostname-test?cleanup=false")
+ .exchange()
+ .expectStatus()
+ .isNotFound();
+
+ webClient.put()
+ .uri("/cloud-infrastructure/pservers/pserver/pserver-hostname-test")
+ .bodyValue(payload)
+ .exchange()
+ .expectStatus()
+ .isCreated();
+
+ PServer pserver = webClient.get()
+ .uri("/cloud-infrastructure/pservers/pserver/pserver-hostname-test?cleanup=false&depth=10000")
+ .exchange()
+ .expectStatus()
+ .isOk()
+ .returnResult(PServer.class)
+ .getResponseBody()
+ .blockFirst();
+
+ assertThat(pserver, samePropertyValuesAs(expected, "resourceVersion"));
+
+ String resourceVersion = pserver.getResourceVersion();
+
+ webClient.delete()
+ .uri(uriBuilder -> uriBuilder
+ .path("/cloud-infrastructure/pservers/pserver/pserver-hostname-test")
+ .queryParam("resource-version", resourceVersion)
+ .build())
+ .exchange()
+ .expectStatus()
+ .isNoContent();
+
+ webClient.get()
+ .uri("/cloud-infrastructure/pservers/pserver/pserver-hostname-test?cleanup=false&depth=10000")
+ .exchange()
+ .expectStatus()
+ .isNotFound();
}
@Test
public void testResponseGetOnResourcePaginated() throws JSONException, IOException, AAIException {
-
- String uri = getGetAllPserversURI();
-
- if (uri.length() != 0 && uri.charAt(0) == '/') {
- uri = uri.substring(1);
- }
-
- when(uriInfo.getPath()).thenReturn(uri);
- when(uriInfo.getPath(false)).thenReturn(uri);
-
- MockHttpServletRequest mockReqGet = new MockHttpServletRequest("GET", uri);
- Response response = legacyMoxyConsumer.getLegacy(defaultSchemaVersion, uri, 1, 10, true,
- "all", "false", httpHeaders, uriInfo, mockReqGet);
- assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+ JanusGraph graph = AAIGraph.getInstance().getGraph();
+ GraphTraversalSource g = graph.traversal();
+ g.addV()
+ .property("aai-node-type", "pserver")
+ .property("hostname", "hostname1")
+ .property("resource-version", UUID.randomUUID().toString())
+ .property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/hostname1")
+ .addV()
+ .property("aai-node-type", "pserver")
+ .property("hostname", "hostname2")
+ .property("resource-version", UUID.randomUUID().toString())
+ .property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/hostname2")
+ .next();
+ g.tx().commit();
+
+ PServerListResponse pservers = webClient.get()
+ .uri(uriBuilder ->
+ uriBuilder
+ .path("/cloud-infrastructure/pservers")
+ .queryParam("resultIndex", "1")
+ .queryParam("resultSize", "10")
+ .build())
+ .exchange()
+ .expectStatus()
+ .isOk()
+ // TODO: Assert values here once test data is isolated to individual test
+ .expectHeader().exists("total-results")
+ .expectHeader().exists("total-pages")
+ .returnResult(PServerListResponse.class)
+ .getResponseBody()
+ .blockFirst();
+
+ assertTrue(pservers.getPserver().size() > 0);
}
@Test
}
public String getResourcePayload(String resourceName) throws IOException {
- String rawPayload = getPayload("payloads/resource/" + resourceName + ".json");
+ String rawPayload = IOUtils.toString(this.getClass().getResourceAsStream("/payloads/resource/" + resourceName + ".json"), StandardCharsets.UTF_8);
return String.format(rawPayload, defaultSchemaVersion);
}
public String getRelationshipPayload(String relationshipName) throws IOException {
- String rawPayload = getPayload("payloads/relationship/" + relationshipName + ".json");
+ String rawPayload = IOUtils.toString(this.getClass().getResourceAsStream("/payloads/relationship/" + relationshipName + ".json"), StandardCharsets.UTF_8);
return String.format(rawPayload, defaultSchemaVersion);
}