Migrate common module to Aluminium Step 1
[transportpce.git] / common / src / test / java / org / opendaylight / transportpce / common / mapping / PortMappingImplTest.java
1 /*
2  * Copyright © 2020 Orange.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.transportpce.common.mapping;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18 import static org.opendaylight.transportpce.common.StringConstants.OPENROADM_DEVICE_VERSION_1_2_1;
19 import static org.opendaylight.transportpce.common.StringConstants.OPENROADM_DEVICE_VERSION_2_2_1;
20
21 import java.util.concurrent.ExecutionException;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.api.WriteTransaction;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.transportpce.common.DataStoreContext;
28 import org.opendaylight.transportpce.common.DataStoreContextImpl;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.Network;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.Nodes;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.NodesBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.NodesKey;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.Mapping;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.MappingBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.MappingKey;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.NodeInfo;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.NodeInfoBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40
41 public class PortMappingImplTest {
42
43     DataBroker dataBroker = null;
44     private PortMappingVersion221 portMappingVersion221;
45     private PortMappingVersion121 portMappingVersion121;
46     private PortMapping portMapping;
47
48     @Before
49     public void setUp() throws Exception {
50         DataStoreContext dataStoreContext = new DataStoreContextImpl();
51         dataBroker = dataStoreContext.getDataBroker();
52         portMappingVersion221 = mock(PortMappingVersion221.class);
53         portMappingVersion121 = mock(PortMappingVersion121.class);
54         portMapping = new PortMappingImpl(dataBroker, portMappingVersion221, portMappingVersion121);
55     }
56
57     @Test
58     public void createMappingDataTest() {
59         //test create mapping version 1
60         when(portMappingVersion121.createMappingData("node")).thenReturn(true);
61         assertTrue(portMapping.createMappingData("node", OPENROADM_DEVICE_VERSION_1_2_1));
62
63         //test create mapping version 2
64         when(portMappingVersion221.createMappingData("node")).thenReturn(true);
65         assertTrue(portMapping.createMappingData("node", OPENROADM_DEVICE_VERSION_2_2_1));
66
67         //test create mapping version with wrong value
68         assertFalse(portMapping.createMappingData("node", "test"));
69     }
70
71
72     @Test
73     public void updateMappingTest() throws ExecutionException, InterruptedException {
74         Mapping mapping = new MappingBuilder().setLogicalConnectionPoint("logicalConnectionPoint")
75                 .setPortDirection("1").setConnectionMapLcp("1").setPartnerLcp("1")
76                 .setPortQual("1").setSupportingCircuitPackName("1").setSupportingOms("1")
77                 .setSupportingOts("1").setSupportingPort("1").build();
78         InstanceIdentifier<Mapping> portMappingIID = InstanceIdentifier.builder(Network.class)
79                 .child(Nodes.class, new NodesKey("node"))
80                 .child(Mapping.class, new MappingKey("logicalConnectionPoint"))
81                 .build();
82         InstanceIdentifier<NodeInfo> nodeInfoIID = InstanceIdentifier.builder(Network.class).child(Nodes.class,
83                 new NodesKey("node")).child(NodeInfo.class).build();
84         final NodeInfo nodeInfo = new NodeInfoBuilder().setOpenroadmVersion(NodeInfo.OpenroadmVersion._221).build();
85         final NodeInfo nodeInfo2 = new NodeInfoBuilder().setOpenroadmVersion(NodeInfo.OpenroadmVersion._121).build();
86         Nodes nodes = new NodesBuilder().setNodeId("node").setNodeInfo(nodeInfo).build();
87         InstanceIdentifier<Nodes> nodeIID = InstanceIdentifier.builder(Network.class).child(Nodes.class,
88                 new NodesKey("node")).build();
89         //create node with portmapping and nodeifno version 2
90         WriteTransaction wr = dataBroker.newWriteOnlyTransaction();
91         wr.merge(LogicalDatastoreType.CONFIGURATION, nodeIID, nodes);
92         wr.merge(LogicalDatastoreType.CONFIGURATION, portMappingIID, mapping);
93         wr.merge(LogicalDatastoreType.CONFIGURATION, nodeInfoIID, nodeInfo);
94         wr.commit().get();
95         //test update port mapping version 2
96         when(portMappingVersion221.updateMapping("node", mapping)).thenReturn(true);
97         assertTrue("Update sould be ok", portMapping.updateMapping("node", mapping));
98
99         //replace node nodefino version 1 instead of version 2
100         WriteTransaction wr2 = dataBroker.newWriteOnlyTransaction();
101         wr2.merge(LogicalDatastoreType.CONFIGURATION, nodeInfoIID, nodeInfo2);
102         wr2.commit().get();
103
104         //test update portmapping version 1
105         when(portMappingVersion121.updateMapping("node", mapping)).thenReturn(true);
106         assertTrue(portMapping.updateMapping("node", mapping));
107
108         //test get node that exists
109         assertNotNull(portMapping.getNode("node"));
110
111         //test get node that doesn't exist
112         assertNull(portMapping.getNode("node2"));
113
114         //test get portmapping for existing node
115         assertEquals(portMapping
116                 .getMapping("node", "logicalConnectionPoint"), mapping);
117
118         //test delete portmapping for existing node
119         portMapping.deleteMappingData("node");
120
121         //test get portmapping that was deleted above and doesn't exist anymore
122         assertNull(portMapping.getMapping("node", "logicalConnectionPoint"));
123
124     }
125
126 }