0a60afb6639a727eb801bf46b63e7b067653c4d2
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / IpProtoClassifierTest.java
1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
2
3 import static org.junit.Assert.*;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.junit.Before;
11 import org.junit.Rule;
12 import org.junit.Test;
13 import org.junit.rules.ExpectedException;
14 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
18
19 import com.google.common.collect.ImmutableMap;
20
21 public class IpProtoClassifierTest {
22
23     List<MatchBuilder> matches;
24     Map<String, ParameterValue> params;
25
26     @Rule
27     public ExpectedException thrown = ExpectedException.none();
28
29     @Before
30     public void setUp() {
31         matches = new ArrayList<>();
32         params = new HashMap<>();
33     }
34
35     @Test
36     public void setValueTest() {
37         matches.add(new MatchBuilder()
38                 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)));
39         params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.TCP));
40         matches = Classifier.IP_PROTO_CL.update(matches, params);
41         assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
42         assertEquals(true, ClassifierTestUtils.TCP.equals(matches.get(0).getIpMatch().getIpProtocol().longValue()));
43     }
44
45     @Test
46     public void overrideByTheSameValueTest() {
47         matches.add(new MatchBuilder()
48                 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE))
49                 .setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.UDP.shortValue())));
50         params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.UDP));
51         matches = Classifier.IP_PROTO_CL.update(matches, params);
52         assertEquals(true, ClassifierTestUtils.IPV6_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType()));
53         assertEquals(true, ClassifierTestUtils.UDP.equals(matches.get(0).getIpMatch().getIpProtocol().longValue()));
54     }
55
56     @Test
57     public void overrideByDifferentValueTest() {
58         matches.add(new MatchBuilder()
59                 .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))
60                 .setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.SCTP.shortValue())));
61         params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifier.PROTO_PARAM, ClassifierTestUtils.TCP));
62         thrown.expect(IllegalArgumentException.class);
63         thrown.expectMessage("Classification conflict detected");
64         matches = Classifier.IP_PROTO_CL.update(matches, params);
65     }
66
67     @Test
68     public void checkPresenceOfRequiredParameters1Test() {
69         params.putAll(ClassifierTestUtils.createIntValueParam(EtherTypeClassifier.ETHERTYPE_PARAM, FlowUtils.IPv4));
70         thrown.expect(IllegalArgumentException.class);
71         thrown.expectMessage("not specified");
72         Classifier.IP_PROTO_CL.checkPresenceOfRequiredParams(params);
73     }
74
75     @Test
76     public void checkPresenceOfRequiredParameters2Test() {
77         params.putAll(ImmutableMap.<String, ParameterValue> of(IpProtoClassifier.PROTO_PARAM,
78                 new ParameterValueBuilder().build()));
79         thrown.expect(IllegalArgumentException.class);
80         thrown.expectMessage("Value of proto parameter is not present");
81         Classifier.IP_PROTO_CL.checkPresenceOfRequiredParams(params);
82     }
83 }