2 * Copyright © 2024 Smartoptics and others. All rights reserved.
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
9 package org.opendaylight.transportpce.renderer.provisiondevice.transaction.history;
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;
20 class TransactionHistoryTest {
24 Transaction transaction = Mockito.mock(Transaction.class);
25 History history = new TransactionHistory();
27 Assert.assertTrue(history.add(transaction));
31 void testDuplicateTransactionIsIgnored() {
33 Transaction t1 = new DeviceInterface("ROADM-A", "DEG1");
34 Transaction t2 = new DeviceInterface("ROADM-A", "DEG1");
36 History history = new TransactionHistory();
39 Assert.assertFalse(history.add(t2));
43 void testAddCollectionOfUniqueTransactions() {
44 Transaction t1 = new DeviceInterface("ROADM-A", "DEG1");
45 Transaction t2 = new DeviceInterface("ROADM-A", "DEG2");
47 List<Transaction> transactions = List.of(t1, t2);
49 History history = new TransactionHistory();
51 Assert.assertTrue(history.add(transactions));
55 void testAddCollectionOfDuplicateTransactions() {
56 Transaction t1 = new DeviceInterface("ROADM-A", "DEG1");
57 Transaction t2 = new DeviceInterface("ROADM-A", "DEG1");
59 List<Transaction> transactions = List.of(t1, t2);
61 History history = new TransactionHistory();
63 Assert.assertFalse(history.add(transactions));
67 void testAddUniqueStringOfInterfaceIds() {
68 String nodeId = "ROADM-A";
69 String[] interfaces = new String[]{"DEG1", "DEG2"};
71 History history = new TransactionHistory();
73 Assert.assertTrue(history.addInterfaces(nodeId, interfaces));
77 void testAddDuplicateStringOfInterfaceIds() {
78 String nodeId = "ROADM-A";
79 String[] interfaces = new String[]{"DEG1", "DEG1"};
81 History history = new TransactionHistory();
83 Assert.assertTrue(history.addInterfaces(nodeId, interfaces));
88 void testAddDuplicateListOfInterfaceIds() {
89 String nodeId = "ROADM-A";
90 List<String> interfaces = List.of("DEG1", "DEG1");
92 History history = new TransactionHistory();
94 Assert.assertTrue(history.addInterfaces(nodeId, interfaces));
99 void rollbackOneInterface() {
101 String nodeId = "ROADM-A";
102 List<String> interfaces = List.of("DEG1", "DEG1");
104 History history = new TransactionHistory();
105 history.addInterfaces(nodeId, interfaces);
107 Delete delete = Mockito.mock(Delete.class);
108 Mockito.when(delete.deleteInterface("ROADM-A", "DEG1")).thenReturn(true);
110 Assert.assertTrue(history.rollback(delete));
112 //Although the same interface was added twice, we only rollback once.
113 Mockito.verify(delete, Mockito.times(1))
114 .deleteInterface("ROADM-A", "DEG1");
118 void rollbackTwoInterfacesInReverseOrderTheyWereAdded() {
120 String nodeId = "ROADM-A";
122 //Note DEG1 is added before DEG2
123 List<String> interfaces = List.of("DEG1", "DEG2");
125 History history = new TransactionHistory();
126 history.addInterfaces(nodeId, interfaces);
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);
132 Assert.assertTrue(history.rollback(delete));
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");