From 90f7343d69fd5629f28eddae873ab3d2571ec46a Mon Sep 17 00:00:00 2001 From: manuedelf Date: Thu, 10 Dec 2020 15:46:35 +0100 Subject: [PATCH] Move TopologyDataUtils from tapi to test-common 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 Change-Id: I29e987bff1c55ce3432c86728b4898f7ce1e38f9 --- ...onvertORTopoObjectToTapiTooObjectTest.java | 10 +++-- .../TapiTopologyImplExceptionTest.java | 4 +- .../tapi/topology/TapiTopologyImplTest.java | 14 ++++--- .../tapi/utils/TapiTopologyDataUtils.java | 28 ++++++++++++++ .../test}/utils/TopologyDataUtils.java | 38 +++++++------------ 5 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TapiTopologyDataUtils.java rename {tapi/src/test/java/org/opendaylight/transportpce/tapi => test-common/src/main/java/org/opendaylight/transportpce/test}/utils/TopologyDataUtils.java (78%) diff --git a/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/ConvertORTopoObjectToTapiTooObjectTest.java b/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/ConvertORTopoObjectToTapiTooObjectTest.java index b5d981e92..abe843129 100644 --- a/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/ConvertORTopoObjectToTapiTooObjectTest.java +++ b/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/ConvertORTopoObjectToTapiTooObjectTest.java @@ -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 muxAIID = InstanceIdentifier.create(Networks.class) .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network diff --git a/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplExceptionTest.java b/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplExceptionTest.java index 3d7ca13ae..4801ac6a3 100644 --- a/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplExceptionTest.java +++ b/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplExceptionTest.java @@ -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> result = tapiTopoImpl.getTopologyDetails(input); RpcResult rpcResult = result.get(); diff --git a/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplTest.java b/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplTest.java index 30a5da121..be1cdf348 100644 --- a/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplTest.java +++ b/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplTest.java @@ -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> 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> 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 index 000000000..2973185fb --- /dev/null +++ b/tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TapiTopologyDataUtils.java @@ -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() { + } + +} diff --git a/tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TopologyDataUtils.java b/test-common/src/main/java/org/opendaylight/transportpce/test/utils/TopologyDataUtils.java 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 0f560f3f7..a5f3220f6 100644 --- a/tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TopologyDataUtils.java +++ b/test-common/src/main/java/org/opendaylight/transportpce/test/utils/TopologyDataUtils.java @@ -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 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> 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 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> 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()); -- 2.36.6