diff options
author | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2024-10-01 16:29:21 +0200 |
---|---|---|
committer | Fiete Ostkamp <fiete.ostkamp@telekom.de> | 2024-10-02 12:15:11 +0000 |
commit | c3eb0a6d5e4471d0bb2ef618cfafbe26546826d8 (patch) | |
tree | 3eb9facfe2a5cea56b988ccf85d805ae369324f2 /aai-core | |
parent | 962c7f9b84c2166c0b37f2580ec83fc9bdfc45d7 (diff) |
Extract entity conversion logic into separate class
Issue-ID: AAI-4008
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
Change-Id: Ic00d989ba97dd9e23d028d9e262ea2967d889e79
Diffstat (limited to 'aai-core')
4 files changed, 194 insertions, 48 deletions
diff --git a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToObject.java b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToObject.java index 186f2ee9..11a028bf 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToObject.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToObject.java @@ -24,6 +24,7 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.ws.rs.core.MultivaluedMap; @@ -37,27 +38,21 @@ import org.onap.aai.setup.SchemaVersion; /** * Given a URI this class returns an object, or series of nested objects * with their keys populated based off the values in the URI. - * + * * It populates the keys in the order they are listed in the model. */ public class URIToObject implements Parsable { + private final SchemaVersion version; + private final Loader loader; + private final Map<String, Introspector> relatedObjects; private Introspector topEntity = null; - private String topEntityName = null; - private String entityName = null; - private Introspector entity = null; - private Introspector previous = null; - private List<Object> parentList = null; - private SchemaVersion version = null; - private Loader loader = null; - private final HashMap<String, Introspector> relatedObjects; - /** * Instantiates a new URI to object. * @@ -77,7 +72,7 @@ public class URIToObject implements Parsable { this.version = loader.getVersion(); } - public URIToObject(Loader loader, URI uri, HashMap<String, Introspector> relatedObjects) + public URIToObject(Loader loader, URI uri, Map<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException { URIParser parser = new URIParser(loader, uri); diff --git a/aai-core/src/main/java/org/onap/aai/rest/notification/EntityConverter.java b/aai-core/src/main/java/org/onap/aai/rest/notification/EntityConverter.java new file mode 100644 index 00000000..e939f323 --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/rest/notification/EntityConverter.java @@ -0,0 +1,66 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2024 Deutsche Telekom. 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.aai.rest.notification; + +import java.util.List; + +import org.onap.aai.introspection.Introspector; +import org.onap.aai.introspection.exceptions.AAIUnmarshallingException; +import org.onap.aai.parsers.uri.URIToObject; + +import lombok.Value; + +@Value +public class EntityConverter { + + final URIToObject parser; + + public Introspector convert(Introspector obj) throws AAIUnmarshallingException { + List<Object> parentList = parser.getParentList(); + parentList.clear(); + + if (!parser.getTopEntity().equals(parser.getEntity())) { + Introspector child = obj; + if (!parser.getLoader().getVersion().equals(obj.getVersion())) { + String json = obj.marshal(false); + child = parser.getLoader().unmarshal(parser.getEntity().getName(), json); + } + + // wrap the child object in its parents + parentList.add(child.getUnderlyingObject()); + } + + final Introspector eventObject; + if (parser.getTopEntity().equals(parser.getEntity())) { + // take the top level parent object passed in + eventObject = obj; + } else { + // take the wrapped child objects (ogres are like onions) + eventObject = parser.getTopEntity(); + } + + return eventObject; + } + + public String getTopEntityName() { + return parser.getTopEntityName(); + } +} diff --git a/aai-core/src/main/java/org/onap/aai/rest/notification/UEBNotification.java b/aai-core/src/main/java/org/onap/aai/rest/notification/UEBNotification.java index 83a446b2..a113cf14 100644 --- a/aai-core/src/main/java/org/onap/aai/rest/notification/UEBNotification.java +++ b/aai-core/src/main/java/org/onap/aai/rest/notification/UEBNotification.java @@ -101,8 +101,8 @@ public class UEBNotification { String action = getAction(status); try { + EntityConverter entityConverter = new EntityConverter(new URIToObject(currentVersionLoader, uri, relatedObjects)); Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header"); - URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects); basePath = formatBasePath(basePath); @@ -111,48 +111,14 @@ public class UEBNotification { eventHeader.setValue("entity-link", entityLink); eventHeader.setValue("action", action); eventHeader.setValue("entity-type", obj.getDbName()); - eventHeader.setValue("top-entity-type", parser.getTopEntityName()); + eventHeader.setValue("top-entity-type", entityConverter.getTopEntityName()); eventHeader.setValue("source-name", sourceOfTruth); eventHeader.setValue("version", notificationVersion.toString()); eventHeader.setValue("id", transactionId); - List<Object> parentList = parser.getParentList(); - parentList.clear(); - if (!parser.getTopEntity().equals(parser.getEntity())) { - Introspector child = obj; - if (!parser.getLoader().getVersion().equals(obj.getVersion())) { - String json = obj.marshal(false); - child = parser.getLoader().unmarshal(parser.getEntity().getName(), json); - } + Introspector eventObject = entityConverter.convert(obj); - // wrap the child object in its parents - parentList.add(child.getUnderlyingObject()); - } - - final Introspector eventObject; - - // convert to most resent version - if (!parser.getLoader().getVersion().equals(currentVersionLoader.getVersion())) { - String json = ""; - if (parser.getTopEntity().equals(parser.getEntity())) { - // convert the parent object passed in - json = obj.marshal(false); - eventObject = currentVersionLoader.unmarshal(obj.getName(), json); - } else { - // convert the object created in the parser - json = parser.getTopEntity().marshal(false); - eventObject = currentVersionLoader.unmarshal(parser.getTopEntity().getName(), json); - } - } else { - if (parser.getTopEntity().equals(parser.getEntity())) { - // take the top level parent object passed in - eventObject = obj; - } else { - // take the wrapped child objects (ogres are like onions) - eventObject = parser.getTopEntity(); - } - } final NotificationEvent event = new NotificationEvent(currentVersionLoader, eventHeader, eventObject, transactionId, sourceOfTruth); events.put(uri.toString(), event); diff --git a/aai-core/src/test/java/org/onap/aai/rest/notification/EntityConverterTest.java b/aai-core/src/test/java/org/onap/aai/rest/notification/EntityConverterTest.java new file mode 100644 index 00000000..cbfedef7 --- /dev/null +++ b/aai-core/src/test/java/org/onap/aai/rest/notification/EntityConverterTest.java @@ -0,0 +1,119 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2024 Deutsche Telekom. 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.aai.rest.notification; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; + + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aai.introspection.Introspector; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.exceptions.AAIUnmarshallingException; +import org.onap.aai.parsers.uri.URIToObject; +import org.onap.aai.setup.SchemaVersion; + +public class EntityConverterTest { + + @Mock URIToObject parser; + @Mock Introspector introspector; + @Mock Loader loader; + @Mock List<Object> parentList; + + EntityConverter entityConverter; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + entityConverter = new EntityConverter(parser); + } + + @Test + public void testConvert_topEntitySameAsEntity() throws AAIUnmarshallingException { + when(parser.getParentList()).thenReturn(parentList); + when(parser.getTopEntity()).thenReturn(introspector); + when(parser.getEntity()).thenReturn(introspector); + + Introspector result = entityConverter.convert(introspector); + + assertEquals(introspector, result); + verify(parser.getParentList()).clear(); + } + + @Test + public void testConvert_topEntityDifferentFromEntity_withVersionMismatch() throws AAIUnmarshallingException { + Introspector topEntity = mock(Introspector.class); + Introspector childEntity = mock(Introspector.class); + String json = "{}"; + + when(parser.getParentList()).thenReturn(parentList); + when(parser.getTopEntity()).thenReturn(topEntity); + when(parser.getEntity()).thenReturn(childEntity); + when(childEntity.getName()).thenReturn("smth"); + when(parser.getLoader()).thenReturn(loader); + when(introspector.getVersion()).thenReturn(new SchemaVersion("v1")); + when(loader.getVersion()).thenReturn(new SchemaVersion("v2")); + when(introspector.marshal(false)).thenReturn(json); + when(loader.unmarshal(anyString(), eq(json))).thenReturn(childEntity); + + Introspector result = entityConverter.convert(introspector); + + assertEquals(topEntity, result); + } + + @Test + public void testConvert_topEntityDifferentFromEntity_withoutVersionMismatch() throws AAIUnmarshallingException { + Introspector topEntity = mock(Introspector.class); + Introspector childEntity = mock(Introspector.class); + + when(parser.getParentList()).thenReturn(parentList); + when(parser.getTopEntity()).thenReturn(topEntity); + when(parser.getEntity()).thenReturn(childEntity); + when(parser.getLoader()).thenReturn(loader); + when(introspector.getVersion()).thenReturn(new SchemaVersion("v1")); + when(loader.getVersion()).thenReturn(new SchemaVersion("v1")); + + Introspector result = entityConverter.convert(introspector); + + assertEquals(topEntity, result); + verify(parentList).add(any()); + } + + @Test + public void testGetTopEntityName() { + String topEntityName = "TopEntity"; + when(parser.getTopEntityName()).thenReturn(topEntityName); + + String result = entityConverter.getTopEntityName(); + + assertEquals(topEntityName, result); + } +} |