HttpLookupUri is now serializable again 70/88270/3
authorBenjamin, Max (mb388a) <mb388a@us.att.com>
Wed, 22 May 2019 15:08:32 +0000 (11:08 -0400)
committerBenjamin, Max (mb388a) <mb388a@us.att.com>
Wed, 22 May 2019 16:11:24 +0000 (12:11 -0400)
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) <mb388a@us.att.com>
common/src/main/java/org/onap/so/client/aai/entities/uri/HttpLookupUri.java
common/src/test/java/org/onap/so/client/aai/entities/uri/ServiceInstanceUriTest.java

index f086a6a..37d21b3 100644 (file)
@@ -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<String> cachedValue = Optional.empty();
+    private transient Optional<String> 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<String> cachedValueOpt = this.getCachedValue();
-        return cachedValueOpt.isPresent() ? cachedValueOpt.get() : "";
+        return cachedValue.get();
     }
 
     protected Optional<String> 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();
 }
index e829666..9bef35e 100644 (file)
@@ -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.<AAIResourceUri>any(AAIResourceUri.class),
+                ArgumentMatchers.<Class<NotFoundException>>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();
+
+    }
 }