Restructure(package) DataJobs feature in NCMP Service 73/138073/6
authorleventecsanyi <levente.csanyi@est.tech>
Thu, 23 May 2024 14:40:13 +0000 (16:40 +0200)
committerleventecsanyi <levente.csanyi@est.tech>
Tue, 28 May 2024 12:07:05 +0000 (14:07 +0200)
   - restructured already existing classes
   - added testware for AlternateIdMatcher

Issue-ID: CPS 2240
Change-Id: I5691ed99627353903689cd2929163f77ed8c4eca
Signed-off-by: leventecsanyi <levente.csanyi@est.tech>
16 files changed:
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/DataJobService.java [moved from cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/DataJobService.java with 87% similarity]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/DataJobMetadata.java [moved from cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/datajob/DataJobMetadata.java with 94% similarity]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/DataJobReadRequest.java [moved from cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/datajob/DataJobReadRequest.java with 96% similarity]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/DataJobWriteRequest.java [moved from cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/datajob/DataJobWriteRequest.java with 96% similarity]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/ReadOperation.java [moved from cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/datajob/ReadOperation.java with 98% similarity]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/WriteOperation.java [moved from cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/datajob/WriteOperation.java with 97% similarity]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistence.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistenceImpl.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/exceptions/NoAlternateIdMatchFoundException.java [moved from cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/NoAlternateIdParentFoundException.java with 82% similarity]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobServiceImpl.java [moved from cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/DataJobServiceImpl.java with 84% similarity]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/AlternateIdMatcher.java [new file with mode: 0644]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/DataJobServiceImplSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistenceImplSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/utils/AlternateIdMatcherSpec.groovy [new file with mode: 0644]
integration-test/src/test/groovy/org/onap/cps/integration/base/CpsIntegrationSpecBase.groovy
integration-test/src/test/groovy/org/onap/cps/integration/performance/ncmp/CmHandleQueryByAlternateIdPerfTest.groovy

  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api;
+package org.onap.cps.ncmp.api.datajobs;
 
-import org.onap.cps.ncmp.api.models.datajob.DataJobMetadata;
-import org.onap.cps.ncmp.api.models.datajob.DataJobReadRequest;
-import org.onap.cps.ncmp.api.models.datajob.DataJobWriteRequest;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobReadRequest;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest;
 
 public interface DataJobService {
 
index 184b125..e230b3f 100644 (file)
@@ -129,16 +129,6 @@ public interface InventoryPersistence extends NcmpPersistence {
      */
     DataNode getCmHandleDataNodeByAlternateId(String alternateId);
 
-    /**
-     * Get data node that matches longest alternate id by removing elements (as defined by the separator string)
-     * from right to left.
-     *
-     * @param alternateId alternate ID
-     * @param separator   a string that separates each element from the next.
-     * @return data node
-     */
-    DataNode getCmHandleDataNodeByLongestMatchAlternateId(final String alternateId, final String separator);
-
     /**
      * Get collection of data nodes of given cm handles.
      *
index bf54fe5..c4cab31 100644 (file)
@@ -33,11 +33,9 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
 import org.onap.cps.api.CpsAnchorService;
 import org.onap.cps.api.CpsDataService;
 import org.onap.cps.api.CpsModuleService;
-import org.onap.cps.ncmp.api.impl.exception.NoAlternateIdParentFoundException;
 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
 import org.onap.cps.spi.FetchDescendantsOption;
@@ -181,19 +179,6 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv
         return dataNodes.iterator().next();
     }
 
-    @Override
-    public DataNode getCmHandleDataNodeByLongestMatchAlternateId(final String alternateId, final String separator) {
-        String bestMatch = alternateId;
-        while (StringUtils.isNotEmpty(bestMatch)) {
-            try {
-                return getCmHandleDataNodeByAlternateId(bestMatch);
-            } catch (final DataNodeNotFoundException ignored) {
-                bestMatch = getParentPath(bestMatch, separator);
-            }
-        }
-        throw new NoAlternateIdParentFoundException(alternateId);
-    }
-
     @Override
     public Collection<DataNode> getCmHandleDataNodes(final Collection<String> cmHandleIds) {
         final Collection<String> xpaths = new ArrayList<>(cmHandleIds.size());
@@ -221,9 +206,4 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv
     private String createCmHandlesJsonData(final List<YangModelCmHandle> yangModelCmHandles) {
         return "{\"cm-handles\":" + jsonObjectMapper.asJsonString(yangModelCmHandles) + "}";
     }
-
-    private static String getParentPath(final String path, final String separator) {
-        final int lastSeparatorIndex = path.lastIndexOf(separator);
-        return lastSeparatorIndex < 0 ? "" : path.substring(0, lastSeparatorIndex);
-    }
 }
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api.impl.exception;
+package org.onap.cps.ncmp.exceptions;
 
 import java.io.Serial;
+import org.onap.cps.ncmp.api.impl.exception.NcmpException;
 
-public class NoAlternateIdParentFoundException extends NcmpException {
+public class NoAlternateIdMatchFoundException extends NcmpException {
 
     @Serial
     private static final long serialVersionUID = -2412915490233422945L;
-    private static final String ALTERNATE_ID_NOT_FOUND = "No matching (parent) cm handle found using alternate ids";
+    private static final String ALTERNATE_ID_NOT_FOUND = "No matching cm handle found using alternate ids";
 
     /**
      * Constructor.
      *
      * @param cpsPath datanode cpsPath
      */
-    public NoAlternateIdParentFoundException(final String cpsPath) {
+    public NoAlternateIdMatchFoundException(final String cpsPath) {
         super(ALTERNATE_ID_NOT_FOUND, String.format("cannot find a datanode with alternate id %s", cpsPath));
     }
 }
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api.impl;
+package org.onap.cps.ncmp.impl.datajobs;
 
 import lombok.extern.slf4j.Slf4j;
-import org.onap.cps.ncmp.api.DataJobService;
-import org.onap.cps.ncmp.api.models.datajob.DataJobMetadata;
-import org.onap.cps.ncmp.api.models.datajob.DataJobReadRequest;
-import org.onap.cps.ncmp.api.models.datajob.DataJobWriteRequest;
+import org.onap.cps.ncmp.api.datajobs.DataJobService;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobReadRequest;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest;
 
 @Slf4j
 public class DataJobServiceImpl implements DataJobService {
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/AlternateIdMatcher.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/AlternateIdMatcher.java
new file mode 100644 (file)
index 0000000..8385f19
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2024 Nordix Foundation
+ *  ================================================================================
+ *  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.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.utils;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
+import org.onap.cps.ncmp.exceptions.NoAlternateIdMatchFoundException;
+import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
+import org.onap.cps.spi.model.DataNode;
+import org.springframework.stereotype.Service;
+
+@Slf4j
+@Service
+@RequiredArgsConstructor
+public class AlternateIdMatcher {
+
+    private final InventoryPersistence inventoryPersistence;
+
+    /**
+     * Get data node that matches longest alternate id by removing elements (as defined by the separator string)
+     * from right to left.
+     *
+     * @param alternateId alternate ID
+     * @param separator   a string that separates each element from the next.
+     * @return data node
+     */
+    public DataNode getCmHandleDataNodeByLongestMatchAlternateId(final String alternateId, final String separator) {
+        String bestMatch = alternateId;
+        while (StringUtils.isNotEmpty(bestMatch)) {
+            try {
+                return inventoryPersistence.getCmHandleDataNodeByAlternateId(bestMatch);
+            } catch (final DataNodeNotFoundException ignored) {
+                bestMatch = getParentPath(bestMatch, separator);
+            }
+        }
+        throw new NoAlternateIdMatchFoundException(alternateId);
+    }
+
+    private String getParentPath(final String path, final String separator) {
+        final int lastSeparatorIndex = path.lastIndexOf(separator);
+        return lastSeparatorIndex < 0 ? "" : path.substring(0, lastSeparatorIndex);
+    }
+}
index 4378764..bef0adc 100644 (file)
@@ -24,12 +24,13 @@ import ch.qos.logback.classic.Level
 import ch.qos.logback.classic.Logger
 import ch.qos.logback.classic.spi.ILoggingEvent
 import ch.qos.logback.core.read.ListAppender
+import org.onap.cps.ncmp.impl.datajobs.DataJobServiceImpl
 import org.slf4j.LoggerFactory
-import org.onap.cps.ncmp.api.models.datajob.DataJobReadRequest
-import org.onap.cps.ncmp.api.models.datajob.DataJobWriteRequest
-import org.onap.cps.ncmp.api.models.datajob.DataJobMetadata
-import org.onap.cps.ncmp.api.models.datajob.ReadOperation
-import org.onap.cps.ncmp.api.models.datajob.WriteOperation
+import org.onap.cps.ncmp.api.datajobs.models.DataJobReadRequest
+import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest
+import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata
+import org.onap.cps.ncmp.api.datajobs.models.ReadOperation
+import org.onap.cps.ncmp.api.datajobs.models.WriteOperation
 import spock.lang.Specification
 
 class DataJobServiceImplSpec extends Specification{
index 9907e9a..66fd7d8 100644 (file)
@@ -37,7 +37,6 @@ import org.onap.cps.api.CpsModuleService
 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
 import org.onap.cps.spi.CascadeDeleteAllowed
 import org.onap.cps.spi.FetchDescendantsOption
-import org.onap.cps.ncmp.api.impl.exception.NoAlternateIdParentFoundException
 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
 import org.onap.cps.spi.model.DataNode
 import org.onap.cps.spi.model.ModuleDefinition
@@ -303,41 +302,6 @@ class InventoryPersistenceImplSpec extends Specification {
             assert objectUnderTest.getCmHandleDataNodeByAlternateId('alternate id') == new DataNode()
     }
 
-    def 'Find cm handle parent data node using alternate ids'() {
-        given: 'cm handle in the registry with alternateId /a/b'
-            def matchingCpsPath = "/dmi-registry/cm-handles[@alternate-id='/a/b']"
-            mockCmHandleQueries.queryNcmpRegistryByCpsPath(matchingCpsPath, OMIT_DESCENDANTS) >> [new DataNode()]
-        and: 'no other cm handle'
-            mockCmHandleQueries.queryNcmpRegistryByCpsPath(*_) >> []
-        expect: 'querying for alternate id a matching result found'
-            assert objectUnderTest.getCmHandleDataNodeByLongestMatchAlternateId(alternateId, '/') != null
-        where: 'the following parameters are used'
-            scenario                              | alternateId
-            'exact match'                         | '/a/b'
-            'exact match with trailing separator' | '/a/b/'
-            'child match'                         | '/a/b/c'
-    }
-
-    def 'Find cm handle parent data node using alternate ids mismatches'() {
-        given: 'cm handle in the registry with alternateId'
-            def matchingCpsPath = "/dmi-registry/cm-handles[@alternate-id='${cpsPath}]"
-            mockCmHandleQueries.queryNcmpRegistryByCpsPath(matchingCpsPath, OMIT_DESCENDANTS) >> [new DataNode()]
-        and: 'no other cm handle'
-            mockCmHandleQueries.queryNcmpRegistryByCpsPath(*_) >> []
-        when: 'attempt to find alternateId'
-            objectUnderTest.getCmHandleDataNodeByLongestMatchAlternateId(alternateId, '/')
-        then: 'no alternate id found exception thrown'
-            def thrown = thrown(NoAlternateIdParentFoundException)
-        and: 'the exception has the relevant details from the error response'
-            assert thrown.message == 'No matching (parent) cm handle found using alternate ids'
-            assert thrown.details == 'cannot find a datanode with alternate id ' + alternateId
-        where: 'the following parameters are used'
-            scenario                              | alternateId | cpsPath
-            'no match for parent only'            | '/a'        | '/a/b'
-            'no match at all'                     | '/x/y/z'    | '/a/b'
-            'no match with trailing separator'    | '/c/d/'     | '/c/d'
-    }
-
     def 'Attempt to get non existing cm handle data node by alternate id'() {
         given: 'query service is invoked and returns empty collection of data nodes'
             mockCmHandleQueries.queryNcmpRegistryByCpsPath(*_) >> []
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/utils/AlternateIdMatcherSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/utils/AlternateIdMatcherSpec.groovy
new file mode 100644 (file)
index 0000000..720a7e7
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2024 Nordix Foundation
+ *  ================================================================================
+ *  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.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.utils
+
+import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
+import org.onap.cps.ncmp.exceptions.NoAlternateIdMatchFoundException
+import org.onap.cps.spi.exceptions.DataNodeNotFoundException
+import org.onap.cps.spi.model.DataNode
+import spock.lang.Specification
+
+class AlternateIdMatcherSpec extends Specification {
+
+    def mockInventoryPersistence = Mock(InventoryPersistence)
+    def objectUnderTest = new AlternateIdMatcher(mockInventoryPersistence)
+
+    def setup() {
+        given: 'cm handle in the registry with alternate id /a/b'
+            mockInventoryPersistence.getCmHandleDataNodeByAlternateId('/a/b') >> new DataNode()
+        and: 'no other cm handle'
+            mockInventoryPersistence.getCmHandleDataNodeByAlternateId(_) >> { throw new DataNodeNotFoundException('', '') }
+    }
+
+    def 'Finding longest alternate id matches.'() {
+        expect: 'querying for alternate id a matching result found'
+            assert objectUnderTest.getCmHandleDataNodeByLongestMatchAlternateId(targetAlternateId, '/') != null
+        where: 'the following parameters are used'
+            scenario                   | targetAlternateId
+            'exact match'              | '/a/b'
+            'parent match'             | '/a/b/c'
+            'grand parent match'       | '/a/b/c/d'
+            'trailing separator match' | '/a/b/'
+    }
+
+    def 'Attempt to find longest alternate id match without any matches.'() {
+        when: 'attempt to find alternateId'
+            objectUnderTest.getCmHandleDataNodeByLongestMatchAlternateId(targetAlternateId, '/')
+        then: 'no alternate id match found exception thrown'
+            def thrown = thrown(NoAlternateIdMatchFoundException)
+        and: 'the exception has the relevant details from the error response'
+            assert thrown.message == 'No matching cm handle found using alternate ids'
+            assert thrown.details == 'cannot find a datanode with alternate id ' + targetAlternateId
+        where: 'the following parameters are used'
+            scenario                   | targetAlternateId
+            'no match for parent only' | '/a'
+            'no match for other child' | '/a/c'
+            'no match at all'          | '/x/y'
+    }
+}
\ No newline at end of file
index 6952b2a..d1a6619 100644 (file)
@@ -46,6 +46,7 @@ import org.onap.cps.spi.exceptions.DataspaceNotFoundException
 import org.onap.cps.spi.model.DataNode
 import org.onap.cps.spi.repository.DataspaceRepository
 import org.onap.cps.spi.utils.SessionManager
+import org.onap.cps.ncmp.utils.AlternateIdMatcher
 import org.onap.cps.utils.JsonObjectMapper
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
@@ -114,6 +115,9 @@ abstract class CpsIntegrationSpecBase extends Specification {
     @Autowired
     InventoryPersistence inventoryPersistence
 
+    @Autowired
+    AlternateIdMatcher alternateIdMatcher
+
     MockWebServer mockDmiServer = null
     DmiDispatcher dmiDispatcher = new DmiDispatcher()
 
index 4f33d03..9504c9e 100644 (file)
@@ -22,7 +22,7 @@ package org.onap.cps.integration.performance.ncmp
 
 import org.onap.cps.integration.ResourceMeter
 import org.onap.cps.integration.performance.base.NcmpPerfTestBase
-import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
+import org.onap.cps.ncmp.utils.AlternateIdMatcher
 
 import java.util.stream.Collectors
 
@@ -32,10 +32,10 @@ import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
 
 class CmHandleQueryByAlternateIdPerfTest extends NcmpPerfTestBase {
 
-    InventoryPersistence objectUnderTest
+    AlternateIdMatcher objectUnderTest
     ResourceMeter resourceMeter = new ResourceMeter()
 
-    def setup() { objectUnderTest = inventoryPersistence }
+    def setup() { objectUnderTest = alternateIdMatcher }
 
     def 'Query cm handle by longest match alternate id'() {
         when: 'an alternate id as cps path query'