30d33a51bce7a2264bfeed5f8984fa0ed5b36df8
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / write / LocatorSetCustomizerTest.java
1 /*
2  * Copyright (c) 2016 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.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import com.google.common.base.Optional;
30 import com.google.common.collect.ImmutableList;
31 import io.fd.honeycomb.translate.vpp.util.NamingContext;
32 import io.fd.honeycomb.translate.write.WriteFailedException;
33 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
34 import io.fd.vpp.jvpp.core.dto.LispAddDelLocatorSet;
35 import io.fd.vpp.jvpp.core.dto.LispAddDelLocatorSetReply;
36 import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetails;
37 import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetailsReplyDump;
38 import java.nio.charset.StandardCharsets;
39 import java.util.Arrays;
40 import java.util.concurrent.ExecutionException;
41 import org.junit.Test;
42 import org.mockito.ArgumentCaptor;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.LocatorSets;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSet;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.InterfaceBuilder;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49
50 public class LocatorSetCustomizerTest extends WriterCustomizerTest {
51
52     private LocatorSetCustomizer customizer;
53
54     @Override
55     public void setUp() {
56         customizer = new LocatorSetCustomizer(api, new NamingContext("locator-set", "locator-set-context"));
57     }
58
59     @Test(expected = NullPointerException.class)
60     public void testWriteCurrentAttributesNullData() throws WriteFailedException {
61         customizer.writeCurrentAttributes(null, null, writeContext);
62     }
63
64     @Test(expected = NullPointerException.class)
65     public void testWriteCurrentAttributesBadData() throws WriteFailedException {
66         customizer.writeCurrentAttributes(null, mock(LocatorSet.class), writeContext);
67     }
68
69     @Test
70     public void testWriteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
71         noMappingDefined(mappingContext, "Locator", "locator-set-context");
72         LocatorSet locatorSet = new LocatorSetBuilder()
73                 .setName("Locator")
74                 .setInterface(Arrays.asList(new InterfaceBuilder().build()))
75                 .build();
76
77         InstanceIdentifier<LocatorSet> validId =
78                 InstanceIdentifier.create(LocatorSets.class).child(LocatorSet.class, new LocatorSetKey("Locator"));
79
80
81         ArgumentCaptor<LispAddDelLocatorSet> locatorSetCaptor = ArgumentCaptor.forClass(LispAddDelLocatorSet.class);
82
83         when(api.lispAddDelLocatorSet(any(LispAddDelLocatorSet.class)))
84                 .thenReturn(future(new LispAddDelLocatorSetReply()));
85         when(writeContext.readAfter(validId)).thenReturn(Optional.of(locatorSet));
86
87         final LispLocatorSetDetailsReplyDump reply = new LispLocatorSetDetailsReplyDump();
88         LispLocatorSetDetails details = new LispLocatorSetDetails();
89         details.lsName = "Locator".getBytes(StandardCharsets.UTF_8);
90         reply.lispLocatorSetDetails = ImmutableList.of(details);
91
92         customizer.writeCurrentAttributes(validId, locatorSet, writeContext);
93
94         verify(api, times(1)).lispAddDelLocatorSet(locatorSetCaptor.capture());
95
96         LispAddDelLocatorSet request = locatorSetCaptor.getValue();
97
98         assertNotNull(request);
99         assertEquals("Locator", new String(request.locatorSetName));
100         assertEquals(1, request.isAdd);
101     }
102
103     @Test
104     public void testUpdateCurrentAttributes() throws WriteFailedException {
105         final InstanceIdentifier<LocatorSet> identifier = InstanceIdentifier.create(LocatorSet.class);
106         try {
107             customizer
108                     .updateCurrentAttributes(identifier, mock(LocatorSet.class), mock(LocatorSet.class), writeContext);
109         } catch (WriteFailedException e) {
110             assertTrue(e.getCause() instanceof UnsupportedOperationException);
111             assertEquals(identifier, e.getFailedId());
112             return;
113         }
114         fail("Test should have failed");
115     }
116
117     @Test(expected = NullPointerException.class)
118     public void testDeleteCurrentAttributesNullData() throws WriteFailedException {
119         customizer.deleteCurrentAttributes(null, null, writeContext);
120     }
121
122     @Test(expected = NullPointerException.class)
123     public void testDeleteCurrentAttributesBadData() throws WriteFailedException {
124         customizer.deleteCurrentAttributes(null, mock(LocatorSet.class), writeContext);
125     }
126
127     @Test
128     public void testDeleteCurrentAttributes() throws InterruptedException, ExecutionException, WriteFailedException {
129         LocatorSet locatorSet = new LocatorSetBuilder()
130                 .setName("Locator")
131                 .build();
132
133         ArgumentCaptor<LispAddDelLocatorSet> locatorSetCaptor = ArgumentCaptor.forClass(LispAddDelLocatorSet.class);
134
135         when(api.lispAddDelLocatorSet(any(LispAddDelLocatorSet.class)))
136                 .thenReturn(future(new LispAddDelLocatorSetReply()));
137
138         customizer.deleteCurrentAttributes(null, locatorSet, writeContext);
139
140         verify(api, times(1)).lispAddDelLocatorSet(locatorSetCaptor.capture());
141
142         LispAddDelLocatorSet request = locatorSetCaptor.getValue();
143
144         assertNotNull(request);
145         assertEquals("Locator", new String(request.locatorSetName));
146         assertEquals(0, request.isAdd);
147     }
148 }