1bb695cc016e708a8605cf9a244d3ae562b171e4
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / write / LocatorSetCustomizerTest.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 com.google.common.collect.ImmutableList;
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.nio.charset.StandardCharsets;
33 import java.util.Arrays;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.ExecutionException;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.locator.sets.grouping.LocatorSets;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.locator.sets.grouping.locator.sets.LocatorSet;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.locator.sets.grouping.locator.sets.LocatorSetBuilder;
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.InterfaceBuilder;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.openvpp.jvpp.core.dto.LispAddDelLocatorSet;
45 import org.openvpp.jvpp.core.dto.LispAddDelLocatorSetReply;
46 import org.openvpp.jvpp.core.dto.LispLocatorSetDetails;
47 import org.openvpp.jvpp.core.dto.LispLocatorSetDetailsReplyDump;
48
49 public class LocatorSetCustomizerTest extends WriterCustomizerTest {
50
51     private NamingContext locatorSetContext;
52
53     @Override
54     public void setUp() {
55         locatorSetContext = new NamingContext("locator-set", "instance");
56     }
57
58     @Test(expected = NullPointerException.class)
59     public void testWriteCurrentAttributesNullData() throws WriteFailedException {
60         new LocatorSetCustomizer(api, locatorSetContext).writeCurrentAttributes(null, null, null);
61     }
62
63     @Test(expected = NullPointerException.class)
64     public void testWriteCurrentAttributesBadData() throws WriteFailedException {
65         new LocatorSetCustomizer(api, locatorSetContext)
66                 .writeCurrentAttributes(null, mock(LocatorSet.class), null);
67     }
68
69     @Test
70     public void testWriteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
71
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         LispAddDelLocatorSetReply fakeReply = new LispAddDelLocatorSetReply();
84
85         CompletableFuture<LispAddDelLocatorSetReply> completeFuture = new CompletableFuture<>();
86         completeFuture.complete(fakeReply);
87
88         when(api.lispAddDelLocatorSet(any(LispAddDelLocatorSet.class))).thenReturn(completeFuture);
89         when(writeContext.readAfter(validId)).thenReturn(Optional.of(locatorSet));
90
91         final LispLocatorSetDetailsReplyDump reply = new LispLocatorSetDetailsReplyDump();
92         LispLocatorSetDetails details = new LispLocatorSetDetails();
93         details.lsName = "Locator".getBytes(StandardCharsets.UTF_8);
94         reply.lispLocatorSetDetails = ImmutableList.of(details);
95
96         cache.put(io.fd.honeycomb.lisp.translate.read.LocatorSetCustomizer.LOCATOR_SETS_CACHE_ID, reply);
97
98         new LocatorSetCustomizer(api, locatorSetContext).writeCurrentAttributes(validId, locatorSet, writeContext);
99
100         verify(api, times(1)).lispAddDelLocatorSet(locatorSetCaptor.capture());
101
102         LispAddDelLocatorSet request = locatorSetCaptor.getValue();
103
104         assertNotNull(request);
105         assertEquals("Locator", new String(request.locatorSetName));
106         assertEquals(1, request.isAdd);
107     }
108
109     @Test(expected = UnsupportedOperationException.class)
110     public void testUpdateCurrentAttributes() throws WriteFailedException {
111         new LocatorSetCustomizer(api, locatorSetContext).updateCurrentAttributes(null, null, null, null);
112     }
113
114     @Test(expected = NullPointerException.class)
115     public void testDeleteCurrentAttributesNullData() throws WriteFailedException {
116         new LocatorSetCustomizer(api, locatorSetContext).deleteCurrentAttributes(null, null, null);
117     }
118
119     @Test(expected = NullPointerException.class)
120     public void testDeleteCurrentAttributesBadData() throws WriteFailedException {
121         new LocatorSetCustomizer(api, locatorSetContext)
122                 .deleteCurrentAttributes(null, mock(LocatorSet.class), null);
123     }
124
125     @Test
126     public void testDeleteCurrentAttributes() throws InterruptedException, ExecutionException, WriteFailedException {
127
128         LocatorSet locatorSet = new LocatorSetBuilder()
129                 .setName("Locator")
130                 .build();
131
132         ArgumentCaptor<LispAddDelLocatorSet> locatorSetCaptor = ArgumentCaptor.forClass(LispAddDelLocatorSet.class);
133
134         LispAddDelLocatorSetReply fakeReply = new LispAddDelLocatorSetReply();
135
136         CompletableFuture<LispAddDelLocatorSetReply> completeFuture = new CompletableFuture<>();
137         completeFuture.complete(fakeReply);
138
139         when(api.lispAddDelLocatorSet(any(LispAddDelLocatorSet.class))).thenReturn(completeFuture);
140
141         new LocatorSetCustomizer(api, locatorSetContext).deleteCurrentAttributes(null, locatorSet, writeContext);
142
143         verify(api, times(1)).lispAddDelLocatorSet(locatorSetCaptor.capture());
144
145         LispAddDelLocatorSet request = locatorSetCaptor.getValue();
146
147         assertNotNull(request);
148         assertEquals("Locator", new String(request.locatorSetName));
149         assertEquals(0, request.isAdd);
150     }
151 }