GBP ofoverlay.sf test improvements
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / EtherTypeClassifierTest.java
1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertSame;
5
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import com.google.common.collect.ImmutableMap;
12 import org.junit.Rule;
13 import org.junit.Test;
14 import org.junit.rules.ExpectedException;
15 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
16 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
17 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
21
22 public class EtherTypeClassifierTest {
23
24     @Rule
25     public ExpectedException thrown = ExpectedException.none();
26
27     @Test
28     public void testUpdate() {
29         List<MatchBuilder> matches = new ArrayList<>();
30         Map<String, ParameterValue> params = new HashMap<>();
31         matches.add(
32                 new MatchBuilder().setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.TCP.shortValue())));
33         params.putAll(
34                 ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv4));
35
36         List<MatchBuilder> updated = Classifier.ETHER_TYPE_CL.update(matches, params);
37
38         assertEquals(1, updated.size());
39         MatchBuilder first = updated.get(0);
40         assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, first.getEthernetMatch().getEthernetType());
41         assertSame(ClassifierTestUtils.TCP, first.getIpMatch().getIpProtocol().longValue());
42     }
43
44     @Test
45     public void testUpdate_overrideBySameValue() {
46         List<MatchBuilder> matches = new ArrayList<>();
47         Map<String, ParameterValue> params = new HashMap<>();
48         matches.add(new MatchBuilder()
49             .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE))
50             .setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.UDP.shortValue())));
51         params.putAll(
52                 ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv6));
53
54         List<MatchBuilder> updated = Classifier.ETHER_TYPE_CL.update(matches, params);
55
56         assertEquals(1, updated.size());
57         MatchBuilder first = updated.get(0);
58         assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, first.getEthernetMatch().getEthernetType());
59         assertSame(ClassifierTestUtils.UDP, first.getIpMatch().getIpProtocol().longValue());
60     }
61
62     @Test
63     public void testUpdate_overrideByDifferentValue() {
64         List<MatchBuilder> matches = new ArrayList<>();
65         Map<String, ParameterValue> params = new HashMap<>();
66         matches.add(new MatchBuilder()
67             .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))
68             .setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.SCTP.shortValue())));
69         params.putAll(
70                 ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv6));
71
72         thrown.expect(IllegalArgumentException.class);
73         thrown.expectMessage(Classifier.MSG_CLASSIFICATION_CONFLICT_DETECTED);
74         Classifier.ETHER_TYPE_CL.update(matches, params);
75     }
76
77     @Test
78     public void testCheckPresenceOfRequiredParameters_EtherTypeMissing() {
79         Map<String, ParameterValue> params = new HashMap<>();
80         params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM,
81                 ClassifierTestUtils.TCP));
82
83         thrown.expect(IllegalArgumentException.class);
84         thrown.expectMessage(Classifier.MSG_NOT_SPECIFIED);
85         Classifier.ETHER_TYPE_CL.checkPresenceOfRequiredParams(params);
86     }
87
88     @Test
89     public void testCheckPresenceOfRequiredParameters_EtherTypeNull() {
90         Map<String, ParameterValue> params = new HashMap<>();
91         params.putAll(
92                 ImmutableMap.of(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, new ParameterValueBuilder().build()));
93
94         thrown.expect(IllegalArgumentException.class);
95         thrown.expectMessage(Classifier.MSG_PARAMETER_IS_NOT_PRESENT);
96         Classifier.ETHER_TYPE_CL.checkPresenceOfRequiredParams(params);
97     }
98 }