HONEYCOMB-145 - Utility Class Refactoring
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / write / InterfaceCustomizerTest.java
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.lisp.translate.write;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26
27 import com.google.common.base.Optional;
28 import io.fd.honeycomb.translate.v3po.util.ByteDataTranslator;
29 import io.fd.honeycomb.translate.v3po.util.NamingContext;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
32 import java.util.concurrent.ExecutionException;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Captor;
36 import org.mockito.Mockito;
37 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.Lisp;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.locator.sets.grouping.LocatorSets;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.locator.sets.grouping.locator.sets.LocatorSet;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.locator.sets.grouping.locator.sets.LocatorSetKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.locator.sets.grouping.locator.sets.locator.set.Interface;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.locator.sets.grouping.locator.sets.locator.set.InterfaceBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.locator.sets.grouping.locator.sets.locator.set.InterfaceKey;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.openvpp.jvpp.core.dto.LispAddDelLocator;
48 import org.openvpp.jvpp.core.dto.LispAddDelLocatorReply;
49 import org.openvpp.jvpp.core.future.FutureJVppCore;
50
51 public class InterfaceCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {
52
53     @Captor
54     private ArgumentCaptor<LispAddDelLocator> intfCaptor;
55
56     private NamingContext namingContext;
57     private InstanceIdentifier<Interface> id;
58     private Interface intf;
59     private InterfaceCustomizer customizer;
60
61     @Override
62     public void setUp() {
63         id = InstanceIdentifier.builder(Lisp.class)
64                 .child(LocatorSets.class)
65                 .child(LocatorSet.class, new LocatorSetKey("Locator"))
66                 .child(Interface.class, new InterfaceKey("Interface"))
67                 .build();
68
69         intf = new InterfaceBuilder()
70                 .setPriority((short) 1)
71                 .setWeight((short) 2)
72                 .build();
73
74         namingContext = new NamingContext("PREFIX", "INSTANCE");
75
76         customizer = new InterfaceCustomizer(api, namingContext);
77
78         when(mappingContext.read(Mockito.any()))
79                 .thenReturn(Optional.of((DataObject) new MappingBuilder().setIndex(5).setName("interface").build()));
80         when(api.lispAddDelLocator(any(LispAddDelLocator.class))).thenReturn(future(new LispAddDelLocatorReply()));
81     }
82
83     @Test(expected = NullPointerException.class)
84     public void testWriteCurrentAttributesNullData() throws WriteFailedException {
85         new InterfaceCustomizer(mock(FutureJVppCore.class), new NamingContext("PREFIX", "INSTANCE"))
86                 .writeCurrentAttributes(null, null, null);
87     }
88
89     @Test(expected = NullPointerException.class)
90     public void testWriteCurrentAttributesNullPriority() throws WriteFailedException {
91         Interface intf = mock(Interface.class);
92         when(intf.getWeight()).thenReturn((short) 1);
93         when(intf.getPriority()).thenReturn(null);
94
95         new InterfaceCustomizer(mock(FutureJVppCore.class), new NamingContext("PREFIX", "INSTANCE"))
96                 .writeCurrentAttributes(null, intf, null);
97     }
98
99     @Test(expected = NullPointerException.class)
100     public void testWriteCurrentAttributesNullWeight() throws WriteFailedException {
101         Interface intf = mock(Interface.class);
102         when(intf.getWeight()).thenReturn(null);
103         when(intf.getPriority()).thenReturn((short) 1);
104
105         new InterfaceCustomizer(mock(FutureJVppCore.class), new NamingContext("PREFIX", "INSTANCE"))
106                 .writeCurrentAttributes(null, intf, null);
107     }
108
109     @Test
110     public void testWriteCurrentAttributes() throws InterruptedException, ExecutionException, WriteFailedException {
111         customizer.writeCurrentAttributes(id, intf, writeContext);
112
113         verify(api, times(1)).lispAddDelLocator(intfCaptor.capture());
114
115         LispAddDelLocator request = intfCaptor.getValue();
116
117         assertNotNull(request);
118         assertEquals(1, request.isAdd);
119         assertEquals(2, request.weight);
120         assertEquals(1, request.priority);
121         assertEquals(5, request.swIfIndex);
122         assertEquals("Locator", toString(request.locatorSetName));
123     }
124
125     @Test(expected = UnsupportedOperationException.class)
126     public void testUpdateCurrentAttributes() throws WriteFailedException {
127         new InterfaceCustomizer(api, namingContext)
128                 .updateCurrentAttributes(null, null, null, null);
129     }
130
131     @Test(expected = NullPointerException.class)
132     public void testDeleteCurrentAttributesNullData() throws WriteFailedException {
133         new InterfaceCustomizer(api, namingContext)
134                 .deleteCurrentAttributes(null, null, null);
135     }
136
137     @Test(expected = NullPointerException.class)
138     public void testDeleteCurrentAttributesNullPriority() throws WriteFailedException {
139         Interface interf = mock(Interface.class);
140         when(interf.getWeight()).thenReturn((short) 1);
141         when(interf.getPriority()).thenReturn(null);
142
143         new InterfaceCustomizer(mock(FutureJVppCore.class), new NamingContext("PREFIX", "INSTANCE"))
144                 .deleteCurrentAttributes(null, interf, null);
145     }
146
147     @Test(expected = NullPointerException.class)
148     public void testDeleteCurrentAttributesNullWeight() throws WriteFailedException {
149         Interface interf = mock(Interface.class);
150         when(interf.getWeight()).thenReturn(null);
151         when(interf.getPriority()).thenReturn((short) 1);
152
153         new InterfaceCustomizer(mock(FutureJVppCore.class), new NamingContext("PREFIX", "INSTANCE"))
154                 .deleteCurrentAttributes(null, interf, null);
155     }
156
157     @Test
158     public void testDeleteCurrentAttributes() throws InterruptedException, ExecutionException, WriteFailedException {
159         customizer.deleteCurrentAttributes(id, intf, writeContext);
160
161         verify(api, times(1)).lispAddDelLocator(intfCaptor.capture());
162
163         LispAddDelLocator request = intfCaptor.getValue();
164
165         assertNotNull(request);
166         assertEquals(0, request.isAdd);
167         assertEquals(2, request.weight);
168         assertEquals(1, request.priority);
169         assertEquals(5, request.swIfIndex);
170         assertEquals("Locator", toString(request.locatorSetName));
171     }
172 }