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.hc2vpp.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.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 import static org.mockito.MockitoAnnotations.initMocks;
29 import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import io.fd.vpp.jvpp.VppCallbackException;
32 import io.fd.vpp.jvpp.core.dto.OneAddDelMapRequestItrRlocs;
33 import io.fd.vpp.jvpp.core.dto.OneAddDelMapRequestItrRlocsReply;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.ExecutionException;
36 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.TimeoutException;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Captor;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170808.itr.remote.locator.sets.grouping.ItrRemoteLocatorSet;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev170808.itr.remote.locator.sets.grouping.ItrRemoteLocatorSetBuilder;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 public class ItrRemoteLocatorSetCustomizerTest extends LispWriterCustomizerTest implements ByteDataTranslator {
48 private static final String VALID_NAME = "loc-set";
51 private ArgumentCaptor<OneAddDelMapRequestItrRlocs> requestCaptor;
53 private ItrRemoteLocatorSetCustomizer customizer;
54 private InstanceIdentifier<ItrRemoteLocatorSet> validId;
55 private ItrRemoteLocatorSet validData;
58 public void setUpTest() throws Exception {
60 customizer = new ItrRemoteLocatorSetCustomizer(api, lispStateCheckService);
61 validId = InstanceIdentifier.create(ItrRemoteLocatorSet.class);
62 validData = new ItrRemoteLocatorSetBuilder().setRemoteLocatorSetName(VALID_NAME).build();
66 public void writeCurrentAttributesSuccess() throws Exception {
68 customizer.writeCurrentAttributes(validId, validData, writeContext);
69 verifyWriteInvoked(true, VALID_NAME);
73 public void writeCurrentAttributesFailed() {
77 customizer.writeCurrentAttributes(validId, validData, writeContext);
78 } catch (WriteFailedException e) {
79 assertTrue(e.getCause() instanceof VppCallbackException);
80 verifyWriteInvoked(true, VALID_NAME);
84 fail("Test should have thrown exception");
88 public void updateCurrentAttributes() {
90 customizer.updateCurrentAttributes(validId, validData, validData, writeContext);
91 } catch (WriteFailedException e) {
92 assertTrue(e.getCause() instanceof UnsupportedOperationException);
96 fail("Test should have thrown exception");
100 public void deleteCurrentAttributesSuccess() throws Exception {
102 customizer.deleteCurrentAttributes(validId, validData, writeContext);
103 verifyWriteInvoked(false, VALID_NAME);
107 public void deleteCurrentAttributesFailed() throws Exception {
111 customizer.writeCurrentAttributes(validId, validData, writeContext);
112 } catch (WriteFailedException e) {
113 assertTrue(e.getCause() instanceof VppCallbackException);
114 verifyWriteInvoked(true, VALID_NAME);
118 fail("Test should have thrown exception");
121 private void onWriteSuccess() {
122 when(api.oneAddDelMapRequestItrRlocs(any(OneAddDelMapRequestItrRlocs.class)))
123 .thenReturn(CompletableFuture.completedFuture(new OneAddDelMapRequestItrRlocsReply()));
126 private void onWriteThrow() {
127 when(api.oneAddDelMapRequestItrRlocs(any(OneAddDelMapRequestItrRlocs.class)))
128 .thenReturn(new CompletableFuture<OneAddDelMapRequestItrRlocsReply>() {
130 public OneAddDelMapRequestItrRlocsReply get(final long l, final TimeUnit timeUnit)
131 throws InterruptedException, ExecutionException, TimeoutException {
132 throw new ExecutionException(new VppCallbackException("oneAddDelMapRequestItrRlocs",
133 "oneAddDelMapRequestItrRlocs failed", 1, -2));
138 private void verifyWriteInvoked(final boolean add, final String name) {
139 verify(api, times(1)).oneAddDelMapRequestItrRlocs(requestCaptor.capture());
141 final OneAddDelMapRequestItrRlocs request = requestCaptor.getValue();
142 assertNotNull(request);
143 assertEquals(booleanToByte(add), request.isAdd);
144 assertEquals(name, toString(request.locatorSetName));