2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.lisp.translate.write;
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;
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;
50 public class LocatorSetCustomizerTest extends WriterCustomizerTest {
52 private LocatorSetCustomizer customizer;
56 customizer = new LocatorSetCustomizer(api, new NamingContext("locator-set", "locator-set-context"));
59 @Test(expected = NullPointerException.class)
60 public void testWriteCurrentAttributesNullData() throws WriteFailedException {
61 customizer.writeCurrentAttributes(null, null, writeContext);
64 @Test(expected = NullPointerException.class)
65 public void testWriteCurrentAttributesBadData() throws WriteFailedException {
66 customizer.writeCurrentAttributes(null, mock(LocatorSet.class), writeContext);
70 public void testWriteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
71 noMappingDefined(mappingContext, "Locator", "locator-set-context");
72 LocatorSet locatorSet = new LocatorSetBuilder()
74 .setInterface(Arrays.asList(new InterfaceBuilder().build()))
77 InstanceIdentifier<LocatorSet> validId =
78 InstanceIdentifier.create(LocatorSets.class).child(LocatorSet.class, new LocatorSetKey("Locator"));
81 ArgumentCaptor<LispAddDelLocatorSet> locatorSetCaptor = ArgumentCaptor.forClass(LispAddDelLocatorSet.class);
83 when(api.lispAddDelLocatorSet(any(LispAddDelLocatorSet.class)))
84 .thenReturn(future(new LispAddDelLocatorSetReply()));
85 when(writeContext.readAfter(validId)).thenReturn(Optional.of(locatorSet));
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);
92 customizer.writeCurrentAttributes(validId, locatorSet, writeContext);
94 verify(api, times(1)).lispAddDelLocatorSet(locatorSetCaptor.capture());
96 LispAddDelLocatorSet request = locatorSetCaptor.getValue();
98 assertNotNull(request);
99 assertEquals("Locator", new String(request.locatorSetName));
100 assertEquals(1, request.isAdd);
104 public void testUpdateCurrentAttributes() throws WriteFailedException {
105 final InstanceIdentifier<LocatorSet> identifier = InstanceIdentifier.create(LocatorSet.class);
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());
114 fail("Test should have failed");
117 @Test(expected = NullPointerException.class)
118 public void testDeleteCurrentAttributesNullData() throws WriteFailedException {
119 customizer.deleteCurrentAttributes(null, null, writeContext);
122 @Test(expected = NullPointerException.class)
123 public void testDeleteCurrentAttributesBadData() throws WriteFailedException {
124 customizer.deleteCurrentAttributes(null, mock(LocatorSet.class), writeContext);
128 public void testDeleteCurrentAttributes() throws InterruptedException, ExecutionException, WriteFailedException {
129 LocatorSet locatorSet = new LocatorSetBuilder()
133 ArgumentCaptor<LispAddDelLocatorSet> locatorSetCaptor = ArgumentCaptor.forClass(LispAddDelLocatorSet.class);
135 when(api.lispAddDelLocatorSet(any(LispAddDelLocatorSet.class)))
136 .thenReturn(future(new LispAddDelLocatorSetReply()));
138 customizer.deleteCurrentAttributes(null, locatorSet, writeContext);
140 verify(api, times(1)).lispAddDelLocatorSet(locatorSetCaptor.capture());
142 LispAddDelLocatorSet request = locatorSetCaptor.getValue();
144 assertNotNull(request);
145 assertEquals("Locator", new String(request.locatorSetName));
146 assertEquals(0, request.isAdd);