98d645d4d2d5d84654fde29020cdddd8a275895a
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / acl / AclWriterTest.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.interfaces.acl;
18
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.when;
21
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.translate.write.WriteFailedException;
24 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
25 import java.util.Collections;
26 import java.util.List;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.AccessLists;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.AclBase;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.EthAcl;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclKey;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.ietf.acl.base.attributes.AccessListsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.ietf.acl.base.attributes.access.lists.AclBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.IetfAclBuilder;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44
45 public class AclWriterTest extends WriterCustomizerTest {
46
47     private static final String ACL_NAME = "acl1";
48     private static final Class<? extends AclBase> ACL_TYPE = EthAcl.class;
49     private static final InstanceIdentifier<Acl> IID =
50         InstanceIdentifier.create(AccessLists.class).child(Acl.class, new AclKey(ACL_NAME, ACL_TYPE));
51
52     @Mock
53     private Acl acl;
54     private AclWriter customizer;
55
56     @Override
57     public void setUp() {
58         customizer = new AclWriter();
59         when(acl.getAclName()).thenReturn(ACL_NAME);
60         doReturn(ACL_TYPE).when(acl).getAclType();
61     }
62
63     private void defineInterfacesContext(final List<Interface> interfaces) {
64         when(writeContext.readAfter(InstanceIdentifier.create(Interfaces.class))).thenReturn(Optional.of(
65             new InterfacesBuilder().setInterface(interfaces).build()
66         ));
67     }
68
69     @Test
70     public void testWrite() throws Exception {
71         customizer.writeCurrentAttributes(IID, acl, writeContext);
72     }
73
74     @Test
75     public void testUpdate() throws WriteFailedException {
76         defineInterfacesContext(Collections.emptyList());
77         customizer.updateCurrentAttributes(IID, acl, acl, writeContext);
78     }
79
80     @Test
81     public void testDelete() throws WriteFailedException {
82         defineInterfacesContext(Collections.emptyList());
83         customizer.deleteCurrentAttributes(IID, acl, writeContext);
84     }
85
86     @Test(expected = WriteFailedException.class)
87     public void testDeleteFailed() throws WriteFailedException {
88         final Interface iface = new InterfaceBuilder().addAugmentation(VppInterfaceAugmentation.class,
89             new VppInterfaceAugmentationBuilder().setIetfAcl(
90                 new IetfAclBuilder().setAccessLists(
91                     new AccessListsBuilder().setAcl(
92                         Collections.singletonList(new AclBuilder().setName(ACL_NAME).setType(ACL_TYPE).build())
93                     ).build()
94                 ).build()
95             ).build()
96         ).build();
97         defineInterfacesContext(Collections.singletonList(iface));
98         customizer.deleteCurrentAttributes(IID, acl, writeContext);
99     }
100 }