summaryrefslogtreecommitdiffstats
path: root/lisp/lisp2vpp/src/test/java/io/fd/honeycomb/lisp/translate/read/ItrRemoteLocatorSetCustomizerTest.java
blob: 9b810b94d46a9b92d6fdfc68f94c68b279da080c (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.lisp.translate.read;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
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 io.fd.honeycomb.translate.read.ReadFailedException;
import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
import io.fd.honeycomb.vpp.test.read.ReaderCustomizerTest;
import io.fd.vpp.jvpp.VppCallbackException;
import io.fd.vpp.jvpp.core.dto.LispGetMapRequestItrRlocs;
import io.fd.vpp.jvpp.core.dto.LispGetMapRequestItrRlocsReply;
import java.nio.charset.StandardCharsets;
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.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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.lisp.feature.data.grouping.LispFeatureDataBuilder;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;


public class ItrRemoteLocatorSetCustomizerTest
        extends ReaderCustomizerTest<ItrRemoteLocatorSet, ItrRemoteLocatorSetBuilder> {

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

    private InstanceIdentifier<ItrRemoteLocatorSet> validId;
    private ItrRemoteLocatorSetBuilder builder;

    public ItrRemoteLocatorSetCustomizerTest() {
        super(ItrRemoteLocatorSet.class, LispFeatureDataBuilder.class);
    }

    @Before
    public void setUp() throws Exception {
        validId = InstanceIdentifier.create(ItrRemoteLocatorSet.class);
        builder = new ItrRemoteLocatorSetBuilder();
    }

    @Override
    protected ReaderCustomizer<ItrRemoteLocatorSet, ItrRemoteLocatorSetBuilder> initCustomizer() {
        return new ItrRemoteLocatorSetCustomizer(api);
    }

    @Test
    public void getBuilder() throws Exception {
        final ItrRemoteLocatorSetBuilder itrRemoteLocatorSetBuilder = getCustomizer().getBuilder(validId);

        assertNotNull(itrRemoteLocatorSetBuilder);
        assertNull(itrRemoteLocatorSetBuilder.getRemoteLocatorSetName());
    }

    @Test
    public void readCurrentAttributesSuccess() throws Exception {
        doReturnValidDataOnDump();

        getCustomizer().readCurrentAttributes(validId, builder, ctx);

        assertNotNull(builder);
        assertEquals(EXPECTED_LOCATOR_SET_NAME, builder.getRemoteLocatorSetName());
        verifyLispGetMapRequestItrRlocsInvokedOnce();
    }

    @Test
    public void readCurrentAttributesEmptyData() throws Exception {
        doReturnEmptyDataOnDump();
        getCustomizer().readCurrentAttributes(validId, builder, ctx);
        verifyInvalidDataCase(builder);
    }

    @Test
    public void readCurrentAttributesFailedCallHalted() {
        doThrowExceptionOnDump();
        try {
            getCustomizer().readCurrentAttributes(validId, builder, ctx);
        } catch (ReadFailedException e) {
            assertTrue(e.getCause() instanceof VppCallbackException);
            assertNotNull(builder);
            assertNull(builder.getRemoteLocatorSetName());

            verifyLispGetMapRequestItrRlocsInvokedOnce();
            return;
        }

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

    @Test
    public void merge() throws Exception {
        LispFeatureDataBuilder builder = new LispFeatureDataBuilder();
        ItrRemoteLocatorSet set = new ItrRemoteLocatorSetBuilder().setRemoteLocatorSetName("loc-set").build();
        getCustomizer().merge(builder, set);

        assertNotNull(builder);
        assertEquals(set, builder.getItrRemoteLocatorSet());
    }


    private void doReturnValidDataOnDump() {
        LispGetMapRequestItrRlocsReply reply = new LispGetMapRequestItrRlocsReply();
        reply.locatorSetName = EXPECTED_LOCATOR_SET_NAME.getBytes(StandardCharsets.UTF_8);

        when(api.lispGetMapRequestItrRlocs(any(LispGetMapRequestItrRlocs.class)))
                .thenReturn(CompletableFuture.completedFuture(reply));
    }

    private void doReturnNullDataOnDump() {
        when(api.lispGetMapRequestItrRlocs(any(LispGetMapRequestItrRlocs.class)))
                .thenReturn(CompletableFuture.completedFuture(null));
    }

    private void doReturnEmptyDataOnDump() {
        when(api.lispGetMapRequestItrRlocs(any(LispGetMapRequestItrRlocs.class)))
                .thenReturn(CompletableFuture.completedFuture(new LispGetMapRequestItrRlocsReply()));
    }

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

    private void verifyLispGetMapRequestItrRlocsInvokedOnce() {
        verify(api, times(1)).lispGetMapRequestItrRlocs(any(LispGetMapRequestItrRlocs.class));
    }

    private void verifyInvalidDataCase(final ItrRemoteLocatorSetBuilder builder) {
        assertNotNull(builder);
        assertNull(builder.getRemoteLocatorSetName());

        verifyLispGetMapRequestItrRlocsInvokedOnce();
    }
}