diff options
author | talig <talig@amdocs.com> | 2018-10-25 10:08:32 +0300 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2018-10-25 10:52:04 +0000 |
commit | 803926af358815abfe9fd27e63fcb0bd40b25784 (patch) | |
tree | 71aaf38201857850a1674711ca6896024f76b2a5 /openecomp-be/lib | |
parent | 6e5e2e19ed43ed4f4b23b58a5c78f2350fe64d19 (diff) |
Add UT for zusammen adaptor
Change-Id: I80720b3c322b55ae39b363f3042c9f0efb5e1510
Issue-ID: SDC-1822
Signed-off-by: talig <talig@amdocs.com>
Diffstat (limited to 'openecomp-be/lib')
8 files changed, 426 insertions, 48 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-api/src/main/java/org/openecomp/core/zusammen/api/ZusammenAdaptor.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-api/src/main/java/org/openecomp/core/zusammen/api/ZusammenAdaptor.java index b7acb06de5..32630df115 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-api/src/main/java/org/openecomp/core/zusammen/api/ZusammenAdaptor.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-api/src/main/java/org/openecomp/core/zusammen/api/ZusammenAdaptor.java @@ -1,3 +1,19 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + package org.openecomp.core.zusammen.api; import com.amdocs.zusammen.adaptor.inbound.api.types.item.*; @@ -23,9 +39,6 @@ public interface ZusammenAdaptor { void updateItem(SessionContext context, Id itemId, Info info); - // TODO: 4/4/2017 remove this workaround when versionId will be recieved from UI - Optional<ItemVersion> getFirstVersion(SessionContext context, Id itemId); - Collection<ItemVersion> listPublicVersions(SessionContext context, Id itemId); ItemVersion getPublicVersion(SessionContext context, Id itemId, Id versionId); diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/pom.xml b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/pom.xml index 7f389cfb8e..5aeaee972a 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/pom.xml +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/pom.xml @@ -67,5 +67,15 @@ <artifactId>openecomp-nosqldb-core</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/ZusammenConnector.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/ZusammenConnector.java index 09851afcfe..7932902dad 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/ZusammenConnector.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/ZusammenConnector.java @@ -1,3 +1,19 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + package org.openecomp.core.zusammen.db; import com.amdocs.zusammen.adaptor.inbound.api.types.item.*; @@ -71,10 +87,10 @@ public interface ZusammenConnector { Id elementId); Element saveElement(SessionContext context, ElementContext elementContext, - ZusammenElement element, String message); + Element element, String message); void resolveElementConflict(SessionContext context, ElementContext elementContext, - ZusammenElement element, Resolution resolution); + Element element, Resolution resolution); void resetVersionHistory(SessionContext context, Id itemId, Id versionId, String changeRef); } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/impl/ZusammenConnectorImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/impl/ZusammenConnectorImpl.java index 3a2854d081..474b7303ab 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/impl/ZusammenConnectorImpl.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/impl/ZusammenConnectorImpl.java @@ -19,21 +19,30 @@ import com.amdocs.zusammen.adaptor.inbound.api.health.HealthAdaptorFactory; import com.amdocs.zusammen.adaptor.inbound.api.item.ElementAdaptorFactory; import com.amdocs.zusammen.adaptor.inbound.api.item.ItemAdaptorFactory; import com.amdocs.zusammen.adaptor.inbound.api.item.ItemVersionAdaptorFactory; -import com.amdocs.zusammen.adaptor.inbound.api.types.item.*; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementConflict; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ItemVersionConflict; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.MergeResult; import com.amdocs.zusammen.commons.health.data.HealthInfo; import com.amdocs.zusammen.datatypes.Id; import com.amdocs.zusammen.datatypes.SessionContext; import com.amdocs.zusammen.datatypes.Space; -import com.amdocs.zusammen.datatypes.item.*; +import com.amdocs.zusammen.datatypes.item.ElementContext; +import com.amdocs.zusammen.datatypes.item.Info; +import com.amdocs.zusammen.datatypes.item.Item; +import com.amdocs.zusammen.datatypes.item.ItemVersion; +import com.amdocs.zusammen.datatypes.item.ItemVersionData; +import com.amdocs.zusammen.datatypes.item.ItemVersionStatus; +import com.amdocs.zusammen.datatypes.item.Resolution; import com.amdocs.zusammen.datatypes.itemversion.ItemVersionRevisions; import com.amdocs.zusammen.datatypes.itemversion.Tag; import com.amdocs.zusammen.datatypes.response.Response; +import java.util.Collection; import org.openecomp.core.zusammen.db.ZusammenConnector; import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer; import org.openecomp.sdc.common.errors.SdcRuntimeException; -import java.util.Collection; - public class ZusammenConnectorImpl implements ZusammenConnector { private static final String GET_ELEMENT_ERR_MSG = @@ -355,8 +364,7 @@ public class ZusammenConnectorImpl implements ZusammenConnector { } @Override - public Element saveElement(SessionContext context, ElementContext elementContext, - ZusammenElement element, String message) { + public Element saveElement(SessionContext context, ElementContext elementContext, Element element, String message) { Response<Element> response = elementAdaptorFactory.createInterface(context) .save(context, elementContext, element, message); if (!response.isSuccessful()) { @@ -369,9 +377,8 @@ public class ZusammenConnectorImpl implements ZusammenConnector { } @Override - public void resolveElementConflict(SessionContext context, ElementContext elementContext, - ZusammenElement element, - Resolution resolution) { + public void resolveElementConflict(SessionContext context, ElementContext elementContext, Element element, + Resolution resolution) { Response<Void> response = elementAdaptorFactory.createInterface(context) .resolveConflict(context, elementContext, element, resolution); if (!response.isSuccessful()) { diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorImpl.java index 186ad7f6cb..4efa2486a2 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorImpl.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorImpl.java @@ -15,22 +15,31 @@ */ package org.openecomp.core.zusammen.impl; -import com.amdocs.zusammen.adaptor.inbound.api.types.item.*; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementConflict; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ItemVersionConflict; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; import com.amdocs.zusammen.commons.health.data.HealthInfo; import com.amdocs.zusammen.datatypes.Id; import com.amdocs.zusammen.datatypes.SessionContext; -import com.amdocs.zusammen.datatypes.item.*; +import com.amdocs.zusammen.datatypes.item.Action; +import com.amdocs.zusammen.datatypes.item.ElementContext; +import com.amdocs.zusammen.datatypes.item.Info; +import com.amdocs.zusammen.datatypes.item.Item; +import com.amdocs.zusammen.datatypes.item.ItemVersion; +import com.amdocs.zusammen.datatypes.item.ItemVersionData; +import com.amdocs.zusammen.datatypes.item.ItemVersionStatus; +import com.amdocs.zusammen.datatypes.item.Resolution; import com.amdocs.zusammen.datatypes.itemversion.ItemVersionRevisions; import com.amdocs.zusammen.datatypes.itemversion.Tag; -import org.openecomp.core.zusammen.api.ZusammenAdaptor; -import org.openecomp.core.zusammen.db.ZusammenConnector; - import java.util.ArrayList; import java.util.Collection; -import java.util.List; import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Collectors; +import org.openecomp.core.zusammen.api.ZusammenAdaptor; +import org.openecomp.core.zusammen.db.ZusammenConnector; public class ZusammenAdaptorImpl implements ZusammenAdaptor { @@ -145,6 +154,9 @@ public class ZusammenAdaptorImpl implements ZusammenAdaptor { if (element.getElementId() != null) { return; } + if (element.getInfo() == null || element.getInfo().getName() == null) { + throw new IllegalArgumentException("When saving element to zusammen - its Id or name must be supplied"); + } Optional<ElementInfo> elementInfo = getElementInfoByName(context, elementContext, parentElementId, element.getInfo().getName()); if (elementInfo.isPresent()) { @@ -190,19 +202,6 @@ public class ZusammenAdaptorImpl implements ZusammenAdaptor { } @Override - public Optional<ItemVersion> getFirstVersion(SessionContext context, Id itemId) { - Collection<ItemVersion> versions = connector.listPublicVersions(context, itemId); - if (versions == null || versions.isEmpty()) { - return Optional.empty(); - } - List<ItemVersion> itemVersions = new ArrayList<>(versions); - sortItemVersionListByModificationTimeDescOrder(itemVersions); - ItemVersion itemVersion = itemVersions.iterator().next(); - - return Optional.ofNullable(itemVersion); - } - - @Override public Collection<ItemVersion> listPublicVersions(SessionContext context, Id itemId) { return connector.listPublicVersions(context, itemId); } @@ -289,10 +288,4 @@ public class ZusammenAdaptorImpl implements ZusammenAdaptor { public String getVersion(SessionContext context) { return connector.getVersion(context); } - - private static void sortItemVersionListByModificationTimeDescOrder( - List<ItemVersion> itemVersions) { - itemVersions.sort((o1, o2) -> Integer.compare(o2.getId().getValue().length(), - o1.getId().getValue().length())); - } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/test/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorImplTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/test/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorImplTest.java new file mode 100644 index 0000000000..22398ab3f1 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/test/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorImplTest.java @@ -0,0 +1,316 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.core.zusammen.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; +import com.amdocs.zusammen.datatypes.Id; +import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.item.Action; +import com.amdocs.zusammen.datatypes.item.ElementContext; +import com.amdocs.zusammen.datatypes.item.Info; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.core.zusammen.db.ZusammenConnector; + +public class ZusammenAdaptorImplTest { + + private static final SessionContext CONTEXT = new SessionContext(); + private static final ElementContext ELEMENT_CONTEXT = new ElementContext(); + private static final Id ELEMENT_ID = new Id("elementId 0"); + private static final List<ElementInfo> ELEMENTS = Arrays.asList( + createElementInfo("elementId1", "element1"), + createElementInfo("elementId2", "element2"), + createElementInfo("elementId3", "element3")); + + @Mock + private ZusammenConnector connector; + @InjectMocks + private ZusammenAdaptorImpl zusammenAdaptor; + @Captor + private ArgumentCaptor<Element> savedElementArg; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void getEmptyWhenElementNameNotExist() { + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + + Optional<Element> element = + zusammenAdaptor.getElementByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "nonExistingName"); + + assertFalse(element.isPresent()); + } + + @Test + public void getEmptyInfoWhenElementNameNotExist() { + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + + Optional<ElementInfo> elementInfo = + zusammenAdaptor.getElementInfoByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "nonExistingName"); + + assertFalse(elementInfo.isPresent()); + } + + @Test + public void getElementWhenItsNameExist() { + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + ZusammenElement returnedElement = new ZusammenElement(); + doReturn(returnedElement).when(connector).getElement(CONTEXT, ELEMENT_CONTEXT, ELEMENTS.get(1).getId()); + + Optional<Element> element = zusammenAdaptor.getElementByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "element2"); + + assertTrue(element.isPresent()); + assertEquals(returnedElement, element.get()); + } + + @Test + public void getElementInfoWhenItsNameExist() { + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + + Optional<ElementInfo> elementInfo = + zusammenAdaptor.getElementInfoByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "element2"); + + assertTrue(elementInfo.isPresent()); + assertEquals(ELEMENTS.get(1), elementInfo.get()); + + } + + @Test + public void listElementsWhenTheirParentIdExist() { + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + + List<ZusammenElement> returnedElements = + Arrays.asList(new ZusammenElement(), new ZusammenElement(), new ZusammenElement()); + doReturn(returnedElements.get(0)).when(connector).getElement(CONTEXT, ELEMENT_CONTEXT, ELEMENTS.get(0).getId()); + doReturn(returnedElements.get(1)).when(connector).getElement(CONTEXT, ELEMENT_CONTEXT, ELEMENTS.get(1).getId()); + doReturn(returnedElements.get(2)).when(connector).getElement(CONTEXT, ELEMENT_CONTEXT, ELEMENTS.get(2).getId()); + + Collection<Element> elements = zusammenAdaptor.listElementData(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + + assertEquals(returnedElements, elements); + } + + @Test + public void getEmptyListWhenParentElementNameNotExist() { + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + + Collection<ElementInfo> elements = + zusammenAdaptor.listElementsByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "nonExistingName"); + + assertTrue(elements.isEmpty()); + } + + @Test + public void listElementsInfoWhenTheirParentElementNameExist() { + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + + List<ElementInfo> returnedElements = Arrays.asList(new ElementInfo(), new ElementInfo()); + doReturn(returnedElements).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENTS.get(1).getId()); + + Collection<ElementInfo> elements = + zusammenAdaptor.listElementsByName(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID, "element2"); + + assertEquals(returnedElements, elements); + } + + @Test(expected = IllegalArgumentException.class) + public void failWhenSavingElementWithoutIdNameOrAction() { + zusammenAdaptor.saveElement(CONTEXT, ELEMENT_CONTEXT, new ZusammenElement(), "Illegal element save"); + } + + @Test + public void saveElementAsRootWhenParentIdNotSupplied() { + String message = "Create new element tree"; + ZusammenElement element = new ZusammenElement(); + element.setAction(Action.CREATE); + + ZusammenElement subElement = new ZusammenElement(); + subElement.setAction(Action.CREATE); + element.addSubElement(subElement); + + testSaveElement(message, element); + + verify(connector).saveElement(CONTEXT, ELEMENT_CONTEXT, element, message); + } + + @Test + public void saveElementAsSubWhenParentIdSupplied() { + String message = "Create sub element"; + ZusammenElement element = new ZusammenElement(); + element.setAction(Action.IGNORE); + element.setElementId(ELEMENT_ID); + + ZusammenElement subElement = new ZusammenElement(); + subElement.setAction(Action.CREATE); + element.addSubElement(subElement); + + testSaveElement(message, element); + + verify(connector).saveElement(CONTEXT, ELEMENT_CONTEXT, element, message); + } + + @Test + public void saveElementWhenItsIdSupplied() { + String message = "Update element"; + ZusammenElement element = new ZusammenElement(); + element.setAction(Action.UPDATE); + element.setElementId(new Id("id")); + + testSaveElement(message, element); + + verify(connector).saveElement(CONTEXT, ELEMENT_CONTEXT, element, message); + } + + @Test + public void saveRootElementWhenItsNameSupplied() { + String message = "Update element"; + ZusammenElement element = new ZusammenElement(); + element.setAction(Action.UPDATE); + element.setInfo(ELEMENTS.get(1).getInfo()); + + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, null); + + testSaveElement(message, element); + + verify(connector).saveElement(eq(CONTEXT), eq(ELEMENT_CONTEXT), savedElementArg.capture(), eq(message)); + + Element savedElement = savedElementArg.getValue(); + assertEquals(element, savedElement); + assertNotNull(savedElement.getElementId()); + } + + @Test + public void saveElementWhenItsNameAndParentIdSupplied() { + String message = "Update existing element"; + ZusammenElement element = new ZusammenElement(); + element.setAction(Action.IGNORE); + element.setElementId(ELEMENT_ID); + + ZusammenElement existingSub = new ZusammenElement(); + existingSub.setAction(Action.UPDATE); + existingSub.setInfo(ELEMENTS.get(2).getInfo()); + element.addSubElement(existingSub); + + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + + testSaveElement(message, element); + + verify(connector).saveElement(eq(CONTEXT), eq(ELEMENT_CONTEXT), savedElementArg.capture(), eq(message)); + + Element savedElement = savedElementArg.getValue(); + assertEquals(element, savedElement); + + Element updated = savedElement.getSubElements().iterator().next(); + assertNotNull(updated.getElementId()); + assertEquals(Action.UPDATE, updated.getAction()); + } + + @Test + public void saveElementWithCreateActionInsteadOfUpdateWhenItDoesNotExist() { + String message = "Create new element"; + ZusammenElement element = new ZusammenElement(); + element.setAction(Action.IGNORE); + element.setElementId(ELEMENT_ID); + + ZusammenElement nonExistingSub = new ZusammenElement(); + nonExistingSub.setAction(Action.UPDATE); + Info info = new Info(); + info.setName("nonExistingName"); + nonExistingSub.setInfo(info); + + element.addSubElement(nonExistingSub); + + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + + testSaveElement(message, element); + + verify(connector).saveElement(eq(CONTEXT), eq(ELEMENT_CONTEXT), savedElementArg.capture(), eq(message)); + + Element savedElement = savedElementArg.getValue(); + assertEquals(element, savedElement); + + Element created = savedElement.getSubElements().iterator().next(); + assertNull(created.getElementId()); + assertEquals(Action.CREATE, created.getAction()); + } + + @Test + public void saveElementWithIgnoreActionWhenItExistAndActionNotSupplied() { + String message = "save existing element"; + ZusammenElement element = new ZusammenElement(); + element.setAction(Action.IGNORE); + element.setElementId(ELEMENT_ID); + + ZusammenElement existingSub = new ZusammenElement(); + existingSub.setInfo(ELEMENTS.get(2).getInfo()); + element.addSubElement(existingSub); + + doReturn(ELEMENTS).when(connector).listElements(CONTEXT, ELEMENT_CONTEXT, ELEMENT_ID); + + testSaveElement(message, element); + + verify(connector).saveElement(eq(CONTEXT), eq(ELEMENT_CONTEXT), savedElementArg.capture(), eq(message)); + + Element savedElement = savedElementArg.getValue(); + assertEquals(element, savedElement); + + Element ignored = savedElement.getSubElements().iterator().next(); + assertNotNull(ignored.getElementId()); + assertEquals(Action.IGNORE, ignored.getAction()); + } + + private void testSaveElement(String message, ZusammenElement element) { + ZusammenElement returnedElement = new ZusammenElement(); + doReturn(returnedElement).when(connector).saveElement(CONTEXT, ELEMENT_CONTEXT, element, message); + + Element saved = zusammenAdaptor.saveElement(CONTEXT, ELEMENT_CONTEXT, element, message); + + assertEquals(returnedElement, saved); + } + + private static ElementInfo createElementInfo(String id, String name) { + ElementInfo elementInfo = new ElementInfo(); + elementInfo.setId(new Id(id)); + Info info = new Info(); + info.setName(name); + elementInfo.setInfo(info); + return elementInfo; + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/dao/ServiceModelDaoFactoryTest.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/dao/ServiceModelDaoFactoryTest.java index 1648209371..0aed663250 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/dao/ServiceModelDaoFactoryTest.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/dao/ServiceModelDaoFactoryTest.java @@ -191,12 +191,6 @@ public class ServiceModelDaoFactoryTest { } @Override - public Optional<ItemVersion> getFirstVersion(SessionContext context, Id itemId) { - - return Optional.ofNullable(itemVersion); - } - - @Override public Collection<ItemVersion> listPublicVersions(SessionContext context, Id itemId) { return null; } diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionableEntityDaoZusammenImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionableEntityDaoZusammenImpl.java index 9e12d0dec9..f90ef79290 100644 --- a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionableEntityDaoZusammenImpl.java +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionableEntityDaoZusammenImpl.java @@ -1,17 +1,35 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + package org.openecomp.sdc.versioning.dao.impl; import com.amdocs.zusammen.datatypes.Id; import com.amdocs.zusammen.datatypes.SessionContext; import com.amdocs.zusammen.datatypes.item.ItemVersion; import com.amdocs.zusammen.datatypes.itemversion.Tag; +import java.util.Collection; +import java.util.Comparator; +import java.util.Optional; +import org.apache.commons.collections4.CollectionUtils; import org.openecomp.core.zusammen.api.ZusammenAdaptor; import org.openecomp.core.zusammen.api.ZusammenUtil; import org.openecomp.sdc.versioning.dao.VersionableEntityDao; import org.openecomp.sdc.versioning.dao.types.Version; import org.openecomp.sdc.versioning.types.VersionableEntityMetadata; -import java.util.Optional; - public class VersionableEntityDaoZusammenImpl implements VersionableEntityDao { private ZusammenAdaptor zusammenAdaptor; @@ -47,9 +65,20 @@ public class VersionableEntityDaoZusammenImpl implements VersionableEntityDao { // TODO: 3/19/2017 move to a common util private Id getItemVersionId(Id itemId, SessionContext context) { - Optional<ItemVersion> itemVersionOptional = zusammenAdaptor.getFirstVersion(context, itemId); + Optional<ItemVersion> itemVersionOptional = getFirstVersion(context, itemId); ItemVersion itemVersion = itemVersionOptional.orElseThrow(() -> new RuntimeException(String.format("No version was found for item %s.", itemId))); return itemVersion.getId(); } + + private Optional<ItemVersion> getFirstVersion(SessionContext context, Id itemId) { + Collection<ItemVersion> versions = zusammenAdaptor.listPublicVersions(context, itemId); + return CollectionUtils.isEmpty(versions) + ? Optional.empty() + : versions.stream().min(getVersionModificationTimeDescComparator()); + } + + private static Comparator<ItemVersion> getVersionModificationTimeDescComparator() { + return (o1, o2) -> Integer.compare(o2.getId().getValue().length(), o1.getId().getValue().length()); + } } |