Move TopologyDataUtils from tapi to test-common 55/94555/7
authormanuedelf <emmanuelle.delfour@orange.com>
Thu, 10 Dec 2020 14:46:35 +0000 (15:46 +0100)
committermanuedelf <emmanuelle.delfour@orange.com>
Sat, 9 Jan 2021 14:41:35 +0000 (15:41 +0100)
Move TopologyDataUtils test class from tapi to test-common in order to
be able to reuse it in other maven modules. As this class is now in
src/main/java, fix spotbugs issues.

JIRA: TRNSPRTPCE-230
Signed-off-by: manuedelf <emmanuelle.delfour@orange.com>
Change-Id: I29e987bff1c55ce3432c86728b4898f7ce1e38f9

tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/ConvertORTopoObjectToTapiTooObjectTest.java
tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplExceptionTest.java
tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplTest.java
tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TapiTopologyDataUtils.java [new file with mode: 0644]
test-common/src/main/java/org/opendaylight/transportpce/test/utils/TopologyDataUtils.java [moved from tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TopologyDataUtils.java with 78% similarity]

index b5d981e922bbac5ca6d67ccf26f8fadf916c0bcb..abe843129ed345df73aa7a7c9d36ed146f26fc7d 100644 (file)
@@ -32,8 +32,9 @@ import org.junit.Test;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.InstanceIdentifiers;
-import org.opendaylight.transportpce.tapi.utils.TopologyDataUtils;
+import org.opendaylight.transportpce.tapi.utils.TapiTopologyDataUtils;
 import org.opendaylight.transportpce.test.AbstractTest;
+import org.opendaylight.transportpce.test.utils.TopologyDataUtils;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.TerminationPoint1;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmTpType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
@@ -92,10 +93,11 @@ public class ConvertORTopoObjectToTapiTooObjectTest extends AbstractTest {
     @BeforeClass
     public static void setUp() throws InterruptedException, ExecutionException {
         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
-            TopologyDataUtils.OPENROADM_TOPOLOGY_FILE, InstanceIdentifiers.OVERLAY_NETWORK_II);
+                TapiTopologyDataUtils.OPENROADM_TOPOLOGY_FILE, InstanceIdentifiers.OVERLAY_NETWORK_II);
         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
-            TopologyDataUtils.OTN_TOPOLOGY_FILE, InstanceIdentifiers.OTN_NETWORK_II);
-        TopologyDataUtils.writePortmappingFromFileToDatastore(getDataStoreContextUtil());
+                TapiTopologyDataUtils.OTN_TOPOLOGY_FILE, InstanceIdentifiers.OTN_NETWORK_II);
+        TopologyDataUtils.writePortmappingFromFileToDatastore(getDataStoreContextUtil(),
+                TapiTopologyDataUtils.PORTMAPPING_FILE);
 
         KeyedInstanceIdentifier<Node, NodeKey> muxAIID = InstanceIdentifier.create(Networks.class)
             .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network
index 3d7ca13ae37476bb70bf1b4ba20619dd7a0a6ab3..4801ac6a3e15857092545da2558a458eb4e08f05 100644 (file)
@@ -21,7 +21,7 @@ import org.mockito.Mockito;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.ReadTransaction;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
-import org.opendaylight.transportpce.tapi.utils.TopologyDataUtils;
+import org.opendaylight.transportpce.tapi.utils.TapiTopologyDataUtils;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInput;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsOutput;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.get.topology.details.output.Topology;
@@ -37,7 +37,7 @@ public class TapiTopologyImplExceptionTest {
         when(dataBroker.newReadOnlyTransaction())
             .thenReturn(new ReadTransactionMock());
 
-        GetTopologyDetailsInput input = TopologyDataUtils.buildGetTopologyDetailsInput(TopologyUtils.T0_MULTILAYER);
+        GetTopologyDetailsInput input = TapiTopologyDataUtils.buildGetTopologyDetailsInput(TopologyUtils.T0_MULTILAYER);
         TapiTopologyImpl tapiTopoImpl = new TapiTopologyImpl(dataBroker);
         ListenableFuture<RpcResult<GetTopologyDetailsOutput>> result = tapiTopoImpl.getTopologyDetails(input);
         RpcResult<GetTopologyDetailsOutput> rpcResult = result.get();
index 30a5da12169142be9275400dcdbdef38458f46f1..be1cdf348423801d3830aa2fbc0204ef26b89737 100644 (file)
@@ -28,8 +28,9 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.transportpce.common.InstanceIdentifiers;
-import org.opendaylight.transportpce.tapi.utils.TopologyDataUtils;
+import org.opendaylight.transportpce.tapi.utils.TapiTopologyDataUtils;
 import org.opendaylight.transportpce.test.AbstractTest;
+import org.opendaylight.transportpce.test.utils.TopologyDataUtils;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.AdministrativeState;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.CapacityUnit;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.ForwardingDirection;
@@ -64,17 +65,18 @@ public class TapiTopologyImplTest extends AbstractTest {
         executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
         endSignal = new CountDownLatch(1);
         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
-            TopologyDataUtils.OPENROADM_TOPOLOGY_FILE, InstanceIdentifiers.OVERLAY_NETWORK_II);
+                TapiTopologyDataUtils.OPENROADM_TOPOLOGY_FILE, InstanceIdentifiers.OVERLAY_NETWORK_II);
         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
-            TopologyDataUtils.OTN_TOPOLOGY_FILE, InstanceIdentifiers.OTN_NETWORK_II);
-        TopologyDataUtils.writePortmappingFromFileToDatastore(getDataStoreContextUtil());
+                TapiTopologyDataUtils.OTN_TOPOLOGY_FILE, InstanceIdentifiers.OTN_NETWORK_II);
+        TopologyDataUtils.writePortmappingFromFileToDatastore(getDataStoreContextUtil(),
+                TapiTopologyDataUtils.PORTMAPPING_FILE);
         LOG.info("setup done");
     }
 
     @Test
     public void getTopologyDetailsForTransponder100GTopologyWhenSuccessful()
             throws ExecutionException, InterruptedException {
-        GetTopologyDetailsInput input = TopologyDataUtils.buildGetTopologyDetailsInput(TopologyUtils.TPDR_100G);
+        GetTopologyDetailsInput input = TapiTopologyDataUtils.buildGetTopologyDetailsInput(TopologyUtils.TPDR_100G);
         TapiTopologyImpl tapiTopoImpl = new TapiTopologyImpl(getDataBroker());
         ListenableFuture<RpcResult<GetTopologyDetailsOutput>> result = tapiTopoImpl.getTopologyDetails(input);
         result.addListener(new Runnable() {
@@ -119,7 +121,7 @@ public class TapiTopologyImplTest extends AbstractTest {
     @Test
     public void getTopologyDetailsForOtnTopologyWithOtnLinksWhenSuccessful()
         throws ExecutionException, InterruptedException {
-        GetTopologyDetailsInput input = TopologyDataUtils.buildGetTopologyDetailsInput(TopologyUtils.T0_MULTILAYER);
+        GetTopologyDetailsInput input = TapiTopologyDataUtils.buildGetTopologyDetailsInput(TopologyUtils.T0_MULTILAYER);
         TapiTopologyImpl tapiTopoImpl = new TapiTopologyImpl(getDataBroker());
         ListenableFuture<RpcResult<GetTopologyDetailsOutput>> result = tapiTopoImpl.getTopologyDetails(input);
         result.addListener(new Runnable() {
diff --git a/tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TapiTopologyDataUtils.java b/tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TapiTopologyDataUtils.java
new file mode 100644 (file)
index 0000000..2973185
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright © 2020 Orange, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.transportpce.tapi.utils;
+
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInput;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInputBuilder;
+
+public final class TapiTopologyDataUtils {
+    public static final String OPENROADM_TOPOLOGY_FILE = "src/test/resources/openroadm-topology.xml";
+    public static final String OTN_TOPOLOGY_FILE = "src/test/resources/otn-topology.xml";
+    public static final String PORTMAPPING_FILE = "src/test/resources/portmapping.xml";
+
+    public static GetTopologyDetailsInput buildGetTopologyDetailsInput(String topoName) {
+        GetTopologyDetailsInputBuilder builtInput = new GetTopologyDetailsInputBuilder();
+        builtInput.setTopologyIdOrName(topoName);
+        return builtInput.build();
+    }
+
+    private TapiTopologyDataUtils() {
+    }
+
+}
similarity index 78%
rename from tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TopologyDataUtils.java
rename to test-common/src/main/java/org/opendaylight/transportpce/test/utils/TopologyDataUtils.java
index 0f560f3f742657a2754305036367e3952a933ce9..a5f3220f6e077f1d9bed9b6fc713209d5db5eaa8 100644 (file)
@@ -5,12 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.transportpce.tapi.utils;
+package org.opendaylight.transportpce.test.utils;
 
 import com.google.common.util.concurrent.FluentFuture;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
@@ -22,8 +22,6 @@ import org.opendaylight.transportpce.test.DataStoreContext;
 import org.opendaylight.transportpce.test.converter.XMLDataObjectConverter;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.Network;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
-import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInput;
-import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInputBuilder;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -33,15 +31,6 @@ import org.slf4j.LoggerFactory;
 public final class TopologyDataUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(TopologyDataUtils.class);
-    public static final String OPENROADM_TOPOLOGY_FILE = "src/test/resources/openroadm-topology.xml";
-    public static final String OTN_TOPOLOGY_FILE = "src/test/resources/otn-topology.xml";
-    public static final String PORTMAPPING_FILE = "src/test/resources/portmapping.xml";
-
-    public static GetTopologyDetailsInput buildGetTopologyDetailsInput(String topoName) {
-        GetTopologyDetailsInputBuilder builtInput = new GetTopologyDetailsInputBuilder();
-        builtInput.setTopologyIdOrName(topoName);
-        return builtInput.build();
-    }
 
     public static <T> void writeTopologyFromFileToDatastore(DataStoreContext dataStoreContextUtil, String file,
         InstanceIdentifier ii) throws InterruptedException, ExecutionException {
@@ -49,9 +38,7 @@ public final class TopologyDataUtils {
         File topoFile = new File(file);
         if (topoFile.exists()) {
             String fileName = topoFile.getName();
-            InputStream targetStream;
-            try {
-                targetStream = new FileInputStream(topoFile);
+            try (InputStream targetStream = new FileInputStream(topoFile)) {
                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = XMLDataObjectConverter
                         .createWithDataStoreUtil(dataStoreContextUtil).transformIntoNormalizedNode(targetStream);
                 if (!transformIntoNormalizedNode.isPresent()) {
@@ -65,12 +52,15 @@ public final class TopologyDataUtils {
                 } else {
                     networks = (Networks) dataObject.get();
                 }
-            } catch (FileNotFoundException e) {
-                LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
+            } catch (IOException e) {
+                LOG.error("An error occured while reading file {}", file, e);
             }
         } else {
             LOG.error("xml file {} not found at {}", topoFile.getName(), topoFile.getAbsolutePath());
         }
+        if (networks == null) {
+            throw new IllegalStateException("Network is null cannot write it to datastore");
+        }
         FluentFuture<? extends CommitInfo> commitFuture = writeTransaction(dataStoreContextUtil.getDataBroker(), ii,
                 networks.nonnullNetwork().values().stream().findFirst().get());
         commitFuture.get();
@@ -85,15 +75,13 @@ public final class TopologyDataUtils {
         return transaction.commit();
     }
 
-    public static void writePortmappingFromFileToDatastore(DataStoreContext dataStoreContextUtil)
+    public static void writePortmappingFromFileToDatastore(DataStoreContext dataStoreContextUtil, String file)
         throws InterruptedException, ExecutionException {
         Network result = null;
-        File portmappingFile = new File(PORTMAPPING_FILE);
+        File portmappingFile = new File(file);
         if (portmappingFile.exists()) {
             String fileName = portmappingFile.getName();
-            InputStream targetStream;
-            try {
-                targetStream = new FileInputStream(portmappingFile);
+            try (InputStream targetStream = new FileInputStream(portmappingFile)) {
                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
                     .transformIntoNormalizedNode(targetStream);
@@ -108,8 +96,8 @@ public final class TopologyDataUtils {
                 } else {
                     result = (Network) dataObject.get();
                 }
-            } catch (FileNotFoundException e) {
-                LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
+            } catch (IOException e) {
+                LOG.error("An error occured while reading file {}", file, e);
             }
         } else {
             LOG.error("xml file {} not found at {}", portmappingFile.getName(), portmappingFile.getAbsolutePath());