summaryrefslogtreecommitdiffstats
path: root/lisp/lisp2vpp/src/test/java/io/fd/honeycomb/lisp/translate/write/ItrRemoteLocatorSetCustomizerTest.java
blob: 11393163ea540f74e33065c700ebbeaa602c4a64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package io.fd.honeycomb.lisp.translate.write;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
import io.fd.honeycomb.translate.write.WriteFailedException;
import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
import io.fd.vpp.jvpp.VppCallbackException;
import io.fd.vpp.jvpp.core.dto.LispAddDelMapRequestItrRlocs;
import io.fd.vpp.jvpp.core.dto.LispAddDelMapRequestItrRlocsReply;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.itr.remote.locator.sets.grouping.ItrRemoteLocatorSet;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.itr.remote.locator.sets.grouping.ItrRemoteLocatorSetBuilder;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

public class ItrRemoteLocatorSetCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {

    private static final String VALID_NAME = "loc-set";

    @Captor
    private ArgumentCaptor<LispAddDelMapRequestItrRlocs> requestCaptor;

    private ItrRemoteLocatorSetCustomizer customizer;
    private InstanceIdentifier<ItrRemoteLocatorSet> validId;
    private ItrRemoteLocatorSet validData;

    @Before
    public void setUp() throws Exception {
        initMocks(this);
        customizer = new ItrRemoteLocatorSetCustomizer(api);
        validId = InstanceIdentifier.create(ItrRemoteLocatorSet.class);
        validData = new ItrRemoteLocatorSetBuilder().setRemoteLocatorSetName(VALID_NAME).build();
    }

    @Test
    public void writeCurrentAttributesSuccess() throws Exception {
        onWriteSuccess();
        customizer.writeCurrentAttributes(validId, validData, writeContext);
        verifyWriteInvoked(true, VALID_NAME);
    }

    @Test
    public void writeCurrentAttributesFailed() {
        onWriteThrow();

        try {
            customizer.writeCurrentAttributes(validId, validData, writeContext);
        } catch (WriteFailedException e) {
            assertTrue(e.getCause() instanceof VppCallbackException);
            verifyWriteInvoked(true, VALID_NAME);
            return;
        }

        fail("Test should have thrown exception");
    }

    @Test
    public void updateCurrentAttributes() {
        try {
            customizer.updateCurrentAttributes(validId, validData, validData, writeContext);
        } catch (WriteFailedException e) {
            assertTrue(e.getCause() instanceof UnsupportedOperationException);
            return;
        }

        fail("Test should have thrown exception");
    }

    @Test
    public void deleteCurrentAttributesSuccess() throws Exception {
        onWriteSuccess();
        customizer.deleteCurrentAttributes(validId, validData, writeContext);
        verifyWriteInvoked(false, VALID_NAME);
    }

    @Test
    public void deleteCurrentAttributesFailed() throws Exception {
        onWriteThrow();

        try {
            customizer.writeCurrentAttributes(validId, validData, writeContext);
        } catch (WriteFailedException e) {
            assertTrue(e.getCause() instanceof VppCallbackException);
            verifyWriteInvoked(true, VALID_NAME);
            return;
        }

        fail("Test should have thrown exception");
    }

    private void onWriteSuccess() {
        when(api.lispAddDelMapRequestItrRlocs(any(LispAddDelMapRequestItrRlocs.class)))
                .thenReturn(CompletableFuture.completedFuture(new LispAddDelMapRequestItrRlocsReply()));
    }

    private void onWriteThrow() {
        when(api.lispAddDelMapRequestItrRlocs(any(LispAddDelMapRequestItrRlocs.class)))
                .thenReturn(new CompletableFuture<LispAddDelMapRequestItrRlocsReply>() {
                    @Override
                    public LispAddDelMapRequestItrRlocsReply get(final long l, final TimeUnit timeUnit)
                            throws InterruptedException, ExecutionException, TimeoutException {
                        throw new ExecutionException(new VppCallbackException("lispAddDelMapRequestItrRlocs", 1, -2));
                    }
                });
    }

    private void verifyWriteInvoked(final boolean add, final String name) {
        verify(api, times(1)).lispAddDelMapRequestItrRlocs(requestCaptor.capture());

        final LispAddDelMapRequestItrRlocs request = requestCaptor.getValue();
        assertNotNull(request);
        assertEquals(booleanToByte(add), request.isAdd);
        assertEquals(name, toString(request.locatorSetName));
    }
}