From ea59a688ce8d31426a830500a164b891bc180b54 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Fri, 10 Aug 2018 17:39:33 -0400 Subject: Handle special aai case better corrected error handling in aai client for service instances updated the exception logic for si's that are not found adhere to mapNotFoundToEmpty in special case Issue-ID: SO-851 Change-Id: I3843733553143dae046c1ae944a3bfd71ac5170c Signed-off-by: Benjamin, Max (mb388a) --- .../main/java/org/onap/so/client/RestRequest.java | 6 +- .../java/org/onap/so/client/aai/AAIClient.java | 9 +- .../org/onap/so/client/aai/AAIResourcesClient.java | 73 +++++++++++++--- .../aai/entities/uri/ServiceInstanceUri.java | 28 +++--- .../graphinventory/entities/uri/HttpAwareUri.java | 9 ++ ...IResourcesClientWithServiceInstanceUriTest.java | 99 ++++++++++++++++++++++ .../aai/entities/uri/ServiceInstanceUriTest.java | 44 ++++++---- 7 files changed, 219 insertions(+), 49 deletions(-) create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/uri/HttpAwareUri.java create mode 100644 common/src/test/java/org/onap/so/client/aai/AAIResourcesClientWithServiceInstanceUriTest.java (limited to 'common') diff --git a/common/src/main/java/org/onap/so/client/RestRequest.java b/common/src/main/java/org/onap/so/client/RestRequest.java index 25bf54b643..985d7cc885 100644 --- a/common/src/main/java/org/onap/so/client/RestRequest.java +++ b/common/src/main/java/org/onap/so/client/RestRequest.java @@ -72,17 +72,13 @@ public class RestRequest implements Callable { try { mapper.get().map(response); } catch (NotFoundException e) { - if (this.client.props.mapNotFoundToEmpty()) { + if (this.client.props.mapNotFoundToEmpty() && "GET".equals(method)) { msoLogger.error(e); return response; } else { throw e; } } - } else { - if (response.getStatus() == Status.NOT_FOUND.getStatusCode() && this.client.props.mapNotFoundToEmpty()) { - return response; - } } return response; diff --git a/common/src/main/java/org/onap/so/client/aai/AAIClient.java b/common/src/main/java/org/onap/so/client/aai/AAIClient.java index 39843b2263..3d2410e2a2 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAIClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAIClient.java @@ -22,11 +22,13 @@ package org.onap.so.client.aai; import java.net.URI; +import javax.ws.rs.NotFoundException; import javax.ws.rs.core.UriBuilder; import org.onap.so.client.RestClient; import org.onap.so.client.graphinventory.GraphInventoryClient; import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; +import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,7 +51,12 @@ public abstract class AAIClient extends GraphInventoryClient { } @Override protected RestClient createClient(GraphInventoryUri uri) { - return new AAIRestClient(getRestProperties(), constructPath(uri)); + try { + return new AAIRestClient(getRestProperties(), constructPath(uri)); + } catch (GraphInventoryUriComputationException | NotFoundException e) { + logger.debug("failed to construct A&AI uri", e); + throw e; + } } protected AAIVersion getVersion() { diff --git a/common/src/main/java/org/onap/so/client/aai/AAIResourcesClient.java b/common/src/main/java/org/onap/so/client/aai/AAIResourcesClient.java index 04757c6fc2..c8801eb00f 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAIResourcesClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAIResourcesClient.java @@ -81,9 +81,13 @@ public class AAIResourcesClient extends AAIClient { */ public boolean exists(AAIResourceUri uri) { AAIUri forceMinimal = this.addParams(Optional.of(Depth.ZERO), true, uri); - RestClient aaiRC = this.createClient(forceMinimal); - - return aaiRC.get().getStatus() == Status.OK.getStatusCode(); + try { + RestClient aaiRC = this.createClient(forceMinimal); + + return aaiRC.get().getStatus() == Status.OK.getStatusCode(); + } catch (NotFoundException e) { + return false; + } } /** @@ -148,7 +152,15 @@ public class AAIResourcesClient extends AAIClient { * @return */ public Optional get(Class clazz, AAIResourceUri uri) { - return this.createClient(uri).get(clazz); + try { + return this.createClient(uri).get(clazz); + } catch (NotFoundException e) { + if (this.getRestProperties().mapNotFoundToEmpty()) { + return Optional.empty(); + } else { + throw e; + } + } } /** @@ -157,7 +169,15 @@ public class AAIResourcesClient extends AAIClient { * @return */ public Response getFullResponse(AAIResourceUri uri) { - return this.createClient(uri).get(); + try { + return this.createClient(uri).get(); + } catch (NotFoundException e) { + if (this.getRestProperties().mapNotFoundToEmpty()) { + return e.getResponse(); + } else { + throw e; + } + } } /** @@ -167,7 +187,15 @@ public class AAIResourcesClient extends AAIClient { * @return */ public Optional get(GenericType resultClass, AAIResourceUri uri) { - return this.createClient(uri).get(resultClass); + try { + return this.createClient(uri).get(resultClass); + } catch (NotFoundException e) { + if (this.getRestProperties().mapNotFoundToEmpty()) { + return Optional.empty(); + } else { + throw e; + } + } } /** @@ -177,7 +205,16 @@ public class AAIResourcesClient extends AAIClient { * @return */ public AAIResultWrapper get(AAIResourceUri uri) { - String json = this.createClient(uri).get(String.class).orElse(null); + String json; + try { + json = this.createClient(uri).get(String.class).orElse(null); + } catch (NotFoundException e) { + if (this.getRestProperties().mapNotFoundToEmpty()) { + json = null; + } else { + throw e; + } + } return new AAIResultWrapper(json); } @@ -189,17 +226,27 @@ public class AAIResourcesClient extends AAIClient { * @return */ public AAIResultWrapper get(AAIResourceUri uri, Class c) { - + String json; + try { + json = this.createClient(uri).get(String.class) + .orElseThrow(() -> createException(c, uri.build() + " not found in A&AI")); + } catch (NotFoundException e) { + throw createException(c, "could not construct uri for use with A&AI"); + } + + return new AAIResultWrapper(json); + } + + private RuntimeException createException(Class c, String message) { RuntimeException e; try { - e = c.getConstructor(String.class).newInstance(uri.build() + " not found in A&AI"); + e = c.getConstructor(String.class).newInstance(message); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e1) { throw new IllegalArgumentException("could not create instance for " + c.getName()); } - String json = this.createClient(uri).get(String.class) - .orElseThrow(() -> e); - return new AAIResultWrapper(json); + + return e; } private Relationship buildRelationship(AAIResourceUri uri) { @@ -248,7 +295,7 @@ public class AAIResourcesClient extends AAIClient { return clone; } @Override - protected T getRestProperties() { + public T getRestProperties() { return super.getRestProperties(); } } diff --git a/common/src/main/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUri.java b/common/src/main/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUri.java index 093918d49b..a132e15d1f 100644 --- a/common/src/main/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUri.java +++ b/common/src/main/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUri.java @@ -22,19 +22,19 @@ package org.onap.so.client.aai.entities.uri; import java.io.IOException; import java.net.URI; -import java.util.Collections; +import java.util.Arrays; import java.util.Map; import java.util.Optional; import javax.ws.rs.BadRequestException; +import javax.ws.rs.NotFoundException; import javax.ws.rs.core.UriBuilder; import org.onap.so.client.aai.AAIObjectType; -import org.onap.so.client.aai.AAIQueryClient; -import org.onap.so.client.graphinventory.Format; -import org.onap.so.client.aai.entities.CustomQuery; +import org.onap.so.client.aai.AAIResourcesClient; import org.onap.so.client.aai.entities.Results; -import org.onap.so.client.graphinventory.entities.uri.SimpleUri; +import org.onap.so.client.graphinventory.Format; +import org.onap.so.client.graphinventory.entities.uri.HttpAwareUri; import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException; import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException; import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException; @@ -42,7 +42,7 @@ import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundExc import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -public class ServiceInstanceUri extends AAISimpleUri { +public class ServiceInstanceUri extends AAISimpleUri implements HttpAwareUri { private Optional cachedValue = Optional.empty(); @@ -55,11 +55,10 @@ public class ServiceInstanceUri extends AAISimpleUri { } protected String getSerivceInstance(Object id) throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException { if (!this.getCachedValue().isPresent()) { - AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(AAIObjectType.SERVICE_INSTANCE, id); - CustomQuery query = new CustomQuery(Collections.singletonList(serviceInstanceUri)); + AAIResourceUri serviceInstanceUri = AAIUriFactory.createNodesUri(AAIObjectType.SERVICE_INSTANCE, id).format(Format.PATHED); String resultJson; try { - resultJson = this.getQueryClient().query(Format.PATHED, query); + resultJson = this.getResourcesClient().get(serviceInstanceUri, NotFoundException.class).getJson(); } catch (BadRequestException e) { throw new GraphInventoryUriNotFoundException("Service instance " + id + " not found at: " + serviceInstanceUri.build()); @@ -99,7 +98,7 @@ public class ServiceInstanceUri extends AAISimpleUri { protected Optional getCachedValue() { return this.cachedValue; } - + @Override public URI build() { try { @@ -119,8 +118,11 @@ public class ServiceInstanceUri extends AAISimpleUri { return new ServiceInstanceUri(this.internalURI.clone(), this.getCachedValue(), values); } - protected AAIQueryClient getQueryClient() { - AAIResourceUri uri = AAIUriFactory.createNodesUri(AAIObjectType.ALLOTTED_RESOURCE, "").clone(); - return new AAIQueryClient(); + public AAIResourcesClient getResourcesClient() { + return new AAIResourcesClient(); + } + @Override + public URI buildNoNetwork() { + return super.build(new String[]{"NONE", "NONE", (String)this.values[0]}); } } diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/HttpAwareUri.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/HttpAwareUri.java new file mode 100644 index 0000000000..145959dc73 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/HttpAwareUri.java @@ -0,0 +1,9 @@ +package org.onap.so.client.graphinventory.entities.uri; + +import java.net.URI; + +public interface HttpAwareUri { + + + public URI buildNoNetwork(); +} diff --git a/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientWithServiceInstanceUriTest.java b/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientWithServiceInstanceUriTest.java new file mode 100644 index 0000000000..efd60a36a8 --- /dev/null +++ b/common/src/test/java/org/onap/so/client/aai/AAIResourcesClientWithServiceInstanceUriTest.java @@ -0,0 +1,99 @@ +package org.onap.so.client.aai; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +import java.util.List; +import java.util.Optional; + +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.so.client.aai.entities.AAIResultWrapper; +import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.so.client.aai.entities.uri.ServiceInstanceUri; +import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; + +public class AAIResourcesClientWithServiceInstanceUriTest { + + @Rule + public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443)); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private ServiceInstanceUri uri; + @Before + public void setUp() { + wireMockRule.stubFor(get(urlMatching("/aai/v[0-9]+/nodes.*")) + .willReturn(aResponse() + .withStatus(404) + .withHeader("Content-Type", "application/json") + .withHeader("Mock", "true"))); + + uri = spy((ServiceInstanceUri)AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, "id")); + doReturn(createClient()).when(uri).getResourcesClient(); + } + + @Test + public void getWithClass() { + AAIResourcesClient client = createClient(); + Optional result = client.get(String.class, uri); + + assertThat(result.isPresent(), equalTo(false)); + } + + @Test + public void getFullResponse() { + AAIResourcesClient client = createClient(); + Response result = client.getFullResponse(uri); + assertThat(result.getStatus(), equalTo(Status.NOT_FOUND.getStatusCode())); + } + + @Test + public void getWithGenericType() { + AAIResourcesClient client = createClient(); + Optional> result = client.get(new GenericType>() {}, uri); + assertThat(result.isPresent(), equalTo(false)); + } + + @Test + public void getAAIWrapper() { + AAIResourcesClient client = createClient(); + AAIResultWrapper result = client.get(uri); + assertThat(result.isEmpty(), equalTo(true)); + } + + @Test + public void getWithException() { + AAIResourcesClient client = createClient(); + this.thrown.expect(IllegalArgumentException.class); + AAIResultWrapper result = client.get(uri, IllegalArgumentException.class); + } + + @Test + public void existsTest() { + AAIResourcesClient client = createClient(); + doReturn(uri).when(uri).clone(); + boolean result = client.exists(uri); + assertThat(result, equalTo(false)); + } + private AAIResourcesClient createClient() { + AAIResourcesClient client = spy(new AAIResourcesClient()); + doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties(); + return client; + } +} diff --git a/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java b/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java index 73720f55c2..2cd7848cfa 100644 --- a/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java +++ b/common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java @@ -21,10 +21,9 @@ package org.onap.so.client.aai.entities.uri; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static com.github.tomakehurst.wiremock.client.WireMock.containing; -import static com.github.tomakehurst.wiremock.client.WireMock.put; +import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; -import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; @@ -43,14 +42,16 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.Optional; +import javax.ws.rs.NotFoundException; import javax.ws.rs.core.UriBuilder; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.onap.so.client.aai.AAIQueryClient; -import org.onap.so.client.graphinventory.Format; -import org.onap.so.client.aai.entities.CustomQuery; +import org.mockito.Matchers; +import org.onap.so.client.aai.AAIResourcesClient; +import org.onap.so.client.aai.entities.AAIResultWrapper; +import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl; import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException; import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException; import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException; @@ -155,9 +156,11 @@ public class ServiceInstanceUriTest { ServiceInstanceUri instance = new ServiceInstanceUri("key3"); ServiceInstanceUri spy = spy(instance); - AAIQueryClient mockQueryClient = mock(AAIQueryClient.class); - when(mockQueryClient.query(any(Format.class), any(CustomQuery.class))).thenReturn(content); - when(spy.getQueryClient()).thenReturn(mockQueryClient); + AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class); + AAIResultWrapper wrapper = mock(AAIResultWrapper.class); + when(mockResourcesClient.get(Matchers.any(AAIResourceUri.class), Matchers.>any())).thenReturn(wrapper); + when(wrapper.getJson()).thenReturn(content); + when(spy.getResourcesClient()).thenReturn(mockResourcesClient); exception.expect(GraphInventoryUriComputationException.class); spy.build(); @@ -176,17 +179,24 @@ public class ServiceInstanceUriTest { public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException { ServiceInstanceUri instance = new ServiceInstanceUri("key3"); ServiceInstanceUri spy = spy(instance); - AAIQueryClient client = mock(AAIQueryClient.class); - when(client.query(any(Format.class), any(CustomQuery.class))).thenReturn("{\"results\":[]}"); - doReturn(client).when(spy).getQueryClient(); - stubFor(put(urlMatching("/aai/v[0-9]+/query.*")) - .withRequestBody(containing("key3")) + AAIResourcesClient client = createClient(); + doReturn(client).when(spy).getResourcesClient(); + /*AAIResultWrapper wrapper = mock(AAIResultWrapper.class); + when(client.get(Matchers.any(AAIResourceUri.class), Matchers.>any())).thenReturn(wrapper); + when(wrapper.getJson()).thenReturn("{\"results\":[]}"); + doReturn(client).when(spy).getResourcesClient();*/ + stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3")) .willReturn(aResponse() - .withStatus(400) + .withStatus(404) .withHeader("Content-Type", "application/json") .withBodyFile(""))); - exception.expect(GraphInventoryUriComputationException.class); - exception.expectMessage(containsString("NotFoundException")); + exception.expect(NotFoundException.class); spy.build(); } + + private AAIResourcesClient createClient() { + AAIResourcesClient client = spy(new AAIResourcesClient()); + doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties(); + return client; + } } -- cgit 1.2.3-korg