neutron: unbreak of neutron northbound yang model revise
[groupbasedpolicy.git] / neutron-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / rule / SecRuleEntityDecoderTest.java
1 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
2
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.fail;
6
7 import java.util.List;
8
9 import org.junit.Assert;
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.api.sf.EtherTypeClassifierDefinition;
15 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
16 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
17 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.EndpointIdentificationConstraints;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraints;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.l3.endpoint.identification.constraints.PrefixConstraint;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.parameter.value.RangeValue;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ConsumerMatchers;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionBase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolBase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmp;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolUdp;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
48
49 public class SecRuleEntityDecoderTest {
50
51     @Rule
52     public ExpectedException thrown = ExpectedException.none();
53
54     private SecurityRuleBuilder secRuleBuilder;
55
56     @Before
57     public void setUp() throws Exception {
58         secRuleBuilder = new SecurityRuleBuilder().setUuid(new Uuid("01234567-abcd-ef01-0123-0123456789ab"));
59     }
60
61     @Test
62     public final void testGetContractId() {
63         Assert.assertEquals("01234567-abcd-ef01-0123-0123456789ab",
64                 SecRuleEntityDecoder.getContractId(secRuleBuilder.build()).getValue());
65     }
66
67     @Test
68     public final void testGetClassifierInstance_onlyEthertype() {
69         secRuleBuilder.setEthertype(EthertypeV4.class);
70         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRuleBuilder.build());
71         Assert.assertEquals(EtherTypeClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
72         // name is ether_type_IPv4
73         String expectedName = new StringBuilder().append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
74             .append(MappingUtils.NAME_VALUE_DELIMETER)
75             .append(EthertypeV4.class.getSimpleName())
76             .toString();
77         Assert.assertEquals(expectedName, ci.getName().getValue());
78         Assert.assertEquals(expectedName, ci.getName().getValue());
79         List<ParameterValue> parameterValues = ci.getParameterValue();
80         Assert.assertNotNull(parameterValues);
81         Assert.assertEquals(1, parameterValues.size());
82         ParameterValue parameter = parameterValues.get(0);
83         Assert.assertEquals(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, parameter.getName().getValue());
84         assertClassifierParameterValue(parameter, EtherTypeClassifierDefinition.IPv4_VALUE, null, null);
85     }
86
87     @Test
88     public final void testGetClassifierInstance_EthertypeAndProtocol() {
89         secRuleBuilder.setEthertype(EthertypeV4.class);
90         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
91         secRuleBuilder.setProtocol(protocolTcp);
92         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRuleBuilder.build());
93         Assert.assertEquals(IpProtoClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
94         // name is ip_proto_tcp__ether_type_IPv4
95         String expectedName = new StringBuilder().append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
96             .append(MappingUtils.NAME_VALUE_DELIMETER)
97             .append(ProtocolTcp.class.getSimpleName())
98             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
99             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
100             .append(MappingUtils.NAME_VALUE_DELIMETER)
101             .append(EthertypeV4.class.getSimpleName())
102             .toString();
103         Assert.assertEquals(expectedName, ci.getName().getValue());
104         List<ParameterValue> parameterValues = ci.getParameterValue();
105         Assert.assertNotNull(parameterValues);
106         Assert.assertEquals(2, parameterValues.size());
107         boolean containsEthertypeParam = false;
108         boolean containsProtoParam = false;
109         for (ParameterValue parameter : parameterValues) {
110             ParameterName parameterName = parameter.getName();
111             Assert.assertNotNull(parameterName);
112             if (EtherTypeClassifierDefinition.ETHERTYPE_PARAM.equals(parameterName.getValue())) {
113                 containsEthertypeParam = true;
114                 assertClassifierParameterValue(parameter, EtherTypeClassifierDefinition.IPv4_VALUE, null, null);
115             } else if (IpProtoClassifierDefinition.PROTO_PARAM.equals(parameterName.getValue())) {
116                 containsProtoParam = true;
117                 assertClassifierParameterValue(parameter, IpProtoClassifierDefinition.TCP_VALUE, null, null);
118             } else {
119                 fail("This parameter is not expected: " + parameter);
120             }
121         }
122         Assert.assertTrue("Classifier-instance does not contain ethertype parameter", containsEthertypeParam);
123         Assert.assertTrue("Classifier-instance does not contain protocol parameter", containsProtoParam);
124     }
125
126     @Test
127     public final void testGetClassifierInstance_EthertypeAndProtocolAndPort() {
128         secRuleBuilder.setEthertype(EthertypeV4.class);
129         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
130         secRuleBuilder.setProtocol(protocolTcp);
131         secRuleBuilder.setPortRangeMin(5);
132         secRuleBuilder.setPortRangeMax(5);
133         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRuleBuilder.build());
134         Assert.assertEquals(L4ClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
135         // name is l4_destport-5__ip_proto-tcp__ether_type-IPv4
136         String expectedName = new StringBuilder().append(L4ClassifierDefinition.DEFINITION.getName().getValue())
137             .append(MappingUtils.NAME_DELIMETER)
138             .append(L4ClassifierDefinition.DST_PORT_PARAM)
139             .append(MappingUtils.NAME_VALUE_DELIMETER)
140             .append(secRuleBuilder.getPortRangeMin())
141             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
142             .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
143             .append(MappingUtils.NAME_VALUE_DELIMETER)
144             .append(ProtocolTcp.class.getSimpleName())
145             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
146             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
147             .append(MappingUtils.NAME_VALUE_DELIMETER)
148             .append(EthertypeV4.class.getSimpleName())
149             .toString();
150         System.out.println(expectedName);
151         System.out.println(ci.getName().getValue());
152         Assert.assertEquals(expectedName, ci.getName().getValue());
153         List<ParameterValue> parameterValues = ci.getParameterValue();
154         Assert.assertNotNull(parameterValues);
155         Assert.assertEquals(3, parameterValues.size());
156         boolean containsEthertypeParam = false;
157         boolean containsProtoParam = false;
158         boolean containsDstPortParam = false;
159         for (ParameterValue parameter : parameterValues) {
160             ParameterName parameterName = parameter.getName();
161             Assert.assertNotNull(parameterName);
162             if (EtherTypeClassifierDefinition.ETHERTYPE_PARAM.equals(parameterName.getValue())) {
163                 containsEthertypeParam = true;
164                 assertClassifierParameterValue(parameter, EtherTypeClassifierDefinition.IPv4_VALUE, null, null);
165             } else if (IpProtoClassifierDefinition.PROTO_PARAM.equals(parameterName.getValue())) {
166                 containsProtoParam = true;
167                 assertClassifierParameterValue(parameter, IpProtoClassifierDefinition.TCP_VALUE, null, null);
168             } else if (L4ClassifierDefinition.DST_PORT_PARAM.equals(parameterName.getValue())) {
169                 containsDstPortParam = true;
170                 assertClassifierParameterValue(parameter, 5L, null, null);
171             } else {
172                 fail("This parameter is not expected: " + parameter);
173             }
174         }
175         Assert.assertTrue("Classifier-instance does not contain ethertype parameter", containsEthertypeParam);
176         Assert.assertTrue("Classifier-instance does not contain protocol parameter", containsProtoParam);
177         Assert.assertTrue("Classifier-instance does not contain destination port parameter", containsDstPortParam);
178     }
179
180     private final void assertClassifierParameterValue(ParameterValue parameter, Long expectedIntValue,
181             String expectedStringValue, RangeValue expectedRangeValue) {
182         Assert.assertEquals(expectedIntValue, parameter.getIntValue());
183         Assert.assertEquals(expectedStringValue, parameter.getStringValue());
184         Assert.assertEquals(expectedRangeValue, parameter.getRangeValue());
185     }
186
187     @Test
188     public final void testGetClassifierInstance_EthertypeAndProtocolAndPorts() {
189         secRuleBuilder.setEthertype(EthertypeV4.class);
190         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
191         secRuleBuilder.setProtocol(protocolTcp);
192         secRuleBuilder.setPortRangeMin(5);
193         secRuleBuilder.setPortRangeMax(10);
194         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRuleBuilder.build());
195         Assert.assertEquals(L4ClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
196         // name is l4_destport_range_min-5_max-10__ip_proto-tcp__ether_type-IPv4
197         String expectedName = new StringBuilder().append(L4ClassifierDefinition.DEFINITION.getName().getValue())
198             .append(MappingUtils.NAME_DELIMETER)
199             .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
200             .append(SecRuleNameDecoder.MIN_PORT)
201             .append(MappingUtils.NAME_VALUE_DELIMETER)
202             .append(secRuleBuilder.getPortRangeMin())
203             .append(SecRuleNameDecoder.MAX_PORT)
204             .append(MappingUtils.NAME_VALUE_DELIMETER)
205             .append(secRuleBuilder.getPortRangeMax())
206             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
207             .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
208             .append(MappingUtils.NAME_VALUE_DELIMETER)
209             .append(ProtocolTcp.class.getSimpleName())
210             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
211             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
212             .append(MappingUtils.NAME_VALUE_DELIMETER)
213             .append(EthertypeV4.class.getSimpleName())
214             .toString();
215         Assert.assertEquals(expectedName, ci.getName().getValue());
216     }
217
218     @Test
219     public final void testGetClassifierRef() {
220         secRuleBuilder.setDirection(DirectionIngress.class);
221         secRuleBuilder.setEthertype(EthertypeV4.class);
222         ClassifierName expectedName = SecRuleNameDecoder.getClassifierRefName(secRuleBuilder.build());
223         ClassifierRef cr = SecRuleEntityDecoder.getClassifierRef(secRuleBuilder.build());
224         Assert.assertEquals(expectedName, cr.getName());
225     }
226
227     @Test
228     public final void testGetDirection_directionIngress() {
229         secRuleBuilder.setDirection(DirectionIngress.class);
230         Assert.assertEquals(Direction.In, SecRuleEntityDecoder.getDirection(secRuleBuilder.build()));
231     }
232
233     @Test
234     public final void testGetDirection_directionEgress() {
235         secRuleBuilder.setDirection(DirectionEgress.class);
236         Assert.assertEquals(Direction.Out, SecRuleEntityDecoder.getDirection(secRuleBuilder.build()));
237     }
238
239     @Test
240     public final void testGetDirection_directionNull() {
241         secRuleBuilder.setDirection(null);
242         thrown.expect(IllegalArgumentException.class);
243         SecRuleEntityDecoder.getDirection(secRuleBuilder.build());
244     }
245
246     @Test
247     public final void testGetDirection_directionUnknown() {
248         secRuleBuilder.setDirection(UnknownDirection.class);
249         thrown.expect(IllegalArgumentException.class);
250         SecRuleEntityDecoder.getDirection(secRuleBuilder.build());
251     }
252
253     private static class UnknownDirection extends DirectionBase {
254     }
255
256     @Test
257     public final void testGetClause_noRemoteIpPrefix() {
258         secRuleBuilder.setDirection(DirectionIngress.class);
259         secRuleBuilder.setEthertype(EthertypeV4.class);
260         ClauseName expectedClauseName = SecRuleNameDecoder.getClauseName(secRuleBuilder.build());
261         Clause clause = SecRuleEntityDecoder.getClause(secRuleBuilder.build());
262         Assert.assertEquals(expectedClauseName, clause.getName());
263         List<SubjectName> subjectRefs = clause.getSubjectRefs();
264         Assert.assertNotNull(subjectRefs);
265         Assert.assertEquals(1, subjectRefs.size());
266         SubjectName subjectNameFromClause = subjectRefs.get(0);
267         SubjectName expectedSubjectName = SecRuleNameDecoder.getSubjectName(secRuleBuilder.build());
268         Assert.assertEquals(expectedSubjectName, subjectNameFromClause);
269         Assert.assertNull(clause.getConsumerMatchers());
270         Assert.assertNull(clause.getProviderMatchers());
271     }
272
273     @Test
274     public final void testGetClause_remoteIpPrefix() {
275         secRuleBuilder.setDirection(DirectionIngress.class);
276         secRuleBuilder.setEthertype(EthertypeV4.class);
277         secRuleBuilder.setRemoteIpPrefix(new IpPrefix(new Ipv4Prefix("10.0.0.0/8")));
278         Clause clause = SecRuleEntityDecoder.getClause(secRuleBuilder.build());
279         ClauseName expectedClauseName = SecRuleNameDecoder.getClauseName(secRuleBuilder.build());
280         Assert.assertEquals(expectedClauseName, clause.getName());
281         List<SubjectName> subjectRefs = clause.getSubjectRefs();
282         Assert.assertNotNull(subjectRefs);
283         Assert.assertEquals(1, subjectRefs.size());
284         SubjectName subjectNameFromClause = subjectRefs.get(0);
285         SubjectName expectedSubjectName = SecRuleNameDecoder.getSubjectName(secRuleBuilder.build());
286         Assert.assertEquals(expectedSubjectName, subjectNameFromClause);
287         Assert.assertNull(clause.getProviderMatchers());
288         ConsumerMatchers consumerMatchers = clause.getConsumerMatchers();
289         Assert.assertNotNull(consumerMatchers);
290         Assert.assertNull(consumerMatchers.getConditionMatcher());
291         Assert.assertNull(consumerMatchers.getGroupIdentificationConstraints());
292         EndpointIdentificationConstraints endpointIdentificationConstraints =
293                 consumerMatchers.getEndpointIdentificationConstraints();
294         Assert.assertNotNull(endpointIdentificationConstraints);
295         L3EndpointIdentificationConstraints l3EndpointIdentificationConstraints =
296                 endpointIdentificationConstraints.getL3EndpointIdentificationConstraints();
297         Assert.assertNotNull(l3EndpointIdentificationConstraints);
298         List<PrefixConstraint> prefixConstraints = l3EndpointIdentificationConstraints.getPrefixConstraint();
299         Assert.assertNotNull(prefixConstraints);
300         Assert.assertEquals(1, prefixConstraints.size());
301         PrefixConstraint prefixConstraint = prefixConstraints.get(0);
302         Assert.assertEquals(new Ipv4Prefix("10.0.0.0/8"), prefixConstraint.getIpPrefix().getIpv4Prefix());
303     }
304
305     @Test
306     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeIPv4() {
307         SecurityRule one = secRuleBuilder.setEthertype(EthertypeV4.class).build();
308         SecurityRule two = secRuleBuilder.setEthertype(EthertypeV4.class).build();
309         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
310     }
311
312     @Test
313     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeNull() {
314         SecurityRule one = secRuleBuilder.setEthertype(EthertypeV4.class).build();
315         SecurityRule two = secRuleBuilder.setEthertype(null).build();
316         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
317     }
318
319     @Test
320     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeNullTwoEthTypeNull() {
321         SecurityRule one = secRuleBuilder.setEthertype(null).build();
322         SecurityRule two = secRuleBuilder.setEthertype(null).build();
323         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
324     }
325
326     @Test
327     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeIPv6() {
328         SecurityRule one = secRuleBuilder.setEthertype(EthertypeV4.class).build();
329         SecurityRule two = secRuleBuilder.setEthertype(EthertypeV6.class).build();
330         assertFalse(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
331     }
332
333     @Test
334     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeNullTwoEthTypeIPv4() {
335         SecurityRule one = secRuleBuilder.setEthertype(null).build();
336         SecurityRule two = secRuleBuilder.setEthertype(EthertypeV4.class).build();
337         assertFalse(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
338     }
339
340     @Test
341     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolTcp() {
342         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
343         SecurityRule one = secRuleBuilder.setProtocol(protocolTcp).build();
344         SecurityRule two = secRuleBuilder.setProtocol(protocolTcp).build();
345         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
346     }
347
348     @Test
349     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolNull() {
350         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
351         SecurityRule one = secRuleBuilder.setProtocol(protocolTcp).build();
352         SecurityRule two = secRuleBuilder.setProtocol(null).build();
353         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
354     }
355
356     @Test
357     public void testIsProtocolOfOneWithinTwo_oneProtocolNullTwoProtocolNull() {
358         SecurityRule one = secRuleBuilder.setProtocol(null).build();
359         SecurityRule two = secRuleBuilder.setProtocol(null).build();
360         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
361     }
362
363     @Test
364     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolUdp() {
365         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
366         SecurityRule one = secRuleBuilder.setProtocol(protocolTcp).build();
367         SecurityRuleAttributes.Protocol protocolUdp = new SecurityRuleAttributes.Protocol(ProtocolUdp.class);
368         SecurityRule two = secRuleBuilder.setProtocol(protocolUdp).build();
369         assertFalse(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
370     }
371
372     @Test
373     public void testIsProtocolOfOneWithinTwo_oneProtocolNullTwoProtocolTcp() {
374         SecurityRule one = secRuleBuilder.setProtocol(null).build();
375         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
376         SecurityRule two = secRuleBuilder.setProtocol(protocolTcp).build();
377         assertFalse(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
378     }
379
380     @Test
381     public final void testIsPortsOfOneWithinTwo_onePortMinGtTwoPortMinOnePortMaxLtTwoPortMax() {
382         SecurityRule one = createSecRuleWithMinMaxPort(6, 9);
383         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
384         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
385     }
386
387     @Test
388     public final void testIsPortsOfOneWithinTwo_onePortMinEqTwoPortMinOnePortMaxEqTwoPortMax() {
389         SecurityRule one = createSecRuleWithMinMaxPort(5, 10);
390         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
391         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
392     }
393
394     @Test
395     public final void testIsPortsOfOneWithinTwo_onePortMinTwoPortMinNullOnePortMaxTwoPortMaxNull() {
396         SecurityRule one = createSecRuleWithMinMaxPort(4, 9);
397         SecurityRule two = createSecRuleWithMinMaxPort(null, null);
398         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
399     }
400
401     @Test
402     public final void testIsPortsOfOneWithinTwo_onePortMinNullTwoPortMinNullOnePortMaxNullTwoPortMaxNull() {
403         SecurityRule one = createSecRuleWithMinMaxPort(null, null);
404         SecurityRule two = createSecRuleWithMinMaxPort(null, null);
405         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
406     }
407
408     @Test
409     public final void testIsPortsOfOneWithinTwo_onePortMinNullTwoPortMinOnePortMaxNullTwoPortMax() {
410         SecurityRule one = createSecRuleWithMinMaxPort(null, null);
411         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
412         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
413     }
414
415     @Test
416     public final void testIsPortsOfOneWithinTwo_onePortMinLtTwoPortMinOnePortMaxLtTwoPortMax() {
417         SecurityRule one = createSecRuleWithMinMaxPort(4, 9);
418         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
419         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
420     }
421
422     @Test
423     public final void testIsPortsOfOneWithinTwo_onePortMinGtTwoPortMinOnePortMaxGtTwoPortMax() {
424         SecurityRule one = createSecRuleWithMinMaxPort(6, 11);
425         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
426         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
427     }
428
429     @Test
430     public final void testIsPortsOfOneWithinTwo_onePortMinLtTwoPortMinOnePortMaxGtTwoPortMax() {
431         SecurityRule one = createSecRuleWithMinMaxPort(4, 11);
432         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
433         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
434     }
435
436     private SecurityRule createSecRuleWithMinMaxPort(Integer portMin, Integer portMax) {
437         return secRuleBuilder.setPortRangeMin(portMin).setPortRangeMax(portMax).build();
438     }
439
440     @Test
441     public final void testGetEtherType_ethertypeIPv4() {
442         secRuleBuilder.setEthertype(EthertypeV4.class);
443         Assert.assertEquals(EtherTypeClassifierDefinition.IPv4_VALUE, SecRuleEntityDecoder.getEtherType(secRuleBuilder.build()));
444     }
445
446     @Test
447     public final void testGetEtherType_ethertypeIPv6() {
448         secRuleBuilder.setEthertype(EthertypeV6.class);
449         Assert.assertEquals(EtherTypeClassifierDefinition.IPv6_VALUE, SecRuleEntityDecoder.getEtherType(secRuleBuilder.build()));
450     }
451
452     @Test
453     public final void testGetEtherType_ethertypeNull() {
454         secRuleBuilder.setEthertype(null);
455         Assert.assertNull(SecRuleEntityDecoder.getEtherType(secRuleBuilder.build()));
456     }
457
458     @Test
459     public final void testGetEtherType_ethertypeUnknown() {
460         secRuleBuilder.setEthertype(UnknownEthertype.class);
461         thrown.expect(IllegalArgumentException.class);
462         SecRuleEntityDecoder.getEtherType(secRuleBuilder.build());
463     }
464
465     private static class UnknownEthertype extends EthertypeBase {
466     }
467
468     @Test
469     public final void testGetProtocol_protoTcp() {
470         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
471         secRuleBuilder.setProtocol(protocolTcp);
472         Assert.assertEquals(IpProtoClassifierDefinition.TCP_VALUE, SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
473     }
474
475     @Test
476     public final void testGetProtocol_protoUdp() {
477         SecurityRuleAttributes.Protocol protocolUdp = new SecurityRuleAttributes.Protocol(ProtocolUdp.class);
478         secRuleBuilder.setProtocol(protocolUdp);
479         Assert.assertEquals(IpProtoClassifierDefinition.UDP_VALUE, SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
480     }
481
482     @Test
483     public final void testGetProtocol_protoIcmp() {
484         SecurityRuleAttributes.Protocol protocolIcmp = new SecurityRuleAttributes.Protocol(ProtocolIcmp.class);
485         secRuleBuilder.setProtocol(protocolIcmp);
486         Assert.assertEquals(IpProtoClassifierDefinition.ICMP_VALUE, SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
487     }
488
489     @Test
490     public final void testGetProtocol_protoNull() {
491         secRuleBuilder.setProtocol(null);
492         Assert.assertNull(SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
493     }
494
495     @Test
496     public final void testGetProtocol_protoUnknown() {
497         SecurityRuleAttributes.Protocol protocolUnknown = new SecurityRuleAttributes.Protocol(UnknownProtocol.class);
498         secRuleBuilder.setProtocol(protocolUnknown);
499         thrown.expect(IllegalArgumentException.class);
500         SecRuleEntityDecoder.getProtocol(secRuleBuilder.build());
501     }
502
503     private static class UnknownProtocol extends ProtocolBase {
504     }
505 }