From ea6353d83982bcc5e0eda1c1cb715044903e8550 Mon Sep 17 00:00:00 2001 From: Konstantin Blagov Date: Thu, 28 Apr 2016 15:38:45 +0200 Subject: [PATCH] GBP ofoverlay.sf test improvements Change-Id: Ieff99ffcc515c57d9b7a603df18928d9cd12a208 Signed-off-by: Konstantin Blagov --- .../renderer/ofoverlay/sf/Classifier.java | 27 +- .../ofoverlay/sf/EtherTypeClassifier.java | 26 +- .../ofoverlay/sf/IpProtoClassifier.java | 31 +- .../renderer/ofoverlay/sf/L4Classifier.java | 56 ++- .../ofoverlay/sf/ChainActionTest.java | 82 ++-- .../sf/ClassificationResultTest.java | 49 ++- .../renderer/ofoverlay/sf/ClassifierTest.java | 34 +- .../ofoverlay/sf/ClassifierTestUtils.java | 73 +-- .../ofoverlay/sf/EtherTypeClassifierTest.java | 94 ++-- .../ofoverlay/sf/IpProtoClassifierTest.java | 87 ++-- .../ofoverlay/sf/L4ClassifierTest.java | 416 ++++++++++-------- 11 files changed, 530 insertions(+), 445 deletions(-) mode change 100644 => 100755 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassificationResultTest.java mode change 100644 => 100755 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassifierTest.java mode change 100644 => 100755 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassifierTestUtils.java mode change 100644 => 100755 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/EtherTypeClassifierTest.java mode change 100644 => 100755 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/IpProtoClassifierTest.java mode change 100644 => 100755 renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/L4ClassifierTest.java diff --git a/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/Classifier.java b/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/Classifier.java index 1ff03b2bb..6ad061aeb 100755 --- a/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/Classifier.java +++ b/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/Classifier.java @@ -25,6 +25,16 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.r */ public abstract class Classifier { + // Messages for exceptions (also for exception tests) + public static final String MSG_CLASSIFICATION_CONFLICT_DETECTED = "Classification conflict detected"; + public static final String MSG_NOT_SPECIFIED = "not specified"; + public static final String MSG_PARAMETER_IS_NOT_PRESENT = "parameter is not present"; + public static final String MSG_MUTUALLY_EXCLUSIVE = "mutually exclusive"; + public static final String MSG_RANGE_VALUE_MISMATCH = "Range value mismatch."; + public static final String MSG_NOT_SUPPORTED = "not supported"; + public static final String MSG_IS_MISSING = "is missing"; + public static final String MSG_NOT_PRESENT = "not present"; + protected final Classifier parent; public static final EtherTypeClassifier ETHER_TYPE_CL = new EtherTypeClassifier(null); @@ -58,7 +68,7 @@ public abstract class Classifier { /** * The result represents supported parameters for the classifier by renderer - * + * * @return list of supported parameters by the classifier */ public abstract List getSupportedParameterValues(); @@ -73,8 +83,8 @@ public abstract class Classifier { */ public final ClassificationResult updateMatch(List matches, Map params) { if (params == null) { - return new ClassificationResult("Classifier-instance with classifier-definition-id: " + this.getId() - + ". No parameters present."); + return new ClassificationResult( + "Classifier-instance with classifier-definition-id: " + this.getId() + ". No parameters present."); } List matchBuilders = matches; try { @@ -110,10 +120,13 @@ public abstract class Classifier { } /** - * Checks presence of required {@code params} in order to decide if classifier can update {@code matches} properly - * in method {@link #update(List, Map)} - * @param params inserted parameters, not null - * @throws IllegalArgumentException when any of required {@code params} is not present, see {@link #updateMatch(List, Map)} + * Checks presence of required {@code params} in order to decide if classifier can update + * {@code matches} properly + * in method {@link #update(List, Map)} + * + * @param params inserted parameters, not null + * @throws IllegalArgumentException when any of required {@code params} is not present, see + * {@link #updateMatch(List, Map)} */ protected abstract void checkPresenceOfRequiredParams(Map params); diff --git a/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/EtherTypeClassifier.java b/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/EtherTypeClassifier.java index 3d90f9706..e5ad48d30 100755 --- a/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/EtherTypeClassifier.java +++ b/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/EtherTypeClassifier.java @@ -52,12 +52,9 @@ public class EtherTypeClassifier extends Classifier { public List getSupportedParameterValues() { List values = ImmutableList.of( - new SupportedIntValueBuilder().setValue(EtherTypeClassifierDefinition.ARP_VALUE) - .build(), - new SupportedIntValueBuilder().setValue(EtherTypeClassifierDefinition.IPv4_VALUE) - .build(), - new SupportedIntValueBuilder().setValue(EtherTypeClassifierDefinition.IPv6_VALUE) - .build()); + new SupportedIntValueBuilder().setValue(EtherTypeClassifierDefinition.ARP_VALUE).build(), + new SupportedIntValueBuilder().setValue(EtherTypeClassifierDefinition.IPv4_VALUE).build(), + new SupportedIntValueBuilder().setValue(EtherTypeClassifierDefinition.IPv6_VALUE).build()); SupportedParameterValuesBuilder b = new SupportedParameterValuesBuilder(); b.setParameterName(new ParameterName(EtherTypeClassifierDefinition.ETHERTYPE_PARAM)); b.setParameterType(new IntBuilder().setSupportedIntValue(values).build()); @@ -69,17 +66,16 @@ public class EtherTypeClassifier extends Classifier { protected void checkPresenceOfRequiredParams(Map params) { if (params.get(EtherTypeClassifierDefinition.ETHERTYPE_PARAM) == null) { throw new IllegalArgumentException( - "Parameter " + EtherTypeClassifierDefinition.ETHERTYPE_PARAM + " not specified."); + "Parameter " + EtherTypeClassifierDefinition.ETHERTYPE_PARAM + " " + MSG_NOT_SPECIFIED); } if (params.get(EtherTypeClassifierDefinition.ETHERTYPE_PARAM).getIntValue() == null) { throw new IllegalArgumentException( - "Value of " + EtherTypeClassifierDefinition.ETHERTYPE_PARAM + " parameter is not present."); + "Value of " + EtherTypeClassifierDefinition.ETHERTYPE_PARAM + " " + MSG_PARAMETER_IS_NOT_PRESENT); } } @Override - protected List update(List matches, - Map params) { + protected List update(List matches, Map params) { Long type = params.get(EtherTypeClassifierDefinition.ETHERTYPE_PARAM).getIntValue(); for (MatchBuilder match : matches) { EthernetMatchBuilder em; @@ -97,11 +93,11 @@ public class EtherTypeClassifier extends Classifier { private void equalOrNotSetValidation(EthernetType ethTypeInMatch, long paramValue) { if (ethTypeInMatch != null) { - if (paramValue != ethTypeInMatch.getType().getValue().longValue()) { - throw new IllegalArgumentException( - "Classification conflict detected at " + EtherTypeClassifierDefinition.ETHERTYPE_PARAM + " parameter for values " + ethTypeInMatch - .getType() - .getValue() + " and " + paramValue + ". It is not allowed " + "to assign different values to the same parameter among all the classifiers within one rule."); + if (paramValue != ethTypeInMatch.getType().getValue()) { + throw new IllegalArgumentException(MSG_CLASSIFICATION_CONFLICT_DETECTED + " at " + + EtherTypeClassifierDefinition.ETHERTYPE_PARAM + " parameter for values " + + ethTypeInMatch.getType().getValue() + " and " + paramValue + ". It is not allowed " + + "to assign different values to the same parameter among all the classifiers within one rule."); } } } diff --git a/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/IpProtoClassifier.java b/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/IpProtoClassifier.java index 56f1649d9..8f1a04e87 100755 --- a/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/IpProtoClassifier.java +++ b/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/IpProtoClassifier.java @@ -51,14 +51,10 @@ public class IpProtoClassifier extends Classifier { public List getSupportedParameterValues() { List values = ImmutableList.of( - new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.ICMP_VALUE) - .build(), - new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.SCTP_VALUE) - .build(), - new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.TCP_VALUE) - .build(), - new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.UDP_VALUE) - .build()); + new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.ICMP_VALUE).build(), + new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.SCTP_VALUE).build(), + new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.TCP_VALUE).build(), + new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.UDP_VALUE).build()); SupportedParameterValuesBuilder b = new SupportedParameterValuesBuilder(); b.setParameterName(new ParameterName(IpProtoClassifierDefinition.PROTO_PARAM)); b.setParameterType(new IntBuilder().setSupportedIntValue(values).build()); @@ -69,12 +65,12 @@ public class IpProtoClassifier extends Classifier { @Override protected void checkPresenceOfRequiredParams(Map params) { if (params.get(IpProtoClassifierDefinition.PROTO_PARAM) == null) { - throw new IllegalArgumentException("Parameter " + IpProtoClassifierDefinition.PROTO_PARAM - + " not specified."); + throw new IllegalArgumentException( + "Parameter " + IpProtoClassifierDefinition.PROTO_PARAM + " " + MSG_NOT_SPECIFIED); } if (params.get(IpProtoClassifierDefinition.PROTO_PARAM).getIntValue() == null) { - throw new IllegalArgumentException("Value of " + IpProtoClassifierDefinition.PROTO_PARAM - + " parameter is not present."); + throw new IllegalArgumentException( + "Value of " + IpProtoClassifierDefinition.PROTO_PARAM + " " + MSG_PARAMETER_IS_NOT_PRESENT); } } @@ -98,9 +94,9 @@ public class IpProtoClassifier extends Classifier { private void equalOrNotSetValidation(Short protoInMatch, long paramValue) { if (protoInMatch != null) { if (paramValue != protoInMatch.longValue()) { - throw new IllegalArgumentException("Classification conflict detected at " + IpProtoClassifierDefinition.PROTO_PARAM - + " parameter for values " + protoInMatch.shortValue() + " and " + paramValue - + ". It is not allowed " + throw new IllegalArgumentException(MSG_CLASSIFICATION_CONFLICT_DETECTED + " at " + + IpProtoClassifierDefinition.PROTO_PARAM + " parameter for values " + protoInMatch + " and " + + paramValue + ". It is not allowed " + "to assign different values to the same parameter among all the classifiers within one rule."); } } @@ -113,8 +109,8 @@ public class IpProtoClassifier extends Classifier { try { readEthType = match.getEthernetMatch().getEthernetType().getType().getValue(); } catch (NullPointerException e) { - throw new IllegalArgumentException("Parameter " + EtherTypeClassifierDefinition.ETHERTYPE_PARAM - + " is missing."); + throw new IllegalArgumentException( + "Parameter " + EtherTypeClassifierDefinition.ETHERTYPE_PARAM + " " + MSG_IS_MISSING); } if (!FlowUtils.IPv4.equals(readEthType) && !FlowUtils.IPv6.equals(readEthType)) { throw new IllegalArgumentException("Parameter " + EtherTypeClassifierDefinition.ETHERTYPE_PARAM @@ -125,6 +121,7 @@ public class IpProtoClassifier extends Classifier { /** * Return the IpProtocol value. May return null. + * * @param params the parameters of classifier-instance inserted by user * @return the IpProtocol value */ diff --git a/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/L4Classifier.java b/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/L4Classifier.java index fcdd7e931..49c2a7b34 100755 --- a/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/L4Classifier.java +++ b/renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/L4Classifier.java @@ -100,17 +100,15 @@ public class L4Classifier extends Classifier { if (params.get(portParam) != null) { StringBuilder paramLog = new StringBuilder(); if (params.get(portParam).getIntValue() == null) { - paramLog.append("Value of ") - .append(portParam) - .append(" parameter is not specified."); + paramLog.append("Value of ").append(portParam).append(" parameter " + MSG_NOT_SPECIFIED); throw new IllegalArgumentException(paramLog.toString()); } - if(params.get(portRangeParam) != null) { + if (params.get(portRangeParam) != null) { paramLog.append("Source port parameters ") .append(portParam) .append(" and ") .append(portRangeParam) - .append(" are mutually exclusive."); + .append(" are " + MSG_MUTUALLY_EXCLUSIVE); throw new IllegalArgumentException(paramLog.toString()); } } @@ -124,12 +122,14 @@ public class L4Classifier extends Classifier { private void validateRangeValue(RangeValue rangeValueParam) { if (rangeValueParam == null) { - throw new IllegalArgumentException("Range parameter is specifiet but value is not present."); + throw new IllegalArgumentException( + "Range parameter is specified but value is " + Classifier.MSG_NOT_PRESENT); } final Long min = rangeValueParam.getMin(); final Long max = rangeValueParam.getMax(); if (min > max) { - throw new IllegalArgumentException("Range value mismatch. " + min + " is greater than MAX " + max + "."); + throw new IllegalArgumentException( + MSG_RANGE_VALUE_MISMATCH + " MIN " + min + " is greater than MAX " + max + "."); } } @@ -137,8 +137,10 @@ public class L4Classifier extends Classifier { public List update(List matches, Map params) { Set sPorts = new HashSet<>(); Set dPorts = new HashSet<>(); - addToPortSet(params, L4ClassifierDefinition.SRC_PORT_PARAM, L4ClassifierDefinition.SRC_PORT_RANGE_PARAM, sPorts); - addToPortSet(params, L4ClassifierDefinition.DST_PORT_PARAM, L4ClassifierDefinition.DST_PORT_RANGE_PARAM, dPorts); + addToPortSet(params, L4ClassifierDefinition.SRC_PORT_PARAM, L4ClassifierDefinition.SRC_PORT_RANGE_PARAM, + sPorts); + addToPortSet(params, L4ClassifierDefinition.DST_PORT_PARAM, L4ClassifierDefinition.DST_PORT_RANGE_PARAM, + dPorts); List newMatches = new ArrayList<>(); for (MatchBuilder matchBuilder : matches) { Layer4Match l4Match = matchBuilder.getLayer4Match(); @@ -154,7 +156,8 @@ public class L4Classifier extends Classifier { return newMatches; } - private void addToPortSet(Map params, String portParam, String portRangeParam, Set portSet) { + private void addToPortSet(Map params, String portParam, String portRangeParam, + Set portSet) { if (params.get(portParam) != null) { portSet.add(params.get(portParam).getIntValue()); } else if (params.get(portRangeParam) != null) { @@ -175,7 +178,7 @@ public class L4Classifier extends Classifier { return new SctpMatchBuilder().build(); } throw new IllegalArgumentException("Parameter " + IpProtoClassifierDefinition.PROTO_PARAM + ": value " + ipProto - + " is not supported."); + + " is " + Classifier.MSG_NOT_SUPPORTED); } private Set createSetFromRange(RangeValue rangeValueParam) { @@ -207,7 +210,8 @@ public class L4Classifier extends Classifier { if (!sPorts.isEmpty() && dPorts.isEmpty()) { for (Long srcPort : sPorts) { equalOrNotSetValidation(udpMatch.getUdpSourcePort(), srcPort.longValue()); - udpMatches.add(new UdpMatchBuilder(udpMatch).setUdpSourcePort(new PortNumber(srcPort.intValue())).build()); + udpMatches + .add(new UdpMatchBuilder(udpMatch).setUdpSourcePort(new PortNumber(srcPort.intValue())).build()); } } else if (sPorts.isEmpty() && !dPorts.isEmpty()) { for (Long dstPort : dPorts) { @@ -234,7 +238,8 @@ public class L4Classifier extends Classifier { if (!sPorts.isEmpty() && dPorts.isEmpty()) { for (Long srcPort : sPorts) { equalOrNotSetValidation(tcpMatch.getTcpSourcePort(), srcPort.longValue()); - tcpMatches.add(new TcpMatchBuilder(tcpMatch).setTcpSourcePort(new PortNumber(srcPort.intValue())).build()); + tcpMatches + .add(new TcpMatchBuilder(tcpMatch).setTcpSourcePort(new PortNumber(srcPort.intValue())).build()); } } else if (sPorts.isEmpty() && !dPorts.isEmpty()) { for (Long dstPort : dPorts) { @@ -261,23 +266,24 @@ public class L4Classifier extends Classifier { if (!sPorts.isEmpty() && dPorts.isEmpty()) { for (Long srcPort : sPorts) { equalOrNotSetValidation(sctpMatch.getSctpSourcePort(), srcPort.longValue()); - sctpMatches.add(new SctpMatchBuilder(sctpMatch).setSctpSourcePort(new PortNumber(srcPort.intValue())) - .build()); + sctpMatches + .add(new SctpMatchBuilder(sctpMatch).setSctpSourcePort(new PortNumber(srcPort.intValue())).build()); } } else if (sPorts.isEmpty() && !dPorts.isEmpty()) { for (Long dstPort : dPorts) { equalOrNotSetValidation(sctpMatch.getSctpDestinationPort(), dstPort.longValue()); - sctpMatches.add(new SctpMatchBuilder(sctpMatch).setSctpDestinationPort(new PortNumber(dstPort.intValue())) - .build()); + sctpMatches.add(new SctpMatchBuilder(sctpMatch) + .setSctpDestinationPort(new PortNumber(dstPort.intValue())).build()); } } else if (!sPorts.isEmpty() && !dPorts.isEmpty()) { for (Long srcPort : sPorts) { for (Long dstPort : dPorts) { equalOrNotSetValidation(sctpMatch.getSctpSourcePort(), srcPort.longValue()); equalOrNotSetValidation(sctpMatch.getSctpDestinationPort(), dstPort.longValue()); - sctpMatches.add(new SctpMatchBuilder(sctpMatch).setSctpSourcePort(new PortNumber(srcPort.intValue())) - .setSctpDestinationPort(new PortNumber(dstPort.intValue())) - .build()); + sctpMatches + .add(new SctpMatchBuilder(sctpMatch).setSctpSourcePort(new PortNumber(srcPort.intValue())) + .setSctpDestinationPort(new PortNumber(dstPort.intValue())) + .build()); } } } @@ -287,7 +293,7 @@ public class L4Classifier extends Classifier { private void equalOrNotSetValidation(PortNumber portInMatch, long paramValue) { if (portInMatch != null) { if (paramValue != portInMatch.getValue().longValue()) { - throw new IllegalArgumentException("Classification conflict detected for port values " + throw new IllegalArgumentException(Classifier.MSG_CLASSIFICATION_CONFLICT_DETECTED + " for port values " + portInMatch.getValue().longValue() + " and " + paramValue + ". It is not allowed " + "to assign different values to the same parameter among all the classifiers within one rule."); } @@ -301,12 +307,14 @@ public class L4Classifier extends Classifier { try { proto = Long.valueOf(match.getIpMatch().getIpProtocol().longValue()); } catch (NullPointerException e) { - throw new IllegalArgumentException("Parameter " + IpProtoClassifierDefinition.PROTO_PARAM + " is missing."); + throw new IllegalArgumentException( + "Parameter " + IpProtoClassifierDefinition.PROTO_PARAM + " " + MSG_IS_MISSING); } - if (!IpProtoClassifierDefinition.TCP_VALUE.equals(proto) && !IpProtoClassifierDefinition.UDP_VALUE.equals(proto) + if (!IpProtoClassifierDefinition.TCP_VALUE.equals(proto) + && !IpProtoClassifierDefinition.UDP_VALUE.equals(proto) && !IpProtoClassifierDefinition.SCTP_VALUE.equals(proto)) { throw new IllegalArgumentException("Value of parameter " + IpProtoClassifierDefinition.PROTO_PARAM - + " is not supported."); + + " is " + Classifier.MSG_NOT_SUPPORTED); } } } diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ChainActionTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ChainActionTest.java index 9fde7121e..a2ae75bd9 100755 --- a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ChainActionTest.java +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ChainActionTest.java @@ -8,11 +8,12 @@ package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf; +import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -20,6 +21,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.google.common.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -35,13 +38,19 @@ import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter; import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager; import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory.EndpointFwdCtxOrdinals; import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.policyenforcer.NetworkElements; +import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.RspName; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfcName; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfpName; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath; +import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePathBuilder; import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath; +import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPathBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; @@ -50,9 +59,6 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import com.google.common.base.Optional; -import com.google.common.util.concurrent.CheckedFuture; - @RunWith(PowerMockRunner.class) @PrepareForTest(ChainAction.class) public class ChainActionTest { @@ -72,16 +78,19 @@ public class ChainActionTest { private Endpoint endpoint; private EndpointManager endpointManager; private EgKey egKey; - private TenantId tenant = new TenantId("e09a2308-6ffa-40af-92a2-69f54b2cf3e4"); + private TenantId tenantId = new TenantId("e09a2308-6ffa-40af-92a2-69f54b2cf3e4"); @SuppressWarnings("unchecked") @Before - public void initialise() throws Exception { + public void init() throws Exception { chainAction = new ChainAction(); - sfcPath = mock(ServiceFunctionPath.class); - when(sfcPath.getName()).thenReturn(new SfpName("sfcPathName")); - when(sfcPath.isSymmetric()).thenReturn(true); + sfcPath = new ServiceFunctionPathBuilder().setName(new SfpName("sfcPathName")).setSymmetric(true).build(); + rsp = new RenderedServicePathBuilder().setName(new RspName("rspName")).build(); + endpoint = new EndpointBuilder().setL2Context(new L2BridgeDomainId("L2context")) + .setMacAddress(new MacAddress("01:23:45:67:89:ab")) + .setTenant(tenantId) + .build(); ctx = mock(OfContext.class); dataBroker = mock(DataBroker.class); @@ -92,7 +101,6 @@ public class ChainActionTest { when(rTx.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(checkedFuture); optRsp = mock(Optional.class); when(checkedFuture.checkedGet()).thenReturn(optRsp); - rsp = mock(RenderedServicePath.class); when(optRsp.isPresent()).thenReturn(true).thenReturn(false); when(optRsp.get()).thenReturn(rsp); @@ -102,36 +110,35 @@ public class ChainActionTest { endpointFwdCtxOrdinals = mock(EndpointFwdCtxOrdinals.class); when(netElements.getSrcEpOrdinals()).thenReturn(endpointFwdCtxOrdinals); when(netElements.getDstEpOrdinals()).thenReturn(endpointFwdCtxOrdinals); - endpoint = mock(Endpoint.class); when(netElements.getSrcEp()).thenReturn(endpoint); - when(netElements.getSrcEp().getTenant()).thenReturn(tenant); endpointManager = mock(EndpointManager.class); when(ctx.getEndpointManager()).thenReturn(endpointManager); egKey = mock(EgKey.class); - Set keysForEndpoint = new HashSet(); + Set keysForEndpoint = new HashSet<>(); keysForEndpoint.add(egKey); when(endpointManager.getEgKeysForEndpoint(any(Endpoint.class))).thenReturn(keysForEndpoint); } @Test - public void staticTest() { - Assert.assertNotNull(chainAction.getId()); - Assert.assertNotNull(chainAction.getActionDef()); + public void testGetters() { + assertEquals(ChainActionDefinition.ID, chainAction.getId()); + assertEquals(ChainActionDefinition.DEFINITION, chainAction.getActionDef()); } @Test - public void updateActionTestDirectionOutOpendaylightSfcNull() { + public void testUpdateAction_DirectionOut_OpendaylightSfcNull() { ActionBuilder actionBuilder = mock(ActionBuilder.class); - List actions = Arrays.asList(actionBuilder); - Map params = new HashMap(); + List actions = Collections.singletonList(actionBuilder); + Map params = new HashMap<>(); String chainName = "chainName"; params.put(ChainActionDefinition.SFC_CHAIN_NAME, chainName); - Integer order = Integer.valueOf(0); + Integer order = 0; OfWriter ofWriter = mock(OfWriter.class); PowerMockito.mockStatic(ChainAction.class); - when(ChainAction.getSfcPath(new SfcName(chainName))).thenReturn(sfcPath); + SfcName sfcName = new SfcName(chainName); + when(ChainAction.getSfcPath(eq(sfcName))).thenReturn(sfcPath); chainAction.setResolvedSymmetricChains(Collections.singletonList(chainName)); List result = @@ -140,10 +147,10 @@ public class ChainActionTest { } @Test - public void updateActionTestParamsNull() { + public void testUpdateAction_ParamsNull() { ActionBuilder actionBuilder = mock(ActionBuilder.class); - List actions = Arrays.asList(actionBuilder); - Integer order = Integer.valueOf(0); + List actions = Collections.singletonList(actionBuilder); + Integer order = 0; OfWriter ofWriter = mock(OfWriter.class); List result = @@ -152,12 +159,12 @@ public class ChainActionTest { } @Test - public void updateActionTestChainNameNull() { + public void testUpdateAction_ChainNameNull() { ActionBuilder actionBuilder = mock(ActionBuilder.class); - List actions = Arrays.asList(actionBuilder); - Map params = new HashMap(); + List actions = Collections.singletonList(actionBuilder); + Map params = new HashMap<>(); params.put(ChainActionDefinition.SFC_CHAIN_NAME, null); - Integer order = Integer.valueOf(0); + Integer order = 0; NetworkElements netElements = mock(NetworkElements.class); OfWriter ofWriter = mock(OfWriter.class); @@ -168,18 +175,19 @@ public class ChainActionTest { } @Test - public void updateActionTest() { + public void testUpdateAction() { ActionBuilder actionBuilder = mock(ActionBuilder.class); - List actions = Arrays.asList(actionBuilder); - Map params = new HashMap(); + List actions = Collections.singletonList(actionBuilder); + Map params = new HashMap<>(); String chainName = "chainName"; params.put(ChainActionDefinition.SFC_CHAIN_NAME, chainName); - Integer order = Integer.valueOf(0); + Integer order = 0; OfWriter ofWriter = mock(OfWriter.class); + ServiceFunctionPath sfcPathNameNull = new ServiceFunctionPathBuilder().setName(null).setSymmetric(true).build(); + PowerMockito.mockStatic(ChainAction.class); - when(ChainAction.getSfcPath(new SfcName(chainName))).thenReturn(sfcPath); - when(sfcPath.getName()).thenReturn(null); + when(ChainAction.getSfcPath(new SfcName(chainName))).thenReturn(sfcPathNameNull); List result = chainAction.updateAction(actions, params, order, netElements, ofWriter, ctx, Direction.Out); @@ -187,8 +195,8 @@ public class ChainActionTest { } @Test - public void isValidTestParameterValueNull() { - ActionInstance actionInstance = mock(ActionInstance.class); - Assert.assertFalse(chainAction.validate(actionInstance).isValid()); + public void testIsValid_ParameterValueNull() { + ActionInstance actionInstance = mock(ActionInstance.class); + Assert.assertFalse(chainAction.validate(actionInstance).isValid()); } } diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassificationResultTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassificationResultTest.java old mode 100644 new mode 100755 index b68f4a2d4..3a5111bae --- a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassificationResultTest.java +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassificationResultTest.java @@ -1,6 +1,6 @@ /* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. - * + * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html @@ -8,55 +8,56 @@ package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; -import java.util.Arrays; +import java.util.Collections; import java.util.List; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder; public class ClassificationResultTest { - private ClassificationResult result; - - private MatchBuilder matchBuilder; private List matches; private String errorMessage = "errorMessage"; @Before - public void initialise() { - matchBuilder = mock(MatchBuilder.class); - matches = Arrays.asList(matchBuilder); + public void init() { + MatchBuilder matchBuilder = mock(MatchBuilder.class); + matches = Collections.singletonList(matchBuilder); } @Test - public void getMatchBuildersTestSuccess() { - result = new ClassificationResult(matches); - Assert.assertEquals(matches, result.getMatchBuilders()); + public void testGetMatchBuilders_Success() { + ClassificationResult result = new ClassificationResult(matches); + assertEquals(matches, result.getMatchBuilders()); } @Test(expected = IllegalStateException.class) - public void getMatchBuildersTestFailure() { - result = new ClassificationResult(errorMessage); + public void testGetMatchBuilders_Failure() { + ClassificationResult result = new ClassificationResult(errorMessage); result.getMatchBuilders(); } @Test - public void getErrorMessageTest() { - result = new ClassificationResult(errorMessage); - Assert.assertEquals(errorMessage, result.getErrorMessage()); - result = new ClassificationResult(matches); - Assert.assertTrue(result.getErrorMessage().isEmpty()); + public void testGetErrorMessage() { + ClassificationResult result1 = new ClassificationResult(errorMessage); + assertEquals(errorMessage, result1.getErrorMessage()); + + ClassificationResult result2 = new ClassificationResult(matches); + assertTrue(result2.getErrorMessage().isEmpty()); } @Test - public void isSuccessfullTest() { - result = new ClassificationResult(errorMessage); - Assert.assertFalse(result.isSuccessfull()); - result = new ClassificationResult(matches); - Assert.assertTrue(result.isSuccessfull()); + public void testIsSuccessfull() { + ClassificationResult result1 = new ClassificationResult(errorMessage); + assertFalse(result1.isSuccessfull()); + + ClassificationResult result2 = new ClassificationResult(matches); + assertTrue(result2.isSuccessfull()); } } diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassifierTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassifierTest.java old mode 100644 new mode 100755 index e2c50aebb..b1f43f914 --- a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassifierTest.java +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassifierTest.java @@ -9,6 +9,8 @@ package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.Collections; @@ -35,37 +37,39 @@ public class ClassifierTest { Map params; @Before - public void setUp() { + public void init() { matches = new ArrayList<>(Collections.singletonList(new MatchBuilder())); params = new HashMap<>(); } @Test - public void updateMatchTest() { - Long dstRangeStart = Long.valueOf(8079); - Long dstRangeEnd = Long.valueOf(8081); - params.putAll(ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, - FlowUtils.IPv4)); + public void testUpdateMatch() { + Long dstRangeStart = 8079L; + Long dstRangeEnd = 8081L; + Long singlePort = 80L; + params.putAll( + ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv4)); params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.TCP)); - params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, 80)); + params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, singlePort)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd)); + ClassificationResult result = Classifier.L4_CL.updateMatch(matches, params); + assertEquals(true, result.isSuccessfull()); assertEquals(3, result.getMatchBuilders().size()); Set dstPorts = new HashSet<>(); for (MatchBuilder match : result.getMatchBuilders()) { - assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType())); - assertEquals(true, new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpSourcePort() - .getValue() - .intValue() == 80); - dstPorts.add(Long.valueOf(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpDestinationPort() + assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, match.getEthernetMatch().getEthernetType()); + assertSame(singlePort.intValue(), + new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpSourcePort().getValue()); + dstPorts.add(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpDestinationPort() .getValue() - .longValue())); + .longValue()); } - for (Long i = dstRangeStart; i <= dstRangeEnd; i++) { - assertEquals(true, dstPorts.contains((i))); + for (Long dstPort = dstRangeStart; dstPort <= dstRangeEnd; dstPort++) { + assertTrue(dstPorts.contains(dstPort)); } } } diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassifierTestUtils.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassifierTestUtils.java old mode 100644 new mode 100755 index 0b8f4a740..b8a3dc3ae --- a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassifierTestUtils.java +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ClassifierTestUtils.java @@ -2,6 +2,7 @@ package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf; import java.util.Map; +import com.google.common.collect.ImmutableMap; import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue; @@ -19,79 +20,53 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder; -import com.google.common.collect.ImmutableMap; - public class ClassifierTestUtils { static final EthernetType IPV4_ETH_TYPE = new EthernetTypeBuilder().setType(new EtherType(FlowUtils.IPv4)).build(); static final EthernetType IPV6_ETH_TYPE = new EthernetTypeBuilder().setType(new EtherType(FlowUtils.IPv6)).build(); - static final Long TCP = Long.valueOf(6); - static final Long UDP = Long.valueOf(17); - static final Long SCTP = Long.valueOf(132); + static final Long TCP = 6L; + static final Long UDP = 17L; + static final Long SCTP = 132L; static Map createRangeValueParam(String paramName, long min, long max) { - return ImmutableMap. of(paramName, - new ParameterValueBuilder() - .setRangeValue(new RangeValueBuilder() - .setMin(min) - .setMax(max) - .build()) - .build()); + return ImmutableMap.of(paramName, new ParameterValueBuilder() + .setRangeValue(new RangeValueBuilder().setMin(min).setMax(max).build()).build()); } static Map createIntValueParam(String paramName, long value) { - return ImmutableMap. of(paramName, - new ParameterValueBuilder() - .setIntValue(value) - .build()); + return ImmutableMap.of(paramName, new ParameterValueBuilder().setIntValue(value).build()); } - static final EthernetMatch createEthernetMatch(EthernetType ethType) { - return new EthernetMatchBuilder() - .setEthernetType(ethType) - .build(); + static EthernetMatch createEthernetMatch(EthernetType ethType) { + return new EthernetMatchBuilder().setEthernetType(ethType).build(); } - static final IpMatch createIpMatch(short ipProtoValue) { - return new IpMatchBuilder() - .setIpProtocol(ipProtoValue) - .build(); + static IpMatch createIpMatch(short ipProtoValue) { + return new IpMatchBuilder().setIpProtocol(ipProtoValue).build(); } - static final Layer4Match createUdpDstPort(int portNumber) { - return new UdpMatchBuilder() - .setUdpDestinationPort(new PortNumber(portNumber)) - .build(); + static Layer4Match createUdpDstPort(int portNumber) { + return new UdpMatchBuilder().setUdpDestinationPort(new PortNumber(portNumber)).build(); } - static final Layer4Match createUdpSrcPort(int portNumber) { - return new UdpMatchBuilder() - .setUdpSourcePort(new PortNumber(portNumber)) - .build(); + static Layer4Match createUdpSrcPort(int portNumber) { + return new UdpMatchBuilder().setUdpSourcePort(new PortNumber(portNumber)).build(); } - final Layer4Match createTcpDstPort(int portNumber) { - return new TcpMatchBuilder() - .setTcpDestinationPort(new PortNumber(portNumber)) - .build(); + static Layer4Match createTcpDstPort(int portNumber) { + return new TcpMatchBuilder().setTcpDestinationPort(new PortNumber(portNumber)).build(); } - final Layer4Match createTcpSrcPort(int portNumber) { - return new TcpMatchBuilder() - .setTcpSourcePort(new PortNumber(portNumber)) - .build(); + static Layer4Match createTcpSrcPort(int portNumber) { + return new TcpMatchBuilder().setTcpSourcePort(new PortNumber(portNumber)).build(); } - final static Layer4Match createSctpDstPort(int portNumber) { - return new SctpMatchBuilder() - .setSctpDestinationPort(new PortNumber(portNumber)) - .build(); + static Layer4Match createSctpDstPort(int portNumber) { + return new SctpMatchBuilder().setSctpDestinationPort(new PortNumber(portNumber)).build(); } - final static Layer4Match createSctpSrcPort(int portNumber) { - return new SctpMatchBuilder() - .setSctpSourcePort(new PortNumber(portNumber)) - .build(); + static Layer4Match createSctpSrcPort(int portNumber) { + return new SctpMatchBuilder().setSctpSourcePort(new PortNumber(portNumber)).build(); } -} \ No newline at end of file +} diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/EtherTypeClassifierTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/EtherTypeClassifierTest.java old mode 100644 new mode 100755 index cdb7ad21d..30170a9ed --- a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/EtherTypeClassifierTest.java +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/EtherTypeClassifierTest.java @@ -1,13 +1,14 @@ package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.Before; +import com.google.common.collect.ImmutableMap; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -18,73 +19,80 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.M import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder; -import com.google.common.collect.ImmutableMap; - public class EtherTypeClassifierTest { @Rule public ExpectedException thrown = ExpectedException.none(); - List matches; - Map params; + @Test + public void testUpdate() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add( + new MatchBuilder().setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.TCP.shortValue()))); + params.putAll( + ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv4)); - @Before - public void setUp() { - matches = new ArrayList<>(); - params = new HashMap<>(); - } + List updated = Classifier.ETHER_TYPE_CL.update(matches, params); - @Test - public void setValueTest() { - matches.add(new MatchBuilder().setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.TCP.shortValue()))); - params.putAll(ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, - FlowUtils.IPv4)); - matches = Classifier.ETHER_TYPE_CL.update(matches, params); - assertEquals(true, - ClassifierTestUtils.IPV4_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType())); - assertEquals(true, ClassifierTestUtils.TCP.equals(matches.get(0).getIpMatch().getIpProtocol().longValue())); + assertEquals(1, updated.size()); + MatchBuilder first = updated.get(0); + assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, first.getEthernetMatch().getEthernetType()); + assertSame(ClassifierTestUtils.TCP, first.getIpMatch().getIpProtocol().longValue()); } @Test - public void overrideByTheSameValueTest() { - matches.add(new MatchBuilder().setEthernetMatch( - ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)).setIpMatch( - ClassifierTestUtils.createIpMatch(ClassifierTestUtils.UDP.shortValue()))); - params.putAll(ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, - FlowUtils.IPv6)); - matches = Classifier.ETHER_TYPE_CL.update(matches, params); - assertEquals(true, - ClassifierTestUtils.IPV6_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType())); - assertEquals(true, ClassifierTestUtils.UDP.equals(matches.get(0).getIpMatch().getIpProtocol().longValue())); + public void testUpdate_overrideBySameValue() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder() + .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)) + .setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.UDP.shortValue()))); + params.putAll( + ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv6)); + + List updated = Classifier.ETHER_TYPE_CL.update(matches, params); + + assertEquals(1, updated.size()); + MatchBuilder first = updated.get(0); + assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, first.getEthernetMatch().getEthernetType()); + assertSame(ClassifierTestUtils.UDP, first.getIpMatch().getIpProtocol().longValue()); } @Test - public void overrideByDifferentValueTest() { - matches.add(new MatchBuilder().setEthernetMatch( - ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)).setIpMatch( - ClassifierTestUtils.createIpMatch(ClassifierTestUtils.SCTP.shortValue()))); - params.putAll(ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, - FlowUtils.IPv6)); + public void testUpdate_overrideByDifferentValue() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder() + .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)) + .setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.SCTP.shortValue()))); + params.putAll( + ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv6)); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Classification conflict detected"); - matches = Classifier.ETHER_TYPE_CL.update(matches, params); + thrown.expectMessage(Classifier.MSG_CLASSIFICATION_CONFLICT_DETECTED); + Classifier.ETHER_TYPE_CL.update(matches, params); } @Test - public void checkPresenceOfRequiredParameters1Test() { + public void testCheckPresenceOfRequiredParameters_EtherTypeMissing() { + Map params = new HashMap<>(); params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.TCP)); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("not specified."); + thrown.expectMessage(Classifier.MSG_NOT_SPECIFIED); Classifier.ETHER_TYPE_CL.checkPresenceOfRequiredParams(params); } @Test - public void checkPresenceOfRequiredParameters2Test() { - params.putAll(ImmutableMap.of(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, - new ParameterValueBuilder().build())); + public void testCheckPresenceOfRequiredParameters_EtherTypeNull() { + Map params = new HashMap<>(); + params.putAll( + ImmutableMap.of(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, new ParameterValueBuilder().build())); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Value of ethertype parameter is not present"); + thrown.expectMessage(Classifier.MSG_PARAMETER_IS_NOT_PRESENT); Classifier.ETHER_TYPE_CL.checkPresenceOfRequiredParams(params); } } diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/IpProtoClassifierTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/IpProtoClassifierTest.java old mode 100644 new mode 100755 index 465557a1a..125934826 --- a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/IpProtoClassifierTest.java +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/IpProtoClassifierTest.java @@ -1,13 +1,14 @@ package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.Before; +import com.google.common.collect.ImmutableMap; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -18,73 +19,79 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.M import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder; -import com.google.common.collect.ImmutableMap; - public class IpProtoClassifierTest { - List matches; - Map params; - @Rule public ExpectedException thrown = ExpectedException.none(); - @Before - public void setUp() { - matches = new ArrayList<>(); - params = new HashMap<>(); - } - @Test - public void setValueTest() { - matches.add(new MatchBuilder().setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); + public void testUpdate() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder() + .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.TCP)); - matches = Classifier.IP_PROTO_CL.update(matches, params); - assertEquals(true, - ClassifierTestUtils.IPV4_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType())); - assertEquals(true, ClassifierTestUtils.TCP.equals(matches.get(0).getIpMatch().getIpProtocol().longValue())); + + List updated = Classifier.IP_PROTO_CL.update(matches, params); + + assertEquals(1, updated.size()); + MatchBuilder first = updated.get(0); + assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, first.getEthernetMatch().getEthernetType()); + assertSame(ClassifierTestUtils.TCP, first.getIpMatch().getIpProtocol().longValue()); } @Test - public void overrideByTheSameValueTest() { - matches.add(new MatchBuilder().setEthernetMatch( - ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)).setIpMatch( - ClassifierTestUtils.createIpMatch(ClassifierTestUtils.UDP.shortValue()))); + public void testUpdate_overrideBySameValue() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder() + .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE)) + .setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.UDP.shortValue()))); params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.UDP)); - matches = Classifier.IP_PROTO_CL.update(matches, params); - assertEquals(true, - ClassifierTestUtils.IPV6_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType())); - assertEquals(true, ClassifierTestUtils.UDP.equals(matches.get(0).getIpMatch().getIpProtocol().longValue())); + + List updated = Classifier.IP_PROTO_CL.update(matches, params); + + assertEquals(1, updated.size()); + MatchBuilder first = updated.get(0); + assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, first.getEthernetMatch().getEthernetType()); + assertSame(ClassifierTestUtils.UDP, first.getIpMatch().getIpProtocol().longValue()); } @Test - public void overrideByDifferentValueTest() { - matches.add(new MatchBuilder().setEthernetMatch( - ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)).setIpMatch( - ClassifierTestUtils.createIpMatch(ClassifierTestUtils.SCTP.shortValue()))); + public void testUpdate_overrideByDifferentValue() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder() + .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE)) + .setIpMatch(ClassifierTestUtils.createIpMatch(ClassifierTestUtils.SCTP.shortValue()))); params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.TCP)); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Classification conflict detected"); - matches = Classifier.IP_PROTO_CL.update(matches, params); + thrown.expectMessage(Classifier.MSG_CLASSIFICATION_CONFLICT_DETECTED); + Classifier.IP_PROTO_CL.update(matches, params); } @Test - public void checkPresenceOfRequiredParameters1Test() { - params.putAll(ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, - FlowUtils.IPv4)); + public void testCheckPresenceOfRequiredParameters_ProtoMissing() { + Map params = new HashMap<>(); + params.putAll( + ClassifierTestUtils.createIntValueParam(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, FlowUtils.IPv4)); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("not specified"); + thrown.expectMessage(Classifier.MSG_NOT_SPECIFIED); Classifier.IP_PROTO_CL.checkPresenceOfRequiredParams(params); } @Test - public void checkPresenceOfRequiredParameters2Test() { - params.putAll(ImmutableMap.of(IpProtoClassifierDefinition.PROTO_PARAM, - new ParameterValueBuilder().build())); + public void testCheckPresenceOfRequiredParameters_ProtoNull() { + Map params = new HashMap<>(); + params.putAll(ImmutableMap.of(IpProtoClassifierDefinition.PROTO_PARAM, new ParameterValueBuilder().build())); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Value of proto parameter is not present"); + thrown.expectMessage(Classifier.MSG_PARAMETER_IS_NOT_PRESENT); Classifier.IP_PROTO_CL.checkPresenceOfRequiredParams(params); } } diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/L4ClassifierTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/L4ClassifierTest.java old mode 100644 new mode 100755 index 22daea5f4..09936a706 --- a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/L4ClassifierTest.java +++ b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/L4ClassifierTest.java @@ -1,6 +1,9 @@ package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.HashMap; @@ -9,8 +12,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.google.common.collect.ImmutableMap; import org.apache.commons.lang3.tuple.Pair; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -26,206 +29,251 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch; import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder; -import com.google.common.collect.ImmutableMap; - public class L4ClassifierTest { - List matches; - - Map params; + private static final long LESSER_RANGE_START = 79L; + private static final long LESSER_RANGE_END = 81L; + private static final long GREATER_RANGE_START = 8079L; + private static final long GREATER_RANGE_END = 8081L; + private static final long SINGLE_PORT = 80L; + private static final int SINGLE_PORT_INT = 80; @Rule public ExpectedException thrown = ExpectedException.none(); - @Before - public void setUp() { - params = new HashMap<>(); - matches = new ArrayList<>(); - } - @Test - public void setTcpSrcPortTest() { - matches.add(new MatchBuilder().setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); - Long sPort = Long.valueOf(80); + public void testUpdate_TcpSrcPort() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder() + .setEthernetMatch( + ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); + Long sPort = SINGLE_PORT; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.TCP)); params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, sPort)); - matches = Classifier.L4_CL.update(matches, params); - assertEquals(true, - ClassifierTestUtils.IPV4_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType())); - TcpMatch match = new TcpMatchBuilder((TcpMatch) matches.get(0).getLayer4Match()).build(); - assertEquals(true, sPort.equals(match.getTcpSourcePort().getValue().longValue())); - assertEquals(true, match.getTcpDestinationPort() == null); + + List updated = Classifier.L4_CL.update(matches, params); + + assertEquals(1, updated.size()); + MatchBuilder first = updated.get(0); + assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, first.getEthernetMatch().getEthernetType()); + TcpMatch match = new TcpMatchBuilder((TcpMatch) first.getLayer4Match()).build(); + assertSame(sPort, match.getTcpSourcePort().getValue().longValue()); + assertNull(match.getTcpDestinationPort()); } @Test - public void setTcpDstPortTest() { - Long dPort = Long.valueOf(80); - matches.add(new MatchBuilder().setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE))); + public void testUpdate_TcpDstPort() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + Long dPort = SINGLE_PORT; + matches.add(new MatchBuilder() + .setEthernetMatch( + ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE))); params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.TCP)); params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort)); - matches = Classifier.L4_CL.update(matches, params); - assertEquals(true, - ClassifierTestUtils.IPV6_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType())); - TcpMatch match = new TcpMatchBuilder((TcpMatch) matches.get(0).getLayer4Match()).build(); - assertEquals(true, dPort.equals(match.getTcpDestinationPort().getValue().longValue())); - assertEquals(true, match.getTcpSourcePort() == null); + + List updated = Classifier.L4_CL.update(matches, params); + + assertEquals(1, updated.size()); + MatchBuilder first = updated.get(0); + assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, first.getEthernetMatch().getEthernetType()); + TcpMatch match = new TcpMatchBuilder((TcpMatch) first.getLayer4Match()).build(); + assertSame(dPort, match.getTcpDestinationPort().getValue().longValue()); + assertNull(match.getTcpSourcePort()); } @Test - public void setTcpSrcPortDstPortRangeTest() { - matches.add(new MatchBuilder().setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); - Long dstRangeStart = Long.valueOf(8079); - Long dstRangeEnd = Long.valueOf(8081); - Long srcPort = Long.valueOf(80); + public void testUpdate_TcpSrcPort_DstPortRange() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder() + .setEthernetMatch( + ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); + Long dstRangeStart = GREATER_RANGE_START; + Long dstRangeEnd = GREATER_RANGE_END; + Long srcPort = SINGLE_PORT; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.TCP)); params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, srcPort)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd)); Classifier.L4_CL.checkPresenceOfRequiredParams(params); - matches = Classifier.L4_CL.update(matches, params); + + List updated = Classifier.L4_CL.update(matches, params); + Set dstPorts = new HashSet<>(); - for (MatchBuilder match : matches) { - assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType())); - assertEquals(true, - Long.valueOf(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpSourcePort().getValue()) - .equals(srcPort)); + for (MatchBuilder match : updated) { + assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, match.getEthernetMatch().getEthernetType()); + assertEquals(srcPort, + Long.valueOf(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpSourcePort().getValue())); dstPorts.add(new TcpMatchBuilder((TcpMatch) match.getLayer4Match()).getTcpDestinationPort() .getValue() .longValue()); } - for (Long i = dstRangeStart; i <= dstRangeEnd; i++) { - assertEquals(true, dstPorts.contains((i))); + for (Long port = dstRangeStart; port <= dstRangeEnd; port++) { + assertTrue(dstPorts.contains((port))); } } @Test - public void overrideDstPortWithTheSameValueTest() { - Long dPort = Long.valueOf(80); - matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(80)).setEthernetMatch( - ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE))); + public void testUpdate_OverrideDstPortWithTheSameValue() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + Long dPort = SINGLE_PORT; + matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(SINGLE_PORT_INT)) + .setEthernetMatch( + ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE))); params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.UDP)); params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort)); - matches = Classifier.L4_CL.update(matches, params); - assertEquals(true, - ClassifierTestUtils.IPV6_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType())); - UdpMatch match = new UdpMatchBuilder((UdpMatch) matches.get(0).getLayer4Match()).build(); - assertEquals(true, dPort.equals(match.getUdpDestinationPort().getValue().longValue())); - assertEquals(true, match.getUdpSourcePort() == null); + + List updated = Classifier.L4_CL.update(matches, params); + + assertEquals(1, updated.size()); + MatchBuilder first = updated.get(0); + assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, first.getEthernetMatch().getEthernetType()); + UdpMatch match = new UdpMatchBuilder((UdpMatch) first.getLayer4Match()).build(); + assertSame(dPort, match.getUdpDestinationPort().getValue().longValue()); + assertNull(match.getUdpSourcePort()); } @Test - public void addUdpSrcPortRangeTest() { - matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(80)).setEthernetMatch( - ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); - Long srcRangeStart = Long.valueOf(8079); - Long srcRangeEnd = Long.valueOf(8081); + public void testUpdate_AddUdpSrcPortRange() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(SINGLE_PORT_INT)) + .setEthernetMatch( + ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); + Long srcRangeStart = GREATER_RANGE_START; + Long srcRangeEnd = GREATER_RANGE_END; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.UDP)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM, srcRangeStart, srcRangeEnd)); - matches = Classifier.L4_CL.update(matches, params); + + List updated = Classifier.L4_CL.update(matches, params); + + assertEquals(3, updated.size()); Set srcPorts = new HashSet<>(); - for (MatchBuilder match : matches) { - assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType())); - assertEquals(true, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort() - .getValue() - .intValue() == 80); - assertEquals(true, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort() - .getValue() - .longValue() == 80); - srcPorts.add(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort() + for (MatchBuilder match : updated) { + assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, match.getEthernetMatch().getEthernetType()); + assertSame(SINGLE_PORT_INT, + new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort().getValue()); + assertEquals(SINGLE_PORT_INT, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort() .getValue() .longValue()); + srcPorts + .add(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort() + .getValue() + .longValue()); } - for (Long i = srcRangeStart; i <= srcRangeEnd; i++) { - assertEquals(true, srcPorts.contains((i))); + for (Long port = srcRangeStart; port <= srcRangeEnd; port++) { + assertTrue(srcPorts.contains((port))); } } @Test - public void setUdpSrcPortRangeDstPortTest() { - matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(80)).setEthernetMatch( - ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE))); - Long dPort = Long.valueOf(80); - Long srcRangeStart = Long.valueOf(8079); - Long srcRangeEnd = Long.valueOf(8081); + public void testUpdate_UdpSrcPortRange_DstPort() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createUdpDstPort(SINGLE_PORT_INT)) + .setEthernetMatch( + ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV6_ETH_TYPE))); + Long dPort = SINGLE_PORT; + Long srcRangeStart = GREATER_RANGE_START; + Long srcRangeEnd = GREATER_RANGE_END; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.UDP)); params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dPort)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM, srcRangeStart, srcRangeEnd)); Classifier.L4_CL.checkPresenceOfRequiredParams(params); - matches = Classifier.L4_CL.update(matches, params); + + List updated = Classifier.L4_CL.update(matches, params); + + assertEquals(3, updated.size()); Set srcPorts = new HashSet<>(); - for (MatchBuilder match : matches) { - assertEquals(true, ClassifierTestUtils.IPV6_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType())); - assertEquals( - true, - dPort.equals(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort() - .getValue() - .longValue())); - srcPorts.add(Long.valueOf(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort() + for (MatchBuilder match : updated) { + assertEquals(ClassifierTestUtils.IPV6_ETH_TYPE, match.getEthernetMatch().getEthernetType()); + assertSame(dPort, new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpDestinationPort() .getValue() - .longValue())); + .longValue()); + srcPorts + .add(new UdpMatchBuilder((UdpMatch) match.getLayer4Match()).getUdpSourcePort() + .getValue() + .longValue()); } - for (Long i = srcRangeStart; i <= srcRangeEnd; i++) { - assertEquals(true, srcPorts.contains((i))); + for (Long port = srcRangeStart; port <= srcRangeEnd; port++) { + assertTrue(srcPorts.contains((port))); } } @Test - public void overrideSrcPortWithTheSameValueTest() { - Long sPort = Long.valueOf(80); + public void testUpdate_OverrideSrcPortWithTheSameValue() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + Long sPort = SINGLE_PORT; matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpSrcPort(sPort.intValue())) .setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.SCTP)); params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, sPort)); - matches = Classifier.L4_CL.update(matches, params); - assertEquals(true, - ClassifierTestUtils.IPV4_ETH_TYPE.equals(matches.get(0).getEthernetMatch().getEthernetType())); - SctpMatch match = new SctpMatchBuilder((SctpMatch) matches.get(0).getLayer4Match()).build(); - assertEquals(true, sPort.equals(match.getSctpSourcePort().getValue().longValue())); - assertEquals(true, match.getSctpDestinationPort() == null); + + List updated = Classifier.L4_CL.update(matches, params); + + assertEquals(1, updated.size()); + MatchBuilder first = updated.get(0); + assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, first.getEthernetMatch().getEthernetType()); + SctpMatch match = new SctpMatchBuilder((SctpMatch) first.getLayer4Match()).build(); + assertSame(sPort, match.getSctpSourcePort().getValue().longValue()); + assertNull(match.getSctpDestinationPort()); } @Test - public void addSctpDstPortRangeTest() { - matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpSrcPort(80)).setEthernetMatch( - ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); - Long dstRangeStart = Long.valueOf(8079); - Long dstRangeEnd = Long.valueOf(8081); + public void testUpdate_AddSctpDstPortRange() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpSrcPort(SINGLE_PORT_INT)) + .setEthernetMatch( + ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); + Long dstRangeStart = GREATER_RANGE_START; + Long dstRangeEnd = GREATER_RANGE_END; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.SCTP)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd)); - matches = Classifier.L4_CL.update(matches, params); + + List updated = Classifier.L4_CL.update(matches, params); + + assertEquals(3, updated.size()); Set dstPorts = new HashSet<>(); - for (MatchBuilder match : matches) { - assertEquals(true, ClassifierTestUtils.IPV4_ETH_TYPE.equals(match.getEthernetMatch().getEthernetType())); - assertEquals(true, new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort() - .getValue() - .intValue() == 80); - dstPorts.add(Long.valueOf(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort() + for (MatchBuilder match : updated) { + assertEquals(ClassifierTestUtils.IPV4_ETH_TYPE, match.getEthernetMatch().getEthernetType()); + assertSame(SINGLE_PORT_INT, + new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort().getValue()); + dstPorts.add(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort() .getValue() - .longValue())); + .longValue()); } - for (Long i = dstRangeStart; i <= dstRangeEnd; i++) { - assertEquals(true, dstPorts.contains((i))); + for (Long port = dstRangeStart; port <= dstRangeEnd; port++) { + assertTrue(dstPorts.contains((port))); } } @Test - public void setSctpSrcPortRangeDstPortRangeTest() { - matches.add(new MatchBuilder().setEthernetMatch(ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); - Long srcRangeStart = Long.valueOf(79); - Long srcRangeEnd = Long.valueOf(81); - Long dstRangeStart = Long.valueOf(8079); - Long dstRangeEnd = Long.valueOf(8081); + public void testUpdate_Sctp_SrcPortRange_DstPortRange() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder() + .setEthernetMatch( + ClassifierTestUtils.createEthernetMatch(ClassifierTestUtils.IPV4_ETH_TYPE))); + Long srcRangeStart = LESSER_RANGE_START; + Long srcRangeEnd = LESSER_RANGE_END; + Long dstRangeStart = GREATER_RANGE_START; + Long dstRangeEnd = GREATER_RANGE_END; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.SCTP)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM, @@ -233,142 +281,162 @@ public class L4ClassifierTest { params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd)); Classifier.L4_CL.checkPresenceOfRequiredParams(params); - matches = Classifier.L4_CL.update(matches, params); + + List updated = Classifier.L4_CL.update(matches, params); + + assertEquals(9, updated.size()); Set> set = new HashSet<>(); - for (MatchBuilder match : matches) { - Long srcPort = Long.valueOf(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort() + for (MatchBuilder match : updated) { + Long srcPort = + new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpSourcePort().getValue().longValue(); + Long dstPort = new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort() .getValue() - .longValue()); - Long dstPort = Long.valueOf(new SctpMatchBuilder((SctpMatch) match.getLayer4Match()).getSctpDestinationPort() - .getValue() - .longValue()); + .longValue(); set.add(Pair.of(srcPort, dstPort)); } - for (Long i = srcRangeStart; i <= srcRangeEnd; i++) { - for (Long j = dstRangeStart; j <= dstRangeEnd; j++) { - assertEquals(true, set.contains(Pair.of(i, j))); + for (Long sPort = srcRangeStart; sPort <= srcRangeEnd; sPort++) { + for (Long dPort = dstRangeStart; dPort <= dstRangeEnd; dPort++) { + assertTrue(set.contains(Pair.of(sPort, dPort))); } } } @Test - public void srcPortSrtPortRangeMutualExclusionTest() { - matches.add(new MatchBuilder()); - Long srcRangeStart = Long.valueOf(8079); - Long srcRangeEnd = Long.valueOf(8081); - Long srcPort = Long.valueOf(80); + public void testCheckPresenceOfRequiredParams_SrcPort_SrtPortRange_MutualExclusion() { + Map params = new HashMap<>(); + Long srcRangeStart = GREATER_RANGE_START; + Long srcRangeEnd = GREATER_RANGE_END; + Long srcPort = SINGLE_PORT; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.SCTP)); params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.SRC_PORT_PARAM, srcPort)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM, srcRangeStart, srcRangeEnd)); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("mutually exclusive"); + thrown.expectMessage(Classifier.MSG_MUTUALLY_EXCLUSIVE); Classifier.L4_CL.checkPresenceOfRequiredParams(params); } @Test - public void dstPortSrtPortRangeMutualExclusionTest() { - matches.add(new MatchBuilder()); - Long dstRangeStart = Long.valueOf(8079); - Long dstRangeEnd = Long.valueOf(8081); - Long dstPort = Long.valueOf(80); + public void testCheckPresenceOfRequiredParams_DstPort_DstPortRange_MutualExclusion() { + Map params = new HashMap<>(); + Long dstRangeStart = GREATER_RANGE_START; + Long dstRangeEnd = GREATER_RANGE_END; + Long dstPort = SINGLE_PORT; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.UDP)); params.putAll(ClassifierTestUtils.createIntValueParam(L4ClassifierDefinition.DST_PORT_PARAM, dstPort)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd)); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("mutually exclusive"); + thrown.expectMessage(Classifier.MSG_MUTUALLY_EXCLUSIVE); Classifier.L4_CL.checkPresenceOfRequiredParams(params); } @Test - public void rangeValueMismatchTest() { - matches.add(new MatchBuilder()); - Long dstRangeStart = Long.valueOf(8081); - Long dstRangeEnd = Long.valueOf(8079); + public void testCheckPresenceOfRequiredParams_RangeValueMismatch() { + Map params = new HashMap<>(); + Long dstRangeStart = GREATER_RANGE_END; + Long dstRangeEnd = GREATER_RANGE_START; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.TCP)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd)); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Range value mismatch"); + thrown.expectMessage(Classifier.MSG_RANGE_VALUE_MISMATCH); Classifier.L4_CL.checkPresenceOfRequiredParams(params); } @Test - public void unsupportedProtocolTest() { + public void testUpdate_UnsupportedProtocol() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); matches.add(new MatchBuilder()); - Long dstRangeStart = Long.valueOf(8079); - Long dstRangeEnd = Long.valueOf(8081); + Long dstRangeStart = GREATER_RANGE_START; + Long dstRangeEnd = GREATER_RANGE_END; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, 136)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd)); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("not supported"); - matches = Classifier.L4_CL.update(matches, params); + thrown.expectMessage(Classifier.MSG_NOT_SUPPORTED); + Classifier.L4_CL.update(matches, params); } @Test - public void classificationConflictTest() { - matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpDstPort(80))); - Long dstRangeStart = Long.valueOf(8079); - Long dstRangeEnd = Long.valueOf(8081); + public void testUpdate_ClassificationConflict() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); + matches.add(new MatchBuilder().setLayer4Match(ClassifierTestUtils.createSctpDstPort(SINGLE_PORT_INT))); + Long dstRangeStart = GREATER_RANGE_START; + Long dstRangeEnd = GREATER_RANGE_END; params.putAll(ClassifierTestUtils.createIntValueParam(IpProtoClassifierDefinition.PROTO_PARAM, ClassifierTestUtils.UDP)); params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd)); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Classification conflict"); - matches = Classifier.L4_CL.update(matches, params); + thrown.expectMessage(Classifier.MSG_CLASSIFICATION_CONFLICT_DETECTED); + Classifier.L4_CL.update(matches, params); } @Test - public void noProtoTest() { + public void testUpdate_NoProto() { + List matches = new ArrayList<>(); + Map params = new HashMap<>(); matches.add(new MatchBuilder()); - Long dstRangeStart = Long.valueOf(8079); - Long dstRangeEnd = Long.valueOf(8081); + Long dstRangeStart = GREATER_RANGE_START; + Long dstRangeEnd = GREATER_RANGE_END; params.putAll(ClassifierTestUtils.createRangeValueParam(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, dstRangeStart, dstRangeEnd)); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage(IpProtoClassifierDefinition.PROTO_PARAM + " is missing"); - matches = Classifier.L4_CL.update(matches, params); + thrown.expectMessage(Classifier.MSG_IS_MISSING); + Classifier.L4_CL.update(matches, params); } @Test - public void checkPresenceOfRequiredParameters1Test() { - params.putAll(ImmutableMap.of(L4ClassifierDefinition.SRC_PORT_PARAM, - new ParameterValueBuilder().build())); + public void testCheckPresenceOfRequiredParameters_SrcPortNotSet() { + Map params = new HashMap<>(); + params.putAll(ImmutableMap.of(L4ClassifierDefinition.SRC_PORT_PARAM, new ParameterValueBuilder().build())); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("not specified"); + thrown.expectMessage(Classifier.MSG_NOT_SPECIFIED); Classifier.L4_CL.checkPresenceOfRequiredParams(params); } @Test - public void checkPresenceOfRequiredParameters2Test() { - params.putAll(ImmutableMap.of(L4ClassifierDefinition.DST_PORT_PARAM, - new ParameterValueBuilder().build())); + public void testCheckPresenceOfRequiredParameters_DstPortNotSet() { + Map params = new HashMap<>(); + params.putAll(ImmutableMap.of(L4ClassifierDefinition.DST_PORT_PARAM, new ParameterValueBuilder().build())); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("not specified"); + thrown.expectMessage(Classifier.MSG_NOT_SPECIFIED); Classifier.L4_CL.checkPresenceOfRequiredParams(params); } @Test - public void checkPresenceOfRequiredParameters3Test() { - params.putAll(ImmutableMap.of(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM, + public void testCheckPresenceOfRequiredParameters_SrcRangeNotSet() { + Map params = new HashMap<>(); + params.putAll(ImmutableMap.of(L4ClassifierDefinition.SRC_PORT_RANGE_PARAM, new ParameterValueBuilder().build())); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("not present"); + thrown.expectMessage(Classifier.MSG_NOT_PRESENT); Classifier.L4_CL.checkPresenceOfRequiredParams(params); } @Test - public void checkPresenceOfRequiredParameters4Test() { - params.putAll(ImmutableMap.of(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, + public void testCheckPresenceOfRequiredParameters_DstRangeNotSet() { + Map params = new HashMap<>(); + params.putAll(ImmutableMap.of(L4ClassifierDefinition.DST_PORT_RANGE_PARAM, new ParameterValueBuilder().build())); + thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("not present"); + thrown.expectMessage(Classifier.MSG_NOT_PRESENT); Classifier.L4_CL.checkPresenceOfRequiredParams(params); } } -- 2.36.6