From 65d71e97406a9a9b1c56475855f1d2e59adeec81 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Wed, 22 May 2019 11:08:32 -0400 Subject: [PATCH] HttpLookupUri is now serializable again cachedValue is now transient added custom serialization created serialization unit test for ServiceInstancesUri Change-Id: Idbc414a8483a4eae9c93ae8b06a1e77cbe683ca8 Issue-ID: SO-1906 Signed-off-by: Benjamin, Max (mb388a) --- .../so/client/aai/entities/uri/HttpLookupUri.java | 23 +++++++++-- .../aai/entities/uri/ServiceInstanceUriTest.java | 46 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java b/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java index f086a6abcf..37d21b375e 100644 --- a/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java +++ b/common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java @@ -21,6 +21,8 @@ package org.onap.so.client.aai.entities.uri; import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.net.URI; import java.util.Map; import java.util.Optional; @@ -42,7 +44,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri { - private Optional cachedValue = Optional.empty(); + private transient Optional cachedValue = Optional.empty(); private final AAIObjectType aaiType; protected HttpLookupUri(AAIObjectType type, Object... values) { @@ -78,8 +80,7 @@ public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri throw new GraphInventoryPayloadException("could not map payload: " + resultJson, e); } } - Optional cachedValueOpt = this.getCachedValue(); - return cachedValueOpt.isPresent() ? cachedValueOpt.get() : ""; + return cachedValue.get(); } protected Optional extractRelatedLink(String jsonString) throws IOException { @@ -139,6 +140,22 @@ public abstract class HttpLookupUri extends AAISimpleUri implements HttpAwareUri return new AAIResourcesClient(); } + private void writeObject(ObjectOutputStream oos) throws IOException { + + oos.writeUTF(this.cachedValue.orElse("")); + } + + private void readObject(ObjectInputStream ois) throws IOException { + + String value = ois.readUTF(); + if ("".equals(value)) { + this.cachedValue = Optional.empty(); + } else { + this.cachedValue = Optional.ofNullable(value); + } + + } + @Override public abstract URI buildNoNetwork(); } 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 e829666577..9bef35e3b5 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 @@ -31,8 +31,14 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Files; @@ -50,9 +56,11 @@ import org.mockito.InjectMocks; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; import org.onap.so.client.aai.AAIClient; +import org.onap.so.client.aai.AAIObjectType; 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.entities.uri.Depth; import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException; import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException; import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException; @@ -227,4 +235,42 @@ public class ServiceInstanceUriTest { exception.expect(NotFoundException.class); spy.build(); } + + @Test + public void serializeTest() throws IOException, ClassNotFoundException, GraphInventoryUriNotFoundException, + GraphInventoryPayloadException { + ServiceInstanceUri instance = new ServiceInstanceUri("key3"); + final String content = new String( + Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json"))); + + ServiceInstanceUri spy = spy(instance); + AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class); + AAIResultWrapper wrapper = mock(AAIResultWrapper.class); + when(mockResourcesClient.get(ArgumentMatchers.any(AAIResourceUri.class), + ArgumentMatchers.>any())).thenReturn(wrapper); + when(wrapper.getJson()).thenReturn(content); + when(spy.getResourcesClient()).thenReturn(mockResourcesClient); + spy.build(); + instance = spy.clone(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos); + objectOutputStream.writeObject(instance); + objectOutputStream.flush(); + objectOutputStream.close(); + + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + + ObjectInputStream objectInputStream = new ObjectInputStream(bis); + ServiceInstanceUri e2 = (ServiceInstanceUri) objectInputStream.readObject(); + objectInputStream.close(); + + ServiceInstanceUri spy2 = spy(e2); + + assertEquals(spy2.build().toString(), instance.build().toString()); + + // use the cached value do not call out to external system + verify(spy2, times(0)).getResourcesClient(); + + } } -- 2.16.6