New Package dealing with device rollback
[transportpce.git] / renderer / src / test / java / org / opendaylight / transportpce / renderer / provisiondevice / transaction / history / TransactionHistoryTest.java
1 /*
2  * Copyright © 2024 Smartoptics and others.  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.renderer.provisiondevice.transaction.history;
10
11 import java.util.List;
12 import org.junit.Assert;
13 import org.junit.jupiter.api.Test;
14 import org.mockito.InOrder;
15 import org.mockito.Mockito;
16 import org.opendaylight.transportpce.renderer.provisiondevice.transaction.DeviceInterface;
17 import org.opendaylight.transportpce.renderer.provisiondevice.transaction.Transaction;
18 import org.opendaylight.transportpce.renderer.provisiondevice.transaction.delete.Delete;
19
20 class TransactionHistoryTest {
21
22     @Test
23     void add() {
24         Transaction transaction = Mockito.mock(Transaction.class);
25         History history = new TransactionHistory();
26
27         Assert.assertTrue(history.add(transaction));
28     }
29
30     @Test
31     void testDuplicateTransactionIsIgnored() {
32
33         Transaction t1 = new DeviceInterface("ROADM-A", "DEG1");
34         Transaction t2 = new DeviceInterface("ROADM-A", "DEG1");
35
36         History history = new TransactionHistory();
37
38         history.add(t1);
39         Assert.assertFalse(history.add(t2));
40     }
41
42     @Test
43     void testAddCollectionOfUniqueTransactions() {
44         Transaction t1 = new DeviceInterface("ROADM-A", "DEG1");
45         Transaction t2 = new DeviceInterface("ROADM-A", "DEG2");
46
47         List<Transaction> transactions = List.of(t1, t2);
48
49         History history = new TransactionHistory();
50
51         Assert.assertTrue(history.add(transactions));
52     }
53
54     @Test
55     void testAddCollectionOfDuplicateTransactions() {
56         Transaction t1 = new DeviceInterface("ROADM-A", "DEG1");
57         Transaction t2 = new DeviceInterface("ROADM-A", "DEG1");
58
59         List<Transaction> transactions = List.of(t1, t2);
60
61         History history = new TransactionHistory();
62
63         Assert.assertFalse(history.add(transactions));
64     }
65
66     @Test
67     void testAddUniqueStringOfInterfaceIds() {
68         String nodeId = "ROADM-A";
69         String[] interfaces = new String[]{"DEG1", "DEG2"};
70
71         History history = new TransactionHistory();
72
73         Assert.assertTrue(history.addInterfaces(nodeId, interfaces));
74     }
75
76     @Test
77     void testAddDuplicateStringOfInterfaceIds() {
78         String nodeId = "ROADM-A";
79         String[] interfaces = new String[]{"DEG1", "DEG1"};
80
81         History history = new TransactionHistory();
82
83         Assert.assertTrue(history.addInterfaces(nodeId, interfaces));
84
85     }
86
87     @Test
88     void testAddDuplicateListOfInterfaceIds() {
89         String nodeId = "ROADM-A";
90         List<String> interfaces = List.of("DEG1", "DEG1");
91
92         History history = new TransactionHistory();
93
94         Assert.assertTrue(history.addInterfaces(nodeId, interfaces));
95
96     }
97
98     @Test
99     void rollbackOneInterface() {
100
101         String nodeId = "ROADM-A";
102         List<String> interfaces = List.of("DEG1", "DEG1");
103
104         History history = new TransactionHistory();
105         history.addInterfaces(nodeId, interfaces);
106
107         Delete delete = Mockito.mock(Delete.class);
108         Mockito.when(delete.deleteInterface("ROADM-A", "DEG1")).thenReturn(true);
109
110         Assert.assertTrue(history.rollback(delete));
111
112         //Although the same interface was added twice, we only rollback once.
113         Mockito.verify(delete, Mockito.times(1))
114                 .deleteInterface("ROADM-A", "DEG1");
115     }
116
117     @Test
118     void rollbackTwoInterfacesInReverseOrderTheyWereAdded() {
119
120         String nodeId = "ROADM-A";
121
122         //Note DEG1 is added before DEG2
123         List<String> interfaces = List.of("DEG1", "DEG2");
124
125         History history = new TransactionHistory();
126         history.addInterfaces(nodeId, interfaces);
127
128         Delete delete = Mockito.mock(Delete.class);
129         Mockito.when(delete.deleteInterface("ROADM-A", "DEG1")).thenReturn(true);
130         Mockito.when(delete.deleteInterface("ROADM-A", "DEG2")).thenReturn(true);
131
132         Assert.assertTrue(history.rollback(delete));
133
134         //The rollback occurs in the reverse order.
135         // i.e. DEG2 before DEG1.
136         InOrder inOrder = Mockito.inOrder(delete);
137         inOrder.verify(delete, Mockito.times(1))
138                 .deleteInterface("ROADM-A", "DEG2");
139         inOrder.verify(delete, Mockito.times(1))
140                 .deleteInterface("ROADM-A", "DEG1");
141
142     }
143 }