Minor fixes and adding unit testing for validators
[packetcable.git] / packetcable-policy-server / src / test / java / org / opendaylight / controller / packetcable / provider / validation / impl / validators / qos / classifier / ClassifierValidatorTest.java
1 /*
2  * Copyright (c) 2015 CableLabs and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.packetcable.provider.validation.impl.validators.qos.classifier;
10
11 import org.junit.Rule;
12 import org.junit.Test;
13 import org.opendaylight.controller.packetcable.provider.test.rules.Params;
14 import org.opendaylight.controller.packetcable.provider.validation.ValidationException;
15 import org.opendaylight.controller.packetcable.provider.validation.Validator;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.packetcable.rev151101.TosByte;
19 import org.opendaylight.yang.gen.v1.urn.packetcable.rev151101.TpProtocol;
20 import org.opendaylight.yang.gen.v1.urn.packetcable.rev151101.pcmm.qos.classifier.Classifier;
21 import org.opendaylight.yang.gen.v1.urn.packetcable.rev151101.pcmm.qos.classifier.ClassifierBuilder;
22
23 /**
24  * @author rvail
25  */
26 @Params.AlwaysUseParams
27 public class ClassifierValidatorTest {
28
29     @Rule
30     public Params<Validator.Extent> extentParams = Params.of(Validator.Extent.class);
31
32     private final ClassifierValidator validator= new ClassifierValidator();
33
34     @Test(expected = ValidationException.class)
35     public void nullClassifier() throws ValidationException {
36         validator.validate(null, extentParams.getCurrentParam());
37     }
38
39     @Params.DoNotUseParams
40     @Test(expected = NullPointerException.class)
41     public void nullExtent() throws ValidationException {
42         validator.validate(buildValidClassifierTree(), null);
43     }
44
45     @Test(expected = ValidationException.class)
46     public void nullSrcIp() throws ValidationException {
47         Classifier classifier = new ClassifierBuilder(buildValidClassifierTree())
48                 .setSrcIp(null)
49                 .build();
50
51         validator.validate(classifier, extentParams.getCurrentParam());
52     }
53
54     @Test(expected = ValidationException.class)
55      public void nullSrcPort() throws ValidationException {
56         Classifier classifier = new ClassifierBuilder(buildValidClassifierTree())
57                 .setSrcPort(null)
58                 .build();
59
60         validator.validate(classifier, extentParams.getCurrentParam());
61     }
62
63     @Test(expected = ValidationException.class)
64     public void nullDstIp() throws ValidationException {
65         Classifier classifier = new ClassifierBuilder(buildValidClassifierTree())
66                 .setDstIp(null)
67                 .build();
68
69         validator.validate(classifier, extentParams.getCurrentParam());
70     }
71
72     @Test(expected = ValidationException.class)
73     public void nullDstPort() throws ValidationException {
74         Classifier classifier = new ClassifierBuilder(buildValidClassifierTree())
75                 .setDstPort(null)
76                 .build();
77
78         validator.validate(classifier, extentParams.getCurrentParam());
79     }
80
81     @Test(expected = ValidationException.class)
82     public void nullProtocol() throws ValidationException {
83         Classifier classifier = new ClassifierBuilder(buildValidClassifierTree())
84                 .setProtocol(null)
85                 .build();
86
87         validator.validate(classifier, extentParams.getCurrentParam());
88     }
89
90     @Test(expected = ValidationException.class)
91     public void nullTosByte() throws ValidationException {
92         Classifier classifier = new ClassifierBuilder(buildValidClassifierTree())
93                 .setTosByte(null)
94                 .build();
95
96         validator.validate(classifier, extentParams.getCurrentParam());
97     }
98
99     @Test(expected = ValidationException.class)
100      public void nullTosMask() throws ValidationException {
101         Classifier classifier = new ClassifierBuilder(buildValidClassifierTree())
102                 .setTosMask(null)
103                 .build();
104
105         validator.validate(classifier, extentParams.getCurrentParam());
106     }
107
108     @Test
109     public void valid() throws ValidationException {
110         Classifier classifier = buildValidClassifierTree();
111         validator.validate(classifier, extentParams.getCurrentParam());
112     }
113
114     public static Classifier buildValidClassifierTree() {
115         return new ClassifierBuilder()
116                 .setSrcIp(new Ipv4Address("10.0.0.100"))
117                 .setSrcPort(new PortNumber(7000))
118                 .setDstIp(new Ipv4Address("10.0.0.200"))
119                 .setDstPort(new PortNumber(8000))
120                 .setProtocol(new TpProtocol(127))
121                 .setTosByte(new TosByte((short)0x10))
122                 .setTosMask(new TosByte((short)0xf0))
123                 .build();
124     }
125 }