3a29a191ab8df36e76f78fa850e01bdd20ba582e
[hc2vpp.git] /
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.ip.subnet.validation;
18
19
20 import com.google.common.collect.Lists;
21 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Address;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.AddressBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.NetmaskBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.PrefixLengthBuilder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
30
31 public class SubnetValidatorTest {
32
33     private SubnetValidator subnetValidator;
34
35     @Before
36     public void init() {
37         MockitoAnnotations.initMocks(this);
38         subnetValidator = new SubnetValidator();
39     }
40
41     @Test(expected = SubnetValidationException.class)
42     public void testValidateNegativeSameTypes() throws SubnetValidationException {
43         List<Address> addresses = Lists.newArrayList();
44
45         addresses.add(new AddressBuilder().setSubnet(new PrefixLengthBuilder().setPrefixLength((short) 24).build())
46                 .build());
47         addresses.add(new AddressBuilder().setSubnet(new PrefixLengthBuilder().setPrefixLength((short) 24).build())
48                 .build());
49
50         subnetValidator.checkNotAddingToSameSubnet(addresses);
51     }
52
53     @Test(expected = SubnetValidationException.class)
54     public void testValidateNegativeMixedTypes() throws SubnetValidationException {
55         List<Address> addresses = Lists.newArrayList();
56
57         addresses.add(new AddressBuilder().setSubnet(new PrefixLengthBuilder().setPrefixLength((short) 24).build())
58                 .build());
59         addresses.add(new AddressBuilder()
60                 .setSubnet(new NetmaskBuilder().setNetmask(new DottedQuad("255.255.255.0")).build())
61                 .build());
62
63         subnetValidator.checkNotAddingToSameSubnet(addresses);
64     }
65
66     @Test
67     public void testValidatePositiveSameTypes() throws SubnetValidationException {
68         List<Address> addresses = Lists.newArrayList();
69
70         addresses.add(new AddressBuilder().setSubnet(new PrefixLengthBuilder().setPrefixLength((short) 24).build())
71                 .build());
72         addresses.add(new AddressBuilder().setSubnet(new PrefixLengthBuilder().setPrefixLength((short) 25).build())
73                 .build());
74
75         subnetValidator.checkNotAddingToSameSubnet(addresses);
76     }
77
78     @Test
79     public void testValidatePositiveMixedTypes() throws SubnetValidationException {
80         List<Address> addresses = Lists.newArrayList();
81
82         addresses.add(new AddressBuilder().setSubnet(new PrefixLengthBuilder().setPrefixLength((short) 24).build())
83                 .build());
84         addresses.add(new AddressBuilder()
85                 .setSubnet(new NetmaskBuilder().setNetmask(new DottedQuad("255.255.0.0")).build())
86                 .build());
87
88         subnetValidator.checkNotAddingToSameSubnet(addresses);
89     }
90 }