summaryrefslogtreecommitdiffstats
path: root/vid-app-common
diff options
context:
space:
mode:
Diffstat (limited to 'vid-app-common')
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java125
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java56
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/aai/util/ParametrizedAAIRestInterfaceTest.java123
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java1
4 files changed, 23 insertions, 282 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java
index 45ced6bdf..8c05a8e6f 100644
--- a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java
+++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java
@@ -31,6 +31,7 @@ import java.net.URI;
import java.net.URLEncoder;
import java.util.Optional;
import java.util.UUID;
+import java.util.function.Supplier;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
@@ -162,15 +163,6 @@ public class AAIRestInterface {
}
- /**
- * Rest get.
- *
- * @param fromAppId the from app id
- * @param transId the trans id
- * @param requestUri the request uri
- * @param xml the xml
- * @return the string
- */
public ResponseWithRequestInfo RestGet(String fromAppId, String transId, URI requestUri, boolean xml) {
return RestGet(fromAppId, transId, requestUri, xml, false);
}
@@ -180,11 +172,16 @@ public class AAIRestInterface {
}
public ResponseWithRequestInfo doRest(String fromAppId, String transId, URI requestUri, String payload, HttpMethod method, boolean xml, boolean propagateExceptions) {
+ return doRest(fromAppId, transId, ()->systemPropertyHelper.getFullServicePath(requestUri), payload, method, xml, propagateExceptions);
+ }
+
+
+ public ResponseWithRequestInfo doRest(String fromAppId, String transId, Supplier<String> urlSupplier, String payload, HttpMethod method, boolean xml, boolean propagateExceptions) {
String url = null;
String methodName = "Rest"+method.name();
try {
- url = systemPropertyHelper.getFullServicePath(requestUri);
+ url = urlSupplier.get();
initRestClient(propagateExceptions);
@@ -223,7 +220,7 @@ public class AAIRestInterface {
} catch (Exception e) {
logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
if (propagateExceptions) {
- throw new ExceptionWithRequestInfo(method, defaultIfNull(url, requestUri.toASCIIString()), e);
+ throw new ExceptionWithRequestInfo(method, defaultIfNull(url, ""), e);
} else {
return new ResponseWithRequestInfo(null, url, method);
}
@@ -235,109 +232,21 @@ public class AAIRestInterface {
}
- /**
- * Delete.
- *
- * @param sourceID the source ID
- * @param transId the trans id
- * @param path the path
- * @return true, if successful
- */
- public boolean Delete(String sourceID, String transId, String path) {
- String methodName = "Delete";
- transId += ":" + UUID.randomUUID().toString();
- logger.debug(methodName + START_STRING);
- Boolean response = false;
- String url = systemPropertyHelper.getFullServicePath(path);
- try {
-
- initRestClient();
- loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
- final Response cres = client.target(url)
- .request()
- .accept(MediaType.APPLICATION_JSON)
- .header(TRANSACTION_ID_HEADER, transId)
- .header(PARTNER_NAME.getHeaderName(), PARTNER_NAME.getHeaderValue())
- .header(FROM_APP_ID_HEADER, sourceID)
- .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId())
- .delete();
- loggingService.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
- if (cres.getStatusInfo().equals(Response.Status.NOT_FOUND)) {
- logger.debug(EELFLoggerDelegate.debugLogger, "Resource does not exist...: " + cres.getStatus()
- + ":" + cres.readEntity(String.class));
- response = false;
- } else if (cres.getStatusInfo().equals(Response.Status.OK) || cres.getStatusInfo().equals(Response.Status.NO_CONTENT)) {
- logger.debug(EELFLoggerDelegate.debugLogger, "Resource " + url + " deleted");
- logger.info(EELFLoggerDelegate.errorLogger, "Resource " + url + " deleted");
- response = true;
- } else {
- logger.debug(EELFLoggerDelegate.debugLogger, "Deleting Resource failed: " + cres.getStatus()
- + ":" + cres.readEntity(String.class));
- response = false;
- }
-
- } catch (Exception e) {
- logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
- }
- return response;
- }
-
-
- /**
- * Rest put.
- *
- * @param fromAppId the from app id
- * @param path the path
- * @param payload the payload
- * @param xml the xml
- * @param propagateExceptions
- * @return the string
- */
public ResponseWithRequestInfo RestPut(String fromAppId, String path, String payload, boolean xml, boolean propagateExceptions) {
return doRest(fromAppId, UUID.randomUUID().toString(), Unchecked.toURI(path), payload, HttpMethod.PUT, xml, propagateExceptions);
}
-
- /**
- * Rest post.
- *
- * @param fromAppId the from app id
- * @param path the path
- * @param payload the payload
- * @param xml the xml
- * @return the string
- */
public Response RestPost(String fromAppId, String path, String payload, boolean xml) {
- String methodName = "RestPost";
- String url=systemPropertyHelper.getServiceBasePath(path);
- String transId = UUID.randomUUID().toString();
- logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING);
-
- Response response = null;
- try {
- initRestClient();
- loggingService.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload);
- response = authenticateRequest(client.target(url)
- .request()
- .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON)
- .header(TRANSACTION_ID_HEADER, transId)
- .header(PARTNER_NAME.getHeaderName(), PARTNER_NAME.getHeaderValue())
- .header(FROM_APP_ID_HEADER, fromAppId))
- .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId())
- .post(Entity.entity(payload, MediaType.APPLICATION_JSON));
- loggingService.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, response);
-
- if (response.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) {
- logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName));
- logger.debug(EELFLoggerDelegate.debugLogger, getValidResponseLogMessage(methodName));
- } else {
- logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, response));
- }
- } catch (Exception e) {
- logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
- }
- return response;
+ ResponseWithRequestInfo response = doRest(
+ fromAppId,
+ UUID.randomUUID().toString(),
+ ()->systemPropertyHelper.getServiceBasePath(path),
+ payload,
+ HttpMethod.POST,
+ xml,
+ false);
+ return response.getResponse();
}
protected String getFailedResponseLogMessage(String path, String methodName, Exception e) {
diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java
index bf8a5a1bc..2076d83ef 100644
--- a/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java
@@ -22,7 +22,6 @@ package org.onap.vid.aai.util;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
-import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static javax.ws.rs.core.Response.Status.OK;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
@@ -166,12 +165,12 @@ public class AAIRestInterfaceTest {
Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
// when
+ when(builder.build(any(), any())).thenReturn(invocation);
+ when(invocation.invoke()).thenReturn(response);
when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
when(response.getStatusInfo()).thenReturn(OK);
Response finalResponse = testSubject.RestPost("", PATH, payload, false);
- // then
- verify(builder).post(entity);
Assert.assertEquals(response, finalResponse);
}
@@ -182,13 +181,13 @@ public class AAIRestInterfaceTest {
Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
// when
- when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
+ when(builder.build(any(), any())).thenReturn(invocation);
+ when(invocation.invoke()).thenReturn(response);
when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
Response finalResponse = testSubject.RestPost("", PATH, payload, false);
// then
- verify(builder).post(entity);
Assert.assertEquals(response, finalResponse);
}
@@ -199,58 +198,15 @@ public class AAIRestInterfaceTest {
Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
// when
- when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
+ when(builder.build(any(), any())).thenReturn(invocation);
+ when(invocation.invoke()).thenThrow(new RuntimeException());
Response finalResponse = testSubject.RestPost("", PATH, payload, false);
// then
- verify(builder).post(entity);
Assert.assertNull(finalResponse);
}
@Test
- public void shouldExecuteRestDeleteMethodWithResponse400() {
- // given
- // when
- when(builder.delete()).thenReturn(response);
- when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
- String reason = "Any reason";
- when(response.readEntity(String.class)).thenReturn(reason);
- when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
- boolean finalResponse = testSubject.Delete("", "", PATH);
-
- // then
- verify(builder).delete();
- Assert.assertFalse(finalResponse);
- }
-
- @Test
- public void shouldExecuteRestDeleteMethodWithResponse404() {
- // given
- // when
- when(builder.delete()).thenReturn(response);
- when(response.getStatusInfo()).thenReturn(NOT_FOUND);
- String reason = "Any reason";
- when(response.readEntity(String.class)).thenReturn(reason);
- when(response.getStatus()).thenReturn(NOT_FOUND.getStatusCode());
- boolean finalResponse = testSubject.Delete("", "", PATH);
-
- // then
- verify(builder).delete();
- Assert.assertFalse(finalResponse);
- }
-
- @Test
- public void shouldFailWhenRestDeleteExecuted() {
- // given
- // when
- when(builder.delete()).thenThrow(new RuntimeException());
- boolean finalResponse = testSubject.Delete("", "", PATH);
- // then
- verify(builder).delete();
- Assert.assertFalse(finalResponse);
- }
-
- @Test
public void shouldExecuteRestGetMethodWithResponse200() {
// given
// when
diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/util/ParametrizedAAIRestInterfaceTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/util/ParametrizedAAIRestInterfaceTest.java
deleted file mode 100644
index c0d3b962f..000000000
--- a/vid-app-common/src/test/java/org/onap/vid/aai/util/ParametrizedAAIRestInterfaceTest.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * VID
- * ================================================================================
- * Copyright (C) 2018 - 2019 Nokia. All rights reserved.
- * ================================================================================
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.vid.aai.util;
-
-import static javax.ws.rs.core.Response.Status.NO_CONTENT;
-import static javax.ws.rs.core.Response.Status.OK;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.io.UnsupportedEncodingException;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Optional;
-import java.util.UUID;
-import javax.servlet.http.HttpServletRequest;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.Invocation;
-import javax.ws.rs.client.WebTarget;
-import javax.ws.rs.core.Response;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
-import org.onap.vid.aai.exceptions.InvalidPropertyException;
-import org.onap.vid.utils.Logging;
-import org.testng.Assert;
-
-@RunWith(Parameterized.class)
-public class ParametrizedAAIRestInterfaceTest {
-
- private static final String PATH = "path";
- private static final String HTTP_LOCALHOST = "http://localhost/";
- @Mock
- private Client client;
- @Mock
- private WebTarget webTarget;
- @Mock
- private Invocation.Builder builder;
- @Mock
- private ServletRequestHelper servletRequestHelper;
- @Mock
- private HttpsAuthClient httpsAuthClient;
- @Mock
- private HttpServletRequest httpServletRequest;
- @Mock
- private Response response;
- @Mock
- private SystemPropertyHelper systemPropertyHelper;
- @Mock
- private Logging loggingService;
-
- private AAIRestInterface testSubject;
- private Response.Status status;
-
- @Parameterized.Parameters
- public static Collection<Object> data() {
- return Arrays.asList(OK, NO_CONTENT);
- }
-
- @Before
- public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this);
- mockSystemProperties();
- testSubject = createTestSubject();
- when(client.target(HTTP_LOCALHOST+PATH)).thenReturn(webTarget);
- when(webTarget.request()).thenReturn(builder);
- when(builder.accept(Mockito.anyString())).thenReturn(builder);
- when(builder.header(Mockito.anyString(), Mockito.anyString())).thenReturn(builder);
- when(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
- }
-
- public ParametrizedAAIRestInterfaceTest(Response.Status status) {
- this.status = status;
- }
-
- private AAIRestInterface createTestSubject() {
- return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper, loggingService);
- }
-
- @Test
- public void testRestDeleteWithValidResponse() {
-
- // when
- when(builder.delete()).thenReturn(response);
- when(response.getStatusInfo()).thenReturn(status);
- boolean finalResponse = testSubject.Delete("", "", PATH);
-
- // then
- verify(builder).delete();
- Assert.assertTrue(finalResponse);
- }
-
- private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException {
- when(systemPropertyHelper.getAAIServerUrl()).thenReturn(Optional.of(HTTP_LOCALHOST));
- when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("cert"));
- when(systemPropertyHelper.getAAIVIDPasswd()).thenReturn(Optional.of("passwd"));
- when(systemPropertyHelper.getAAIVIDUsername()).thenReturn(Optional.of("user"));
- when(systemPropertyHelper.getEncodedCredentials()).thenReturn("someCredentials");
- when(systemPropertyHelper.getFullServicePath(Mockito.anyString())).thenReturn("http://localhost/path");
- }
-
-}
diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java
index 422443510..3fd92ee90 100644
--- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/OutgoingRequestHeadersTest.java
@@ -250,7 +250,6 @@ public class OutgoingRequestHeadersTest {
return Stream.<ThrowingConsumer<AAIRestInterface>>of(
client -> client.RestGet("from app id", "some transId", Unchecked.toURI("/any path"), false),
- client -> client.Delete("whatever source id", "some transId", "/any path"),
client -> client.RestPost("from app id", "/any path", "some payload", false),
client -> client.RestPut("from app id", "/any path", "some payload", false, false)