HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfacesstate / TapCustomizerTest.java
1 /*
2  * Copyright (c) 2016 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.translate.v3po.interfacesstate;
18
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 import io.fd.honeycomb.translate.read.ReadFailedException;
25 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.honeycomb.vpp.test.read.ReaderCustomizerTest;
28 import io.fd.honeycomb.vpp.test.util.InterfaceDumpHelper;
29 import org.junit.Test;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.Tap;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.TapBuilder;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
39 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDetails;
40 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDetailsReplyDump;
41
42 public class TapCustomizerTest extends ReaderCustomizerTest<Tap, TapBuilder> implements InterfaceDumpHelper {
43
44     private static final String IFC_CTX_NAME = "ifc-test-instance";
45     private static final String IF_NAME = "tap1";
46     private static final String TAP_NAME = "testTapName";
47     private static final int IF_INDEX = 1;
48     private static final InstanceIdentifier<Tap> IID =
49         InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME))
50             .augmentation(VppInterfaceStateAugmentation.class).child(Tap.class);
51     private NamingContext interfaceContext;
52
53     public TapCustomizerTest() {
54         super(Tap.class, VppInterfaceStateAugmentationBuilder.class);
55     }
56
57     @Override
58     protected void setUp() throws Exception {
59         interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
60         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME);
61         whenSwInterfaceDumpThenReturn(api, ifaceDetails());
62     }
63
64     private SwInterfaceDetails ifaceDetails() {
65         final SwInterfaceDetails details = new SwInterfaceDetails();
66         details.swIfIndex = IF_INDEX;
67         details.interfaceName = IF_NAME.getBytes();
68         return details;
69     }
70
71     @Override
72     protected ReaderCustomizer<Tap, TapBuilder> initCustomizer() {
73         return new TapCustomizer(api, interfaceContext);
74     }
75
76     @Test
77     public void testRead() throws ReadFailedException {
78         final TapBuilder builder = mock(TapBuilder.class);
79         when(api.swInterfaceTapDump(any())).thenReturn(future(tapDump()));
80         getCustomizer().readCurrentAttributes(IID, builder, ctx);
81         verify(builder).setTapName(TAP_NAME);
82     }
83
84     @Test(expected = ReadFailedException.class)
85     public void testReadFailed() throws ReadFailedException {
86         when(api.swInterfaceTapDump(any())).thenReturn(failedFuture());
87         getCustomizer().readCurrentAttributes(IID, mock(TapBuilder.class), ctx);
88     }
89
90     private SwInterfaceTapDetailsReplyDump tapDump() {
91         final SwInterfaceTapDetailsReplyDump reply = new SwInterfaceTapDetailsReplyDump();
92         final SwInterfaceTapDetails details = new SwInterfaceTapDetails();
93         details.devName = TAP_NAME.getBytes();
94         details.swIfIndex = IF_INDEX;
95         reply.swInterfaceTapDetails.add(details);
96         return reply;
97     }
98 }