From a9ceeefef9a561937b42ed23bc5797930d5efefe Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Mon, 11 Feb 2019 14:56:03 -0500 Subject: restructured a&ai client made generics a bit safer and updated groovy tests Change-Id: I6d7a2567cd9bf795e0cfa4914a7674a4ed5f50c4 Issue-ID: SO-1493 Signed-off-by: Benjamin, Max (mb388a) --- .../aai/AAICommonObjectMapperPatchProvider.java | 8 +- .../client/aai/AAICommonObjectMapperProvider.java | 27 +--- .../org/onap/so/client/aai/AAIPatchConverter.java | 81 ------------ .../org/onap/so/client/aai/AAIResourcesClient.java | 136 ++++++++----------- .../java/org/onap/so/client/aai/AAIRestClient.java | 5 +- .../so/client/aai/AAISingleTransactionClient.java | 88 ++++++------ .../onap/so/client/aai/AAITransactionalClient.java | 98 ++++++-------- .../so/client/aai/EmptyStringToNullSerializer.java | 52 -------- .../so/client/aai/entities/AAIResultWrapper.java | 84 +----------- .../EmptyStringToNullSerializer.java | 52 ++++++++ ...phInventoryCommonObjectMapperPatchProvider.java | 35 +++++ .../GraphInventoryCommonObjectMapperProvider.java | 52 ++++++++ .../GraphInventoryPatchConverter.java | 83 ++++++++++++ .../GraphInventoryResourcesClient.java | 147 +++++++++++++++++++++ .../GraphInventorySingleTransactionClient.java | 87 ++++++++++++ .../GraphInventoryTransactionalClient.java | 94 +++++++++++++ .../entities/GraphInventoryResultWrapper.java | 117 ++++++++++++++++ .../onap/so/client/aai/AAIPatchConverterTest.java | 112 ---------------- .../org/onap/so/client/aai/AAIRestClientTest.java | 3 +- .../client/aai/AAISingleTransactionClientTest.java | 3 +- .../so/client/aai/AAITransactionalClientTest.java | 3 +- .../GraphInventoryPatchConverterTest.java | 114 ++++++++++++++++ 22 files changed, 936 insertions(+), 545 deletions(-) delete mode 100644 common/src/main/java/org/onap/so/client/aai/AAIPatchConverter.java delete mode 100644 common/src/main/java/org/onap/so/client/aai/EmptyStringToNullSerializer.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/EmptyStringToNullSerializer.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryCommonObjectMapperPatchProvider.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryCommonObjectMapperProvider.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryPatchConverter.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryResourcesClient.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySingleTransactionClient.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionalClient.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryResultWrapper.java delete mode 100644 common/src/test/java/org/onap/so/client/aai/AAIPatchConverterTest.java create mode 100644 common/src/test/java/org/onap/so/client/graphinventory/GraphInventoryPatchConverterTest.java (limited to 'common') diff --git a/common/src/main/java/org/onap/so/client/aai/AAICommonObjectMapperPatchProvider.java b/common/src/main/java/org/onap/so/client/aai/AAICommonObjectMapperPatchProvider.java index 33c9769400..9c8345d4b6 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAICommonObjectMapperPatchProvider.java +++ b/common/src/main/java/org/onap/so/client/aai/AAICommonObjectMapperPatchProvider.java @@ -20,16 +20,12 @@ package org.onap.so.client.aai; -import com.fasterxml.jackson.databind.module.SimpleModule; +import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperPatchProvider; -public class AAICommonObjectMapperPatchProvider extends AAICommonObjectMapperProvider { +public class AAICommonObjectMapperPatchProvider extends GraphInventoryCommonObjectMapperPatchProvider { public AAICommonObjectMapperPatchProvider() { super(); - EmptyStringToNullSerializer sp = new EmptyStringToNullSerializer(); - SimpleModule emptyStringModule = new SimpleModule(); - emptyStringModule.addSerializer(String.class, sp); - mapper.registerModule(emptyStringModule); } } diff --git a/common/src/main/java/org/onap/so/client/aai/AAICommonObjectMapperProvider.java b/common/src/main/java/org/onap/so/client/aai/AAICommonObjectMapperProvider.java index 0e2071842f..15bc2ea8ef 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAICommonObjectMapperProvider.java +++ b/common/src/main/java/org/onap/so/client/aai/AAICommonObjectMapperProvider.java @@ -20,33 +20,12 @@ package org.onap.so.client.aai; -import org.onap.so.client.policy.CommonObjectMapperProvider; +import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; -import com.fasterxml.jackson.databind.AnnotationIntrospector; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.MapperFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; -import com.fasterxml.jackson.databind.type.TypeFactory; -import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector; - -public class AAICommonObjectMapperProvider extends CommonObjectMapperProvider { +public class AAICommonObjectMapperProvider extends GraphInventoryCommonObjectMapperProvider { public AAICommonObjectMapperProvider() { - mapper = new ObjectMapper(); - mapper.setSerializationInclusion(Include.NON_NULL); - mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); - mapper.enable(MapperFeature.USE_ANNOTATIONS); - mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); - mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - AnnotationIntrospector aiJaxb = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()); - AnnotationIntrospector aiJackson = new JacksonAnnotationIntrospector(); - // first Jaxb, second Jackson annotations - mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(aiJaxb, aiJackson)); + super(); } } diff --git a/common/src/main/java/org/onap/so/client/aai/AAIPatchConverter.java b/common/src/main/java/org/onap/so/client/aai/AAIPatchConverter.java deleted file mode 100644 index 6ccb592409..0000000000 --- a/common/src/main/java/org/onap/so/client/aai/AAIPatchConverter.java +++ /dev/null @@ -1,81 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 - 2018 AT&T Intellectual Property. 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.so.client.aai; - -import java.util.List; -import java.util.Map; -import java.util.regex.Pattern; - -import org.onap.so.client.graphinventory.exceptions.GraphInventoryPatchDepthExceededException; -import org.onap.so.jsonpath.JsonPathUtil; - -import com.fasterxml.jackson.core.JsonProcessingException; - -public class AAIPatchConverter { - - private static final AAICommonObjectMapperProvider standardProvider = new AAICommonObjectMapperProvider(); - private static final AAICommonObjectMapperPatchProvider patchProvider = new AAICommonObjectMapperPatchProvider(); - private static final Pattern LOCATE_COMPLEX_OBJECT = Pattern.compile("^((?!relationship-list).)+?\\['[^\\[\\]]+?'\\]$"); - - - protected String convertPatchFormat(Object obj) { - return validatePatchObject(marshallObjectToPatchFormat(obj)); - } - - protected String validatePatchObject(String payload) { - if (hasComplexObject(payload)) { - throw new GraphInventoryPatchDepthExceededException(payload); - } - - return payload; - } - - /** validates client side that json does not include any complex objects - * relationship-list is omitted from this validation - */ - protected boolean hasComplexObject(String json) { - if (json.isEmpty()) { - return false; - } - String complex = "$.*.*"; - String array = "$.*.*.*"; - List result = JsonPathUtil.getInstance().getPathList(json, complex); - List result2 = JsonPathUtil.getInstance().getPathList(json, array); - - result.addAll(result2); - return result.stream().anyMatch(item -> LOCATE_COMPLEX_OBJECT.matcher(item).find()); - } - - protected String marshallObjectToPatchFormat(Object obj) { - Object value = obj; - try { - if (!(obj instanceof Map || obj instanceof String)) { - value = patchProvider.getMapper().writeValueAsString(obj); - } else if (obj instanceof Map) { - value = standardProvider.getMapper().writeValueAsString(obj); - } - } catch (JsonProcessingException e) { - value = "{}"; - } - - return (String)value; - } -} 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 87951d516b..288ac9bc7f 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 @@ -36,9 +36,10 @@ import org.onap.so.client.aai.entities.AAIEdgeLabel; import org.onap.so.client.aai.entities.AAIResultWrapper; import org.onap.so.client.aai.entities.uri.AAIResourceUri; import org.onap.so.client.aai.entities.uri.AAIUri; +import org.onap.so.client.graphinventory.GraphInventoryResourcesClient; import org.onap.so.client.graphinventory.entities.uri.Depth; -public class AAIResourcesClient extends AAIClient { +public class AAIResourcesClient extends AAIClient implements GraphInventoryResourcesClient { public AAIResourcesClient() { super(); @@ -49,37 +50,30 @@ public class AAIResourcesClient extends AAIClient { this.version = version; } - /** - * creates a new object in A&AI - * - * @param obj - can be any object which will marshal into a valid A&AI payload - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#create(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) */ + @Override public void create(AAIResourceUri uri, Object obj) { RestClient aaiRC = this.createClient(uri); aaiRC.put(obj); return; } - /** - * creates a new object in A&AI with no payload body - * - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#createEmpty(org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public void createEmpty(AAIResourceUri uri) { RestClient aaiRC = this.createClient(uri); aaiRC.put(""); return; } - /** - * returns false if the object does not exist in A&AI - * - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#exists(org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public boolean exists(AAIResourceUri uri) { AAIUri forceMinimal = this.addParams(Optional.of(Depth.ZERO), true, uri); try { @@ -91,12 +85,10 @@ public class AAIResourcesClient extends AAIClient { } } - /** - * Adds a relationship between two objects in A&AI - * @param uriA - * @param uriB - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public void connect(AAIResourceUri uriA, AAIResourceUri uriB) { AAIResourceUri uriAClone = uriA.clone(); RestClient aaiRC = this.createClient(uriAClone.relationshipAPI()); @@ -104,14 +96,10 @@ public class AAIResourcesClient extends AAIClient { return; } - /** - * Adds a relationship between two objects in A&AI - * with a given edge label - * @param uriA - * @param uriB - * @param edge label - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.AAIEdgeLabel) */ + @Override public void connect(AAIResourceUri uriA, AAIResourceUri uriB, AAIEdgeLabel label) { AAIResourceUri uriAClone = uriA.clone(); RestClient aaiRC = this.createClient(uriAClone.relationshipAPI()); @@ -119,13 +107,10 @@ public class AAIResourcesClient extends AAIClient { return; } - /** - * Removes relationship from two objects in A&AI - * - * @param uriA - * @param uriB - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#disconnect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public void disconnect(AAIResourceUri uriA, AAIResourceUri uriB) { AAIResourceUri uriAClone = uriA.clone(); RestClient aaiRC = this.createClient(uriAClone.relationshipAPI()); @@ -133,12 +118,10 @@ public class AAIResourcesClient extends AAIClient { return; } - /** - * Deletes object from A&AI. Automatically handles resource-version. - * - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#delete(org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public void delete(AAIResourceUri uri) { AAIResourceUri clone = uri.clone(); RestClient aaiRC = this.createClient(clone); @@ -150,23 +133,20 @@ public class AAIResourcesClient extends AAIClient { return; } - /** - * @param obj - can be any object which will marshal into a valid A&AI payload - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#update(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) */ + @Override public void update(AAIResourceUri uri, Object obj) { RestClient aaiRC = this.createClient(uri); aaiRC.patch(obj); return; } - /** - * Retrieves an object from A&AI and unmarshalls it into the Class specified - * @param clazz - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#get(java.lang.Class, org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public Optional get(Class clazz, AAIResourceUri uri) { try { return this.createClient(uri).get(clazz); @@ -179,11 +159,10 @@ public class AAIResourcesClient extends AAIClient { } } - /** - * Retrieves an object from A&AI and returns complete response - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#getFullResponse(org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public Response getFullResponse(AAIResourceUri uri) { try { return this.createClient(uri).get(); @@ -196,12 +175,10 @@ public class AAIResourcesClient extends AAIClient { } } - /** - * Retrieves an object from A&AI and automatically unmarshalls it into a Map or List - * @param resultClass - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#get(javax.ws.rs.core.GenericType, org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public Optional get(GenericType resultClass, AAIResourceUri uri) { try { return this.createClient(uri).get(resultClass); @@ -214,12 +191,10 @@ public class AAIResourcesClient extends AAIClient { } } - /** - * Retrieves an object from A&AI wrapped in a helper class which offer additional features - * - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#get(org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public AAIResultWrapper get(AAIResourceUri uri) { String json; try { @@ -234,13 +209,10 @@ public class AAIResourcesClient extends AAIClient { return new AAIResultWrapper(json); } - /** - * Retrieves an object from A&AI wrapped in a helper class which offer additional features - * If the object cannot be found in A&AI the method will throw the runtime exception - * included as an argument - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#get(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Class) */ + @Override public AAIResultWrapper get(AAIResourceUri uri, Class c) { String json; try { @@ -285,13 +257,10 @@ public class AAIResourcesClient extends AAIClient { return result; } - /** - * Will automatically create the object if it does not exist - * - * @param obj - Optional object which serializes to a valid A&AI payload - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#createIfNotExists(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.Optional) */ + @Override public AAIResourcesClient createIfNotExists(AAIResourceUri uri, Optional obj) { if(!this.exists(uri)){ if (obj.isPresent()) { @@ -304,20 +273,18 @@ public class AAIResourcesClient extends AAIClient { return this; } - /** - * Starts a transaction which encloses multiple A&AI mutations - * - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#beginTransaction() */ + @Override public AAITransactionalClient beginTransaction() { return new AAITransactionalClient(this.getVersion()); } - /** - * Starts a transaction groups multiple A&AI mutations - * - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#beginSingleTransaction() */ + @Override public AAISingleTransactionClient beginSingleTransaction() { return new AAISingleTransactionClient(this.getVersion()); } @@ -333,6 +300,9 @@ public class AAIResourcesClient extends AAIClient { return clone; } + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryResourcesClient#getRestProperties() + */ @Override public T getRestProperties() { return super.getRestProperties(); diff --git a/common/src/main/java/org/onap/so/client/aai/AAIRestClient.java b/common/src/main/java/org/onap/so/client/aai/AAIRestClient.java index 4f235c35f1..a2651195ee 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAIRestClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAIRestClient.java @@ -28,6 +28,7 @@ import javax.ws.rs.core.Response; import org.onap.so.client.ResponseExceptionMapper; import org.onap.so.client.RestClientSSL; +import org.onap.so.client.graphinventory.GraphInventoryPatchConverter; import org.onap.so.client.policy.CommonObjectMapperProvider; import org.onap.so.utils.TargetEntity; @@ -36,7 +37,7 @@ public class AAIRestClient extends RestClientSSL { private final AAIProperties aaiProperties; private static final AAICommonObjectMapperProvider standardProvider = new AAICommonObjectMapperProvider(); - private final AAIPatchConverter patchConverter = new AAIPatchConverter(); + private final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter(); protected AAIRestClient(AAIProperties props, URI uri) { super(props, Optional.of(uri)); @@ -81,7 +82,7 @@ public class AAIRestClient extends RestClientSSL { return super.patch(convertToPatchFormat(obj), resultClass); } - protected AAIPatchConverter getPatchConverter() { + protected GraphInventoryPatchConverter getPatchConverter() { return this.patchConverter; } diff --git a/common/src/main/java/org/onap/so/client/aai/AAISingleTransactionClient.java b/common/src/main/java/org/onap/so/client/aai/AAISingleTransactionClient.java index 2ecdb7c480..ba65ac3f15 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAISingleTransactionClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAISingleTransactionClient.java @@ -43,19 +43,21 @@ import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRespon import org.onap.so.client.aai.entities.uri.AAIResourceUri; import org.onap.so.client.aai.entities.uri.AAIUri; import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.so.client.graphinventory.GraphInventoryPatchConverter; +import org.onap.so.client.graphinventory.GraphInventorySingleTransactionClient; import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Joiner; -public class AAISingleTransactionClient extends AAIClient { +public class AAISingleTransactionClient extends AAIClient implements GraphInventorySingleTransactionClient { private final SingleTransactionRequest request; private final AAIVersion version; private int actionCount = 0; - private final AAIPatchConverter patchConverter = new AAIPatchConverter(); + private final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter(); protected AAISingleTransactionClient(AAIVersion version) { super(); @@ -63,37 +65,30 @@ public class AAISingleTransactionClient extends AAIClient { this.request = new SingleTransactionRequest(); } - /** - * creates a new object in A&AI - * - * @param obj - can be any object which will marshal into a valid A&AI payload - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#create(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) */ + @Override public AAISingleTransactionClient create(AAIResourceUri uri, Object obj) { request.getOperations().add(new OperationBodyRequest().withAction("put").withUri(uri.build().toString()).withBody(obj)); incrementActionAmount(); return this; } - /** - * creates a new object in A&AI with no payload body - * - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#createEmpty(org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public AAISingleTransactionClient createEmpty(AAIResourceUri uri) { request.getOperations().add(new OperationBodyRequest().withAction("put").withUri(uri.build().toString()).withBody(new HashMap())); incrementActionAmount(); return this; } - /** - * Adds a relationship between two objects in A&AI - * @param uriA - * @param uriB - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public AAISingleTransactionClient connect(AAIResourceUri uriA, AAIResourceUri uriB) { AAIResourceUri uriAClone = uriA.clone(); request.getOperations().add(new OperationBodyRequest().withAction("put").withUri(uriAClone.relationshipAPI().build().toString()).withBody(this.buildRelationship(uriB))); @@ -101,13 +96,10 @@ public class AAISingleTransactionClient extends AAIClient { return this; } - /** - * relationship between multiple objects in A&AI - connects A to all objects specified in list - * - * @param uriA - * @param uris - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List) */ + @Override public AAISingleTransactionClient connect(AAIResourceUri uriA, List uris) { for (AAIResourceUri uri : uris) { this.connect(uriA, uri); @@ -115,6 +107,10 @@ public class AAISingleTransactionClient extends AAIClient { return this; } + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.AAIEdgeLabel) + */ + @Override public AAISingleTransactionClient connect(AAIResourceUri uriA, AAIResourceUri uriB, AAIEdgeLabel label) { AAIResourceUri uriAClone = uriA.clone(); RestClient aaiRC = this.createClient(uriAClone.relationshipAPI()); @@ -122,6 +118,10 @@ public class AAISingleTransactionClient extends AAIClient { return this; } + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List, org.onap.so.client.aai.entities.AAIEdgeLabel) + */ + @Override public AAISingleTransactionClient connect(AAIResourceUri uriA, List uris, AAIEdgeLabel label) { for (AAIResourceUri uri : uris) { this.connect(uriA, uri, label); @@ -129,13 +129,10 @@ public class AAISingleTransactionClient extends AAIClient { return this; } - /** - * Removes relationship from two objects in A&AI - * - * @param uriA - * @param uriB - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#disconnect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public AAISingleTransactionClient disconnect(AAIResourceUri uriA, AAIResourceUri uriB) { AAIResourceUri uriAClone = uriA.clone(); request.getOperations().add(new OperationBodyRequest().withAction("delete").withUri(uriAClone.relationshipAPI().build().toString()).withBody(this.buildRelationship(uriB))); @@ -143,24 +140,20 @@ public class AAISingleTransactionClient extends AAIClient { return this; } - /** - * Removes relationship from multiple objects - disconnects A from all objects specified in list - * @param uriA - * @param uris - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#disconnect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List) */ + @Override public AAISingleTransactionClient disconnect(AAIResourceUri uriA, List uris) { for (AAIResourceUri uri : uris) { this.disconnect(uriA, uri); } return this; } - /** - * Deletes object from A&AI. Automatically handles resource-version. - * - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#delete(org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public AAISingleTransactionClient delete(AAIResourceUri uri) { AAIResourcesClient client = new AAIResourcesClient(); AAIResourceUri clone = uri.clone(); @@ -172,11 +165,10 @@ public class AAISingleTransactionClient extends AAIClient { return this; } - /** - * @param obj - can be any object which will marshal into a valid A&AI payload - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#update(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) */ + @Override public AAISingleTransactionClient update(AAIResourceUri uri, Object obj) { final String payload = getPatchConverter().convertPatchFormat(obj); @@ -188,10 +180,10 @@ public class AAISingleTransactionClient extends AAIClient { private void incrementActionAmount() { actionCount++; } - /** - * Executes all created transactions in A&AI - * @throws BulkProcessFailed + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionClient#execute() */ + @Override public void execute() throws BulkProcessFailed { RestClient client = this.createClient(AAIUriFactory.createResourceUri(AAIObjectType.SINGLE_TRANSACTION)); try { @@ -261,7 +253,7 @@ public class AAISingleTransactionClient extends AAIClient { return this.request; } - protected AAIPatchConverter getPatchConverter() { + protected GraphInventoryPatchConverter getPatchConverter() { return this.patchConverter; } } diff --git a/common/src/main/java/org/onap/so/client/aai/AAITransactionalClient.java b/common/src/main/java/org/onap/so/client/aai/AAITransactionalClient.java index 118a3edf1c..dd4cb2f591 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAITransactionalClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAITransactionalClient.java @@ -20,8 +20,6 @@ package org.onap.so.client.aai; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; - import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -42,8 +40,9 @@ import org.onap.so.client.aai.entities.bulkprocess.OperationBody; import org.onap.so.client.aai.entities.bulkprocess.Transaction; import org.onap.so.client.aai.entities.bulkprocess.Transactions; import org.onap.so.client.aai.entities.uri.AAIResourceUri; -import org.onap.so.client.aai.entities.uri.AAIUri; import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.so.client.graphinventory.GraphInventoryPatchConverter; +import org.onap.so.client.graphinventory.GraphInventoryTransactionalClient; import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed; import org.onap.so.jsonpath.JsonPathUtil; @@ -51,14 +50,14 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Joiner; -public class AAITransactionalClient extends AAIClient { +public class AAITransactionalClient extends AAIClient implements GraphInventoryTransactionalClient { private final Transactions transactions; private Transaction currentTransaction; private final AAIVersion version; private int actionCount = 0; - private final AAIPatchConverter patchConverter = new AAIPatchConverter(); + private final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter(); protected AAITransactionalClient(AAIVersion version) { super(); @@ -73,47 +72,39 @@ public class AAITransactionalClient extends AAIClient { currentTransaction = transaction; } - /** - * adds an additional transaction and closes the previous transaction - * - * @return AAITransactionalClient + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#beginNewTransaction() */ + @Override public AAITransactionalClient beginNewTransaction() { startTransaction(); return this; } - /** - * creates a new object in A&AI - * - * @param obj - can be any object which will marshal into a valid A&AI payload - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#create(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) */ + @Override public AAITransactionalClient create(AAIResourceUri uri, Object obj) { currentTransaction.getPut().add(new OperationBody().withUri(uri.build().toString()).withBody(obj)); incrementActionAmount(); return this; } - /** - * creates a new object in A&AI with no payload body - * - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#createEmpty(org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public AAITransactionalClient createEmpty(AAIResourceUri uri) { currentTransaction.getPut().add(new OperationBody().withUri(uri.build().toString()).withBody(new HashMap())); incrementActionAmount(); return this; } - /** - * Adds a relationship between two objects in A&AI - * @param uriA - * @param uriB - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public AAITransactionalClient connect(AAIResourceUri uriA, AAIResourceUri uriB) { AAIResourceUri uriAClone = uriA.clone(); currentTransaction.getPut().add(new OperationBody().withUri(uriAClone.relationshipAPI().build().toString()).withBody(this.buildRelationship(uriB))); @@ -121,13 +112,10 @@ public class AAITransactionalClient extends AAIClient { return this; } - /** - * relationship between multiple objects in A&AI - connects A to all objects specified in list - * - * @param uriA - * @param uris - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List) */ + @Override public AAITransactionalClient connect(AAIResourceUri uriA, List uris) { for (AAIResourceUri uri : uris) { this.connect(uriA, uri); @@ -135,6 +123,10 @@ public class AAITransactionalClient extends AAIClient { return this; } + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.AAIEdgeLabel) + */ + @Override public AAITransactionalClient connect(AAIResourceUri uriA, AAIResourceUri uriB, AAIEdgeLabel label) { AAIResourceUri uriAClone = uriA.clone(); RestClient aaiRC = this.createClient(uriAClone.relationshipAPI()); @@ -142,6 +134,10 @@ public class AAITransactionalClient extends AAIClient { return this; } + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List, org.onap.so.client.aai.entities.AAIEdgeLabel) + */ + @Override public AAITransactionalClient connect(AAIResourceUri uriA, List uris, AAIEdgeLabel label) { for (AAIResourceUri uri : uris) { this.connect(uriA, uri, label); @@ -149,13 +145,10 @@ public class AAITransactionalClient extends AAIClient { return this; } - /** - * Removes relationship from two objects in A&AI - * - * @param uriA - * @param uriB - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#disconnect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public AAITransactionalClient disconnect(AAIResourceUri uriA, AAIResourceUri uriB) { AAIResourceUri uriAClone = uriA.clone(); currentTransaction.getDelete().add(new OperationBody().withUri(uriAClone.relationshipAPI().build().toString()).withBody(this.buildRelationship(uriB))); @@ -163,24 +156,20 @@ public class AAITransactionalClient extends AAIClient { return this; } - /** - * Removes relationship from multiple objects - disconnects A from all objects specified in list - * @param uriA - * @param uris - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#disconnect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List) */ + @Override public AAITransactionalClient disconnect(AAIResourceUri uriA, List uris) { for (AAIResourceUri uri : uris) { this.disconnect(uriA, uri); } return this; } - /** - * Deletes object from A&AI. Automatically handles resource-version. - * - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#delete(org.onap.so.client.aai.entities.uri.AAIResourceUri) */ + @Override public AAITransactionalClient delete(AAIResourceUri uri) { AAIResourcesClient client = new AAIResourcesClient(); AAIResourceUri clone = uri.clone(); @@ -192,11 +181,10 @@ public class AAITransactionalClient extends AAIClient { return this; } - /** - * @param obj - can be any object which will marshal into a valid A&AI payload - * @param uri - * @return + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#update(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) */ + @Override public AAITransactionalClient update(AAIResourceUri uri, Object obj) { final String payload = getPatchConverter().convertPatchFormat(obj); currentTransaction.getPatch().add(new OperationBody().withUri(uri.build().toString()).withBody(payload)); @@ -207,10 +195,10 @@ public class AAITransactionalClient extends AAIClient { private void incrementActionAmount() { actionCount++; } - /** - * Executes all created transactions in A&AI - * @throws BulkProcessFailed + /* (non-Javadoc) + * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#execute() */ + @Override public void execute() throws BulkProcessFailed { RestClient client = this.createClient(AAIUriFactory.createResourceUri(AAIObjectType.BULK_PROCESS)); try { @@ -293,7 +281,7 @@ public class AAITransactionalClient extends AAIClient { return this.transactions; } - protected AAIPatchConverter getPatchConverter() { + protected GraphInventoryPatchConverter getPatchConverter() { return this.patchConverter; } } diff --git a/common/src/main/java/org/onap/so/client/aai/EmptyStringToNullSerializer.java b/common/src/main/java/org/onap/so/client/aai/EmptyStringToNullSerializer.java deleted file mode 100644 index 1120ebe0b9..0000000000 --- a/common/src/main/java/org/onap/so/client/aai/EmptyStringToNullSerializer.java +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 - 2018 AT&T Intellectual Property. 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.so.client.aai; - -import java.io.IOException; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.ser.std.StdSerializer; - -public class EmptyStringToNullSerializer extends StdSerializer { - - private static final long serialVersionUID = 5367385969270400106L; - - public EmptyStringToNullSerializer() { - this(null); - } - public EmptyStringToNullSerializer(Class t) { - super(t); - } - - @Override - public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) - throws IOException, JsonProcessingException { - - if("".equals(value)) { - gen.writeNull(); - } else { - gen.writeString(value); - } - } - -} diff --git a/common/src/main/java/org/onap/so/client/aai/entities/AAIResultWrapper.java b/common/src/main/java/org/onap/so/client/aai/entities/AAIResultWrapper.java index 77ea9bcdfe..9b3f98baa4 100644 --- a/common/src/main/java/org/onap/so/client/aai/entities/AAIResultWrapper.java +++ b/common/src/main/java/org/onap/so/client/aai/entities/AAIResultWrapper.java @@ -20,96 +20,22 @@ package org.onap.so.client.aai.entities; -import java.io.IOException; import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -import org.onap.so.client.aai.AAICommonObjectMapperProvider; -import org.onap.so.jsonpath.JsonPathUtil; +import org.onap.so.client.graphinventory.entities.GraphInventoryResultWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class AAIResultWrapper implements Serializable { +public class AAIResultWrapper extends GraphInventoryResultWrapper implements Serializable { private static final long serialVersionUID = 5895841925807816737L; - private final String jsonBody; - private final ObjectMapper mapper; - private final transient Logger logger = LoggerFactory.getLogger(AAIResultWrapper.class); + private final static transient Logger logger = LoggerFactory.getLogger(AAIResultWrapper.class); public AAIResultWrapper(String json) { - this.jsonBody = json; - this.mapper = new AAICommonObjectMapperProvider().getMapper(); + super(json, logger); } public AAIResultWrapper(Object aaiObject) { - this.mapper = new AAICommonObjectMapperProvider().getMapper(); - this.jsonBody = mapObjectToString(aaiObject); - } - - protected String mapObjectToString(Object aaiObject) { - try { - return mapper.writeValueAsString(aaiObject); - } catch (JsonProcessingException e) { - logger.warn("could not parse object into json - defaulting to {}"); - return "{}"; - } + super(aaiObject, logger); } - public Optional getRelationships() { - final String path = "$.relationship-list"; - if (isEmpty()) { - return Optional.empty(); - } - Optional result = JsonPathUtil.getInstance().locateResult(jsonBody, path); - if (result.isPresent()) { - return Optional.of(new Relationships(result.get())); - } else { - return Optional.empty(); - } - } - - public String getJson() { - if(jsonBody == null) { - return "{}"; - } else { - return jsonBody; - } - } - - public Map asMap() { - if (isEmpty()) { - return new HashMap<>(); - } - try { - return mapper.readValue(this.jsonBody, new TypeReference>(){}); - } catch (IOException e) { - return new HashMap<>(); - } - } - - public Optional asBean(Class clazz) { - if (isEmpty()) { - return Optional.empty(); - } - try { - return Optional.of(mapper.readValue(this.jsonBody, clazz)); - } catch (IOException e) { - return Optional.empty(); - } - } - - public boolean isEmpty() { - return jsonBody == null; - } - @Override - public String toString() { - return this.getJson(); - } - } diff --git a/common/src/main/java/org/onap/so/client/graphinventory/EmptyStringToNullSerializer.java b/common/src/main/java/org/onap/so/client/graphinventory/EmptyStringToNullSerializer.java new file mode 100644 index 0000000000..e21386f809 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/EmptyStringToNullSerializer.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 AT&T Intellectual Property. 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.so.client.graphinventory; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class EmptyStringToNullSerializer extends StdSerializer { + + private static final long serialVersionUID = 5367385969270400106L; + + public EmptyStringToNullSerializer() { + this(null); + } + public EmptyStringToNullSerializer(Class t) { + super(t); + } + + @Override + public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) + throws IOException, JsonProcessingException { + + if("".equals(value)) { + gen.writeNull(); + } else { + gen.writeString(value); + } + } + +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryCommonObjectMapperPatchProvider.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryCommonObjectMapperPatchProvider.java new file mode 100644 index 0000000000..47c9e77b84 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryCommonObjectMapperPatchProvider.java @@ -0,0 +1,35 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 AT&T Intellectual Property. 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.so.client.graphinventory; + +import com.fasterxml.jackson.databind.module.SimpleModule; + +public class GraphInventoryCommonObjectMapperPatchProvider extends GraphInventoryCommonObjectMapperProvider { + + + public GraphInventoryCommonObjectMapperPatchProvider() { + super(); + EmptyStringToNullSerializer sp = new EmptyStringToNullSerializer(); + SimpleModule emptyStringModule = new SimpleModule(); + emptyStringModule.addSerializer(String.class, sp); + mapper.registerModule(emptyStringModule); + } +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryCommonObjectMapperProvider.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryCommonObjectMapperProvider.java new file mode 100644 index 0000000000..f9857424a2 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryCommonObjectMapperProvider.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.so.client.graphinventory; + +import org.onap.so.client.policy.CommonObjectMapperProvider; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.databind.AnnotationIntrospector; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; +import com.fasterxml.jackson.databind.type.TypeFactory; +import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector; + +public class GraphInventoryCommonObjectMapperProvider extends CommonObjectMapperProvider { + + public GraphInventoryCommonObjectMapperProvider() { + mapper = new ObjectMapper(); + mapper.setSerializationInclusion(Include.NON_NULL); + mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + mapper.enable(MapperFeature.USE_ANNOTATIONS); + mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); + mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + AnnotationIntrospector aiJaxb = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()); + AnnotationIntrospector aiJackson = new JacksonAnnotationIntrospector(); + // first Jaxb, second Jackson annotations + mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(aiJaxb, aiJackson)); + } + +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryPatchConverter.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryPatchConverter.java new file mode 100644 index 0000000000..00e597b189 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryPatchConverter.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 AT&T Intellectual Property. 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.so.client.graphinventory; + +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +import org.onap.so.client.aai.AAICommonObjectMapperPatchProvider; +import org.onap.so.client.aai.AAICommonObjectMapperProvider; +import org.onap.so.client.graphinventory.exceptions.GraphInventoryPatchDepthExceededException; +import org.onap.so.jsonpath.JsonPathUtil; + +import com.fasterxml.jackson.core.JsonProcessingException; + +public class GraphInventoryPatchConverter { + + private static final AAICommonObjectMapperProvider standardProvider = new AAICommonObjectMapperProvider(); + private static final AAICommonObjectMapperPatchProvider patchProvider = new AAICommonObjectMapperPatchProvider(); + private static final Pattern LOCATE_COMPLEX_OBJECT = Pattern.compile("^((?!relationship-list).)+?\\['[^\\[\\]]+?'\\]$"); + + + public String convertPatchFormat(Object obj) { + return validatePatchObject(marshallObjectToPatchFormat(obj)); + } + + public String validatePatchObject(String payload) { + if (hasComplexObject(payload)) { + throw new GraphInventoryPatchDepthExceededException(payload); + } + + return payload; + } + + /** validates client side that json does not include any complex objects + * relationship-list is omitted from this validation + */ + protected boolean hasComplexObject(String json) { + if (json.isEmpty()) { + return false; + } + String complex = "$.*.*"; + String array = "$.*.*.*"; + List result = JsonPathUtil.getInstance().getPathList(json, complex); + List result2 = JsonPathUtil.getInstance().getPathList(json, array); + + result.addAll(result2); + return result.stream().anyMatch(item -> LOCATE_COMPLEX_OBJECT.matcher(item).find()); + } + + protected String marshallObjectToPatchFormat(Object obj) { + Object value = obj; + try { + if (!(obj instanceof Map || obj instanceof String)) { + value = patchProvider.getMapper().writeValueAsString(obj); + } else if (obj instanceof Map) { + value = standardProvider.getMapper().writeValueAsString(obj); + } + } catch (JsonProcessingException e) { + value = "{}"; + } + + return (String)value; + } +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryResourcesClient.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryResourcesClient.java new file mode 100644 index 0000000000..7fbe286b98 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryResourcesClient.java @@ -0,0 +1,147 @@ +package org.onap.so.client.graphinventory; + +import java.util.Optional; + +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.Response; + +import org.onap.so.client.RestProperties; +import org.onap.so.client.graphinventory.entities.GraphInventoryEdgeLabel; +import org.onap.so.client.graphinventory.entities.GraphInventoryResultWrapper; +import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; + +public interface GraphInventoryResourcesClient { + + /** + * creates a new object in GraphInventory + * + * @param obj - can be any object which will marshal into a valid GraphInventory payload + * @param uri + * @return + */ + void create(Uri uri, Object obj); + + /** + * creates a new object in GraphInventory with no payload body + * + * @param uri + * @return + */ + void createEmpty(Uri uri); + + /** + * returns false if the object does not exist in GraphInventory + * + * @param uri + * @return + */ + boolean exists(Uri uri); + + /** + * Adds a relationship between two objects in GraphInventory + * @param uriA + * @param uriB + * @return + */ + void connect(Uri uriA, Uri uriB); + + /** + * Adds a relationship between two objects in GraphInventory + * with a given edge label + * @param uriA + * @param uriB + * @param edge label + * @return + */ + void connect(Uri uriA, Uri uriB, EdgeLabel label); + + /** + * Removes relationship from two objects in GraphInventory + * + * @param uriA + * @param uriB + * @return + */ + void disconnect(Uri uriA, Uri uriB); + + /** + * Deletes object from GraphInventory. Automatically handles resource-version. + * + * @param uri + * @return + */ + void delete(Uri uri); + + /** + * @param obj - can be any object which will marshal into a valid GraphInventory payload + * @param uri + * @return + */ + void update(Uri uri, Object obj); + + /** + * Retrieves an object from GraphInventory and unmarshalls it into the Class specified + * @param clazz + * @param uri + * @return + */ + Optional get(Class clazz, Uri uri); + + /** + * Retrieves an object from GraphInventory and returns complete response + * @param uri + * @return + */ + Response getFullResponse(Uri uri); + + /** + * Retrieves an object from GraphInventory and automatically unmarshalls it into a Map or List + * @param resultClass + * @param uri + * @return + */ + Optional get(GenericType resultClass, Uri uri); + + /** + * Retrieves an object from GraphInventory wrapped in a helper class which offer additional features + * + * @param uri + * @return + */ + Wrapper get(Uri uri); + + /** + * Retrieves an object from GraphInventory wrapped in a helper class which offer additional features + * If the object cannot be found in GraphInventory the method will throw the runtime exception + * included as an argument + * @param uri + * @return + */ + Wrapper get(Uri uri, Class c); + + /** + * Will automatically create the object if it does not exist + * + * @param obj - Optional object which serializes to a valid GraphInventory payload + * @param uri + * @return + */ + Self createIfNotExists(Uri uri, Optional obj); + + /** + * Starts a transaction which encloses multiple GraphInventory mutations + * + * @return + */ + TransactionalClient beginTransaction(); + + /** + * Starts a transaction groups multiple GraphInventory mutations + * + * @return + */ + SingleTransactionClient beginSingleTransaction(); + + T getRestProperties(); + +} \ No newline at end of file diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySingleTransactionClient.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySingleTransactionClient.java new file mode 100644 index 0000000000..e1aa2252d9 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySingleTransactionClient.java @@ -0,0 +1,87 @@ +package org.onap.so.client.graphinventory; + +import java.util.List; + +import org.onap.so.client.graphinventory.entities.GraphInventoryEdgeLabel; +import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; +import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed; + +public interface GraphInventorySingleTransactionClient { + + /** + * creates a new object in A&AI + * + * @param obj - can be any object which will marshal into a valid A&AI payload + * @param uri + * @return + */ + Self create(Uri uri, Object obj); + + /** + * creates a new object in A&AI with no payload body + * + * @param uri + * @return + */ + Self createEmpty(Uri uri); + + /** + * Adds a relationship between two objects in A&AI + * @param uriA + * @param uriB + * @return + */ + Self connect(Uri uriA, Uri uriB); + + /** + * relationship between multiple objects in A&AI - connects A to all objects specified in list + * + * @param uriA + * @param uris + * @return + */ + Self connect(Uri uriA, List uris); + + Self connect(Uri uriA, Uri uriB, EdgeLabel label); + + Self connect(Uri uriA, List uris, EdgeLabel label); + + /** + * Removes relationship from two objects in A&AI + * + * @param uriA + * @param uriB + * @return + */ + Self disconnect(Uri uriA, Uri uriB); + + /** + * Removes relationship from multiple objects - disconnects A from all objects specified in list + * @param uriA + * @param uris + * @return + */ + Self disconnect(Uri uriA, List uris); + + /** + * Deletes object from A&AI. Automatically handles resource-version. + * + * @param uri + * @return + */ + Self delete(Uri uri); + + /** + * @param obj - can be any object which will marshal into a valid A&AI payload + * @param uri + * @return + */ + Self update(Uri uri, Object obj); + + /** + * Executes all created transactions in A&AI + * @throws BulkProcessFailed + */ + void execute() throws BulkProcessFailed; + +} \ No newline at end of file diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionalClient.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionalClient.java new file mode 100644 index 0000000000..a7362c85da --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionalClient.java @@ -0,0 +1,94 @@ +package org.onap.so.client.graphinventory; + +import java.util.List; + +import org.onap.so.client.graphinventory.entities.GraphInventoryEdgeLabel; +import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; +import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed; + +public interface GraphInventoryTransactionalClient { + + /** + * adds an additional transaction and closes the previous transaction + * + * @return Self + */ + Self beginNewTransaction(); + + /** + * creates a new object in A&AI + * + * @param obj - can be any object which will marshal into a valid A&AI payload + * @param uri + * @return + */ + Self create(Uri uri, Object obj); + + /** + * creates a new object in A&AI with no payload body + * + * @param uri + * @return + */ + Self createEmpty(Uri uri); + + /** + * Adds a relationship between two objects in A&AI + * @param uriA + * @param uriB + * @return + */ + Self connect(Uri uriA, Uri uriB); + + /** + * relationship between multiple objects in A&AI - connects A to all objects specified in list + * + * @param uriA + * @param uris + * @return + */ + Self connect(Uri uriA, List uris); + + Self connect(Uri uriA, Uri uriB, EdgeLabel label); + + Self connect(Uri uriA, List uris, EdgeLabel label); + + /** + * Removes relationship from two objects in A&AI + * + * @param uriA + * @param uriB + * @return + */ + Self disconnect(Uri uriA, Uri uriB); + + /** + * Removes relationship from multiple objects - disconnects A from all objects specified in list + * @param uriA + * @param uris + * @return + */ + Self disconnect(Uri uriA, List uris); + + /** + * Deletes object from A&AI. Automatically handles resource-version. + * + * @param uri + * @return + */ + Self delete(Uri uri); + + /** + * @param obj - can be any object which will marshal into a valid A&AI payload + * @param uri + * @return + */ + Self update(Uri uri, Object obj); + + /** + * Executes all created transactions in A&AI + * @throws BulkProcessFailed + */ + void execute() throws BulkProcessFailed; + +} \ No newline at end of file diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryResultWrapper.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryResultWrapper.java new file mode 100644 index 0000000000..cc1ce0063b --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/GraphInventoryResultWrapper.java @@ -0,0 +1,117 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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.so.client.graphinventory.entities; + +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import org.onap.so.client.aai.AAICommonObjectMapperProvider; +import org.onap.so.client.aai.entities.Relationships; +import org.onap.so.jsonpath.JsonPathUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class GraphInventoryResultWrapper implements Serializable { + + private static final long serialVersionUID = 5895841925807816727L; + protected final String jsonBody; + protected final ObjectMapper mapper; + private final transient Logger logger; + + protected GraphInventoryResultWrapper(String json, Logger logger) { + this.jsonBody = json; + this.mapper = new AAICommonObjectMapperProvider().getMapper(); + this.logger = logger; + } + + protected GraphInventoryResultWrapper(Object aaiObject, Logger logger) { + this.mapper = new AAICommonObjectMapperProvider().getMapper(); + this.jsonBody = mapObjectToString(aaiObject); + this.logger = logger; + } + + protected String mapObjectToString(Object aaiObject) { + try { + return mapper.writeValueAsString(aaiObject); + } catch (JsonProcessingException e) { + logger.warn("could not parse object into json - defaulting to {}"); + return "{}"; + } + } + public Optional getRelationships() { + final String path = "$.relationship-list"; + if (isEmpty()) { + return Optional.empty(); + } + Optional result = JsonPathUtil.getInstance().locateResult(jsonBody, path); + if (result.isPresent()) { + return Optional.of(new Relationships(result.get())); + } else { + return Optional.empty(); + } + } + + public String getJson() { + if(jsonBody == null) { + return "{}"; + } else { + return jsonBody; + } + } + + public Map asMap() { + if (isEmpty()) { + return new HashMap<>(); + } + try { + return mapper.readValue(this.jsonBody, new TypeReference>(){}); + } catch (IOException e) { + return new HashMap<>(); + } + } + + public Optional asBean(Class clazz) { + if (isEmpty()) { + return Optional.empty(); + } + try { + return Optional.of(mapper.readValue(this.jsonBody, clazz)); + } catch (IOException e) { + return Optional.empty(); + } + } + + public boolean isEmpty() { + return jsonBody == null; + } + @Override + public String toString() { + return this.getJson(); + } + +} diff --git a/common/src/test/java/org/onap/so/client/aai/AAIPatchConverterTest.java b/common/src/test/java/org/onap/so/client/aai/AAIPatchConverterTest.java deleted file mode 100644 index 0d4490f51d..0000000000 --- a/common/src/test/java/org/onap/so/client/aai/AAIPatchConverterTest.java +++ /dev/null @@ -1,112 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 - 2018 AT&T Intellectual Property. 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.so.client.aai; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.HashMap; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.onap.aai.domain.yang.GenericVnf; - -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; - - -@RunWith(MockitoJUnitRunner.class) -public class AAIPatchConverterTest { - - private ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper(); - - @Test - public void convertObjectToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException { - AAIPatchConverter validator = new AAIPatchConverter(); - GenericVnf vnf = new GenericVnf(); - vnf.setIpv4Loopback0Address(""); - String result = validator.marshallObjectToPatchFormat(vnf); - GenericVnf resultObj = mapper.readValue(result.toString(), GenericVnf.class); - assertTrue("expect object to become a String to prevent double marshalling", result instanceof String); - assertNull("expect null because of custom mapper", resultObj.getIpv4Loopback0Address()); - - } - - @Test - public void convertStringToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException { - AAIPatchConverter validator = new AAIPatchConverter(); - String payload = "{\"ipv4-loopback0-address\":\"\"}"; - String result = validator.marshallObjectToPatchFormat(payload); - - assertEquals("expect no change", payload, result); - } - - @Test - public void convertStringToPatchFormatNull_Test() throws URISyntaxException, JsonParseException, JsonMappingException, IOException { - AAIPatchConverter validator = new AAIPatchConverter(); - String payload = "{\"ipv4-loopback0-address\": null}"; - String result = validator.marshallObjectToPatchFormat(payload); - System.out.println(result); - assertEquals("expect no change", payload, result); - } - - @Test - public void convertMapToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException { - AAIPatchConverter validator = new AAIPatchConverter(); - HashMap map = new HashMap<>(); - map.put("ipv4-loopback0-address", ""); - map.put("ipv4-loopback1-address", "192.168.1.1"); - String result = validator.marshallObjectToPatchFormat(map); - - assertEquals("expect string", "{\"ipv4-loopback1-address\":\"192.168.1.1\"}", result); - } - - @Test - public void hasComplexObjectTest() { - AAIPatchConverter validator = new AAIPatchConverter(); - String hasNesting = "{ \"hello\" : \"world\", \"nested\" : { \"key\" : \"value\" } }"; - String noNesting = "{ \"hello\" : \"world\" }"; - String arrayCase = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"nestedComplex\" : [{\"key\" : \"value\"}]}"; - String empty = "{}"; - String arrayCaseSimpleOnly = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"]}"; - String relationshipListCaseNesting = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}], \"nested\" : { \"key\" : \"value\" }}"; - String relationshipListCase = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}]}"; - String nothing = ""; - - assertTrue("expect has nesting", validator.hasComplexObject(hasNesting)); - assertFalse("expect no nesting", validator.hasComplexObject(noNesting)); - assertTrue("expect has nesting", validator.hasComplexObject(arrayCase)); - assertFalse("expect no nesting", validator.hasComplexObject(empty)); - assertFalse("expect no nesting", validator.hasComplexObject(arrayCaseSimpleOnly)); - assertFalse("expect no nesting", validator.hasComplexObject(relationshipListCase)); - assertTrue("expect has nesting", validator.hasComplexObject(relationshipListCaseNesting)); - assertFalse("expect no nesting", validator.hasComplexObject(nothing)); - } - -} diff --git a/common/src/test/java/org/onap/so/client/aai/AAIRestClientTest.java b/common/src/test/java/org/onap/so/client/aai/AAIRestClientTest.java index ad15417b71..95b30f934b 100644 --- a/common/src/test/java/org/onap/so/client/aai/AAIRestClientTest.java +++ b/common/src/test/java/org/onap/so/client/aai/AAIRestClientTest.java @@ -41,6 +41,7 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.so.client.RestClientSSL; +import org.onap.so.client.graphinventory.GraphInventoryPatchConverter; import org.onap.so.client.graphinventory.exceptions.GraphInventoryPatchDepthExceededException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -68,7 +69,7 @@ public class AAIRestClientTest { public void verifyPatchValidation() throws URISyntaxException { AAIRestClient client = new AAIRestClient(props, new URI("")); AAIRestClient spy = spy(client); - AAIPatchConverter patchValidatorMock = mock(AAIPatchConverter.class); + GraphInventoryPatchConverter patchValidatorMock = mock(GraphInventoryPatchConverter.class); doReturn(patchValidatorMock).when(spy).getPatchConverter(); String payload = "{}"; doReturn(Response.ok().build()).when(spy).method(eq("PATCH"), any()); diff --git a/common/src/test/java/org/onap/so/client/aai/AAISingleTransactionClientTest.java b/common/src/test/java/org/onap/so/client/aai/AAISingleTransactionClientTest.java index 428fa276db..27637126c6 100644 --- a/common/src/test/java/org/onap/so/client/aai/AAISingleTransactionClientTest.java +++ b/common/src/test/java/org/onap/so/client/aai/AAISingleTransactionClientTest.java @@ -45,6 +45,7 @@ import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRespon import org.onap.so.client.aai.entities.uri.AAIResourceUri; import org.onap.so.client.aai.entities.uri.AAIUriFactory; import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl; +import org.onap.so.client.graphinventory.GraphInventoryPatchConverter; import org.skyscreamer.jsonassert.JSONAssert; import com.fasterxml.jackson.core.JsonParseException; @@ -117,7 +118,7 @@ public class AAISingleTransactionClientTest { @Test public void confirmPatchFormat() { AAISingleTransactionClient singleTransaction = spy(new AAISingleTransactionClient(AAIVersion.LATEST)); - AAIPatchConverter mock = mock(AAIPatchConverter.class); + GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class); doReturn(mock).when(singleTransaction).getPatchConverter(); singleTransaction.update(uriA, "{}"); verify(mock, times(1)).convertPatchFormat(any()); diff --git a/common/src/test/java/org/onap/so/client/aai/AAITransactionalClientTest.java b/common/src/test/java/org/onap/so/client/aai/AAITransactionalClientTest.java index 621375882b..342e3b1aa4 100644 --- a/common/src/test/java/org/onap/so/client/aai/AAITransactionalClientTest.java +++ b/common/src/test/java/org/onap/so/client/aai/AAITransactionalClientTest.java @@ -43,6 +43,7 @@ import org.onap.aai.domain.yang.Relationship; import org.onap.so.client.aai.entities.uri.AAIResourceUri; import org.onap.so.client.aai.entities.uri.AAIUriFactory; import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl; +import org.onap.so.client.graphinventory.GraphInventoryPatchConverter; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; @@ -143,7 +144,7 @@ public class AAITransactionalClientTest { @Test public void confirmPatchFormat() { AAITransactionalClient client = spy(new AAITransactionalClient(AAIVersion.LATEST)); - AAIPatchConverter mock = mock(AAIPatchConverter.class); + GraphInventoryPatchConverter mock = mock(GraphInventoryPatchConverter.class); doReturn(mock).when(client).getPatchConverter(); client.update(uriA, "{}"); verify(mock, times(1)).convertPatchFormat(any()); diff --git a/common/src/test/java/org/onap/so/client/graphinventory/GraphInventoryPatchConverterTest.java b/common/src/test/java/org/onap/so/client/graphinventory/GraphInventoryPatchConverterTest.java new file mode 100644 index 0000000000..d24b3ff147 --- /dev/null +++ b/common/src/test/java/org/onap/so/client/graphinventory/GraphInventoryPatchConverterTest.java @@ -0,0 +1,114 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 AT&T Intellectual Property. 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.so.client.graphinventory; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.aai.domain.yang.GenericVnf; +import org.onap.so.client.aai.AAICommonObjectMapperProvider; +import org.onap.so.client.graphinventory.GraphInventoryPatchConverter; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + + +@RunWith(MockitoJUnitRunner.class) +public class GraphInventoryPatchConverterTest { + + private ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper(); + + @Test + public void convertObjectToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException { + GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter(); + GenericVnf vnf = new GenericVnf(); + vnf.setIpv4Loopback0Address(""); + String result = validator.marshallObjectToPatchFormat(vnf); + GenericVnf resultObj = mapper.readValue(result.toString(), GenericVnf.class); + assertTrue("expect object to become a String to prevent double marshalling", result instanceof String); + assertNull("expect null because of custom mapper", resultObj.getIpv4Loopback0Address()); + + } + + @Test + public void convertStringToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException { + GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter(); + String payload = "{\"ipv4-loopback0-address\":\"\"}"; + String result = validator.marshallObjectToPatchFormat(payload); + + assertEquals("expect no change", payload, result); + } + + @Test + public void convertStringToPatchFormatNull_Test() throws URISyntaxException, JsonParseException, JsonMappingException, IOException { + GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter(); + String payload = "{\"ipv4-loopback0-address\": null}"; + String result = validator.marshallObjectToPatchFormat(payload); + System.out.println(result); + assertEquals("expect no change", payload, result); + } + + @Test + public void convertMapToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException { + GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter(); + HashMap map = new HashMap<>(); + map.put("ipv4-loopback0-address", ""); + map.put("ipv4-loopback1-address", "192.168.1.1"); + String result = validator.marshallObjectToPatchFormat(map); + + assertEquals("expect string", "{\"ipv4-loopback1-address\":\"192.168.1.1\"}", result); + } + + @Test + public void hasComplexObjectTest() { + GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter(); + String hasNesting = "{ \"hello\" : \"world\", \"nested\" : { \"key\" : \"value\" } }"; + String noNesting = "{ \"hello\" : \"world\" }"; + String arrayCase = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"nestedComplex\" : [{\"key\" : \"value\"}]}"; + String empty = "{}"; + String arrayCaseSimpleOnly = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"]}"; + String relationshipListCaseNesting = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}], \"nested\" : { \"key\" : \"value\" }}"; + String relationshipListCase = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}]}"; + String nothing = ""; + + assertTrue("expect has nesting", validator.hasComplexObject(hasNesting)); + assertFalse("expect no nesting", validator.hasComplexObject(noNesting)); + assertTrue("expect has nesting", validator.hasComplexObject(arrayCase)); + assertFalse("expect no nesting", validator.hasComplexObject(empty)); + assertFalse("expect no nesting", validator.hasComplexObject(arrayCaseSimpleOnly)); + assertFalse("expect no nesting", validator.hasComplexObject(relationshipListCase)); + assertTrue("expect has nesting", validator.hasComplexObject(relationshipListCaseNesting)); + assertFalse("expect no nesting", validator.hasComplexObject(nothing)); + } + +} -- cgit 1.2.3-korg