From bcef86b43e8d62d9b1c0b6a5e7d95e3ce79c0131 Mon Sep 17 00:00:00 2001 From: Konstantin Blagov Date: Wed, 16 Mar 2016 17:41:37 +0100 Subject: [PATCH] GBP core tests improvements Change-Id: Ic932f856bb92059bf9ab033086bbc3baec8f59e5 Signed-off-by: Konstantin Blagov --- groupbasedpolicy/pom.xml | 10 + .../api/StatisticsManager.java | 0 .../dto/ValidationResultBuilder.java | 21 ++ .../statistics/StatisticsManagerImpl.java | 6 +- .../endpoint/EndPointRpcRegistryTest.java | 181 ------------ .../endpoint/EndpointRpcRegistryTest.java | 272 ++++++++++++++++++ .../resolver/EpgKeyDtoTest.java | 72 +++++ .../resolver/PolicyResolverTest.java | 100 +++++++ .../groupbasedpolicy/resolver/PolicyTest.java | 97 ++++++- .../sf/ClassifierInstanceValidatorTest.java | 105 +++++++ .../SubjectFeatureDefinitionProviderTest.java | 74 +++++ .../statistics/StatisticManagerImplTest.java | 250 ++++++++-------- .../test/GbpDataBrokerTest.java | 3 +- .../groupbasedpolicy/util/IidFactoryTest.java | 131 +++++++-- .../util/SingletonTaskTest.java | 67 ++--- 15 files changed, 1026 insertions(+), 363 deletions(-) mode change 100644 => 100755 groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/api/StatisticsManager.java mode change 100644 => 100755 groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/dto/ValidationResultBuilder.java delete mode 100644 groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndPointRpcRegistryTest.java create mode 100755 groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndpointRpcRegistryTest.java create mode 100755 groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/EpgKeyDtoTest.java create mode 100755 groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyResolverTest.java mode change 100644 => 100755 groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyTest.java create mode 100755 groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/sf/ClassifierInstanceValidatorTest.java create mode 100755 groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/sf/SubjectFeatureDefinitionProviderTest.java mode change 100644 => 100755 groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/test/GbpDataBrokerTest.java mode change 100644 => 100755 groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/util/IidFactoryTest.java mode change 100644 => 100755 groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/util/SingletonTaskTest.java diff --git a/groupbasedpolicy/pom.xml b/groupbasedpolicy/pom.xml index 075ac46e3..258fb435b 100755 --- a/groupbasedpolicy/pom.xml +++ b/groupbasedpolicy/pom.xml @@ -41,6 +41,16 @@ mockito-all test + + org.powermock + powermock-module-junit4 + test + + + org.powermock + powermock-api-mockito + test + org.opendaylight.controller sal-binding-broker-impl diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/api/StatisticsManager.java b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/api/StatisticsManager.java old mode 100644 new mode 100755 diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/dto/ValidationResultBuilder.java b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/dto/ValidationResultBuilder.java old mode 100644 new mode 100755 index d59fbc27a..75237fe49 --- a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/dto/ValidationResultBuilder.java +++ b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/dto/ValidationResultBuilder.java @@ -34,6 +34,27 @@ public final class ValidationResultBuilder implements Builder public String getMessage() { return message; } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + ValidationResultImpl that = (ValidationResultImpl) o; + + if (success != that.success) + return false; + return message.equals(that.message); + } + + @Override + public int hashCode() { + int result = (success ? 1 : 0); + result = 31 * result + message.hashCode(); + return result; + } } private boolean success; diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/statistics/StatisticsManagerImpl.java b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/statistics/StatisticsManagerImpl.java index eebbfe5ed..c009770b9 100755 --- a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/statistics/StatisticsManagerImpl.java +++ b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/statistics/StatisticsManagerImpl.java @@ -13,6 +13,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.concurrent.ExecutionException; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; @@ -128,8 +129,9 @@ public class StatisticsManagerImpl implements StatisticsManager, AutoCloseable { Optional storeOpt = Optional.absent(); try { storeOpt = rtx.read(LogicalDatastoreType.OPERATIONAL, statsIID).get(); - } catch (Exception e) { - e.printStackTrace(); + } catch (InterruptedException | ExecutionException e) { + LOG.error("Exception thrown on reading from Datastore: {}", e.getMessage()); + return null; } if (storeOpt.isPresent()) { StatisticsStore store = storeOpt.get(); diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndPointRpcRegistryTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndPointRpcRegistryTest.java deleted file mode 100644 index fd4760a5d..000000000 --- a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndPointRpcRegistryTest.java +++ /dev/null @@ -1,181 +0,0 @@ -package org.opendaylight.groupbasedpolicy.endpoint; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.atLeast; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; -import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration; -import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; -import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentation; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix; -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.groupbasedpolicy.common.rev140421.EndpointGroupId; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.SetEndpointGroupConditionsInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnsetEndpointGroupConditionsInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.has.endpoint.group.conditions.EndpointGroupCondition; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L2; -import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.binding.RpcService; - -import com.google.common.util.concurrent.CheckedFuture; - -public class EndPointRpcRegistryTest { - - private DataBroker dataProvider; - private RpcProviderRegistry rpcRegistry; - private RpcRegistration rpcRegistration; - private WriteTransaction t; - private EpRendererAugmentation epRendererAugmentation; - - private EndpointRpcRegistry endpointRpcRegistry; - - @SuppressWarnings("unchecked") - @Before - public void initialisation() { - dataProvider = mock(DataBroker.class); - rpcRegistry = mock(RpcProviderRegistry.class); - epRendererAugmentation = mock(EpRendererAugmentation.class); - - t = mock(WriteTransaction.class); - when(dataProvider.newWriteOnlyTransaction()).thenReturn(t); - CheckedFuture f = mock(CheckedFuture.class); - when(t.submit()).thenReturn(f); - - rpcRegistration = mock(RpcRegistration.class); - when(rpcRegistry.addRpcImplementation(any(Class.class), any(RpcService.class))).thenReturn(rpcRegistration); - - endpointRpcRegistry = new EndpointRpcRegistry(dataProvider, rpcRegistry); - } - - @SuppressWarnings("unchecked") - @Test - public void registerTest() throws Exception { - verify(rpcRegistry).addRpcImplementation(any(Class.class), any(RpcService.class)); - verify(t).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Endpoints.class)); - endpointRpcRegistry.register(epRendererAugmentation); - Assert.assertEquals(1, EndpointRpcRegistry.registeredRenderers.size()); - - endpointRpcRegistry.unregister(epRendererAugmentation); - Assert.assertEquals(0, EndpointRpcRegistry.registeredRenderers.size()); - } - - @Test - public void registerTestSafelyFail() { - endpointRpcRegistry.register(epRendererAugmentation); - endpointRpcRegistry.register(null); - Assert.assertEquals(1, EndpointRpcRegistry.registeredRenderers.size()); - } - - @Test - public void registerEndpointTest()throws Exception{ - endpointRpcRegistry.registerEndpoint(setRegisterEndpointVariablesForTest()); - verify(t, times(1)).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(DataObject.class), eq(true)); - - endpointRpcRegistry.registerEndpoint(setL3AddressVariableForTest()); - verify(t, times(2)).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(DataObject.class), eq(true)); - } - - private RegisterEndpointInput setRegisterEndpointVariablesForTest() throws Exception{ - RegisterEndpointInputBuilder registerEndpointInputBuilder = new RegisterEndpointInputBuilder(); - registerEndpointInputBuilder.setL2Context(new L2BridgeDomainId("l2bdId")); - registerEndpointInputBuilder.setMacAddress(new MacAddress("5E:83:39:98:4F:1B")); - return registerEndpointInputBuilder.build(); - } - - private RegisterEndpointInput setL3AddressVariableForTest() { - RegisterEndpointInputBuilder registerEndpointInputBuilder = new RegisterEndpointInputBuilder(); - List l3AddressList = new ArrayList<>(); - l3AddressList.add(new L3AddressBuilder().setL3Context(new L3ContextId("l3c")) - .setIpAddress(new IpAddress(new Ipv4Address("1.1.1.1"))) - .build()); - registerEndpointInputBuilder.setL3Address(l3AddressList); - return registerEndpointInputBuilder.build(); - } - - @Test - public void registerL3PrefixEndpointTest()throws Exception{ - endpointRpcRegistry.registerL3PrefixEndpoint(setL3PrefixTestVariables()); - verify(t, atLeast(1)).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(DataObject.class)); - } - - private RegisterL3PrefixEndpointInput setL3PrefixTestVariables(){ - RegisterL3PrefixEndpointInputBuilder registerL3PrefixEndpointInputBuilder = new RegisterL3PrefixEndpointInputBuilder(); - registerL3PrefixEndpointInputBuilder.setL3Context(new L3ContextId("l3c")); - registerL3PrefixEndpointInputBuilder.setIpPrefix(new IpPrefix(new Ipv4Prefix("1.1.1.0/24"))); - registerL3PrefixEndpointInputBuilder.setTenant(new TenantId("t1")); - return registerL3PrefixEndpointInputBuilder.build(); - } - - @Test - public void unregisterEndpointTest() throws Exception{ - L2 l2Mock = mock(L2.class); - List l2List = new ArrayList<>(); - l2List.add(l2Mock); - L3 l3Mock = mock(L3.class); - List l3List = new ArrayList<>(); - l3List.add(l3Mock); - UnregisterEndpointInput unregisterEndpointInputMock = mock(UnregisterEndpointInput.class); - doReturn(l2List).when(unregisterEndpointInputMock).getL2(); - doReturn(l3List).when(unregisterEndpointInputMock).getL3(); - endpointRpcRegistry.unregisterEndpoint(unregisterEndpointInputMock); - verify(t, times(2)).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class)); - } - - @Test - public void setEndpointGroupConditionsTest()throws Exception{ - EndpointGroupId endpointGroupIdMock = mock(EndpointGroupId.class); - - EndpointGroupCondition endpointGroupConditionMock = mock(EndpointGroupCondition.class); - List endpointGroupConditionList = new ArrayList(); - endpointGroupConditionList.add(endpointGroupConditionMock); - SetEndpointGroupConditionsInput setEndpointGroupConditionsInputMock = mock(SetEndpointGroupConditionsInput.class); - doReturn(endpointGroupIdMock).when(setEndpointGroupConditionsInputMock).getEndpointGroup(); - doReturn(endpointGroupConditionList).when(setEndpointGroupConditionsInputMock).getEndpointGroupCondition(); - endpointRpcRegistry.setEndpointGroupConditions(setEndpointGroupConditionsInputMock); - verify(t, atLeast(1)).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(EndpointGroupCondition.class)); - } - - @Test - public void unsetEndpointGroupConditionsTest()throws Exception{ - UnsetEndpointGroupConditionsInput unsetEndpointGroupConditionsInputMock = mock(UnsetEndpointGroupConditionsInput.class); - EndpointGroupCondition endpointGroupConditionMock = mock(EndpointGroupCondition.class); - List endpointGroupConditionList = new ArrayList(); - endpointGroupConditionList.add(endpointGroupConditionMock); - doReturn(mock(EndpointGroupId.class)).when(unsetEndpointGroupConditionsInputMock).getEndpointGroup(); - doReturn(endpointGroupConditionList).when(unsetEndpointGroupConditionsInputMock).getEndpointGroupCondition(); - endpointRpcRegistry.unsetEndpointGroupConditions(unsetEndpointGroupConditionsInputMock); - verify(t, times(1)).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class)); - } -} diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndpointRpcRegistryTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndpointRpcRegistryTest.java new file mode 100755 index 000000000..c347df183 --- /dev/null +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndpointRpcRegistryTest.java @@ -0,0 +1,272 @@ +package org.opendaylight.groupbasedpolicy.endpoint; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import com.google.common.util.concurrent.CheckedFuture; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentation; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix; +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.groupbasedpolicy.common.rev140421.ConditionName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.SetEndpointGroupConditionsInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.SetEndpointGroupConditionsInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnsetEndpointGroupConditionsInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnsetEndpointGroupConditionsInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.has.endpoint.group.conditions.EndpointGroupCondition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.has.endpoint.group.conditions.EndpointGroupConditionBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L2; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L2Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3Prefix; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3PrefixBuilder; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class EndpointRpcRegistryTest { + + private static final String L2_BD_ID = "l2bdId"; + private static final String MAC_ADDRESS = "5E:83:39:98:4F:1B"; + private static final String L3_CX_ID = "l3c"; + private static final String IPv4_ADDRESS = "1.1.1.1"; + private static final String IPv4_PREFIX = "1.1.1.0/24"; + private static final String TENANT_ID = "t1"; + private static final String EPG_ID = "epg_1"; + private static final String CONDITION_NAME = "condition_1"; + + private DataBroker dataProvider; + private EpRendererAugmentation epRendererAugmentation; + private EndpointRpcRegistry endpointRpcRegistry; + + @Before + public void init() { + dataProvider = mock(DataBroker.class); + epRendererAugmentation = mock(EpRendererAugmentation.class); + WriteTransaction wt = newWriteTransactionMock(); + RpcProviderRegistry rpcRegistry = mock(RpcProviderRegistry.class); + + endpointRpcRegistry = new EndpointRpcRegistry(dataProvider, rpcRegistry); + } + + @Test + public void testConstructor() throws Exception { + RpcProviderRegistry rpcRegistry = mock(RpcProviderRegistry.class); + EndpointRpcRegistry other = new EndpointRpcRegistry(dataProvider, rpcRegistry); + other.close(); + } + + @Test + public void testRegister() throws Exception { + WriteTransaction wt = newWriteTransactionMock(); + + endpointRpcRegistry.register(epRendererAugmentation); + Assert.assertEquals(1, EndpointRpcRegistry.registeredRenderers.size()); + + endpointRpcRegistry.unregister(epRendererAugmentation); + Assert.assertEquals(0, EndpointRpcRegistry.registeredRenderers.size()); + } + + @Test + public void testRegister_SafelyFail() { + endpointRpcRegistry.register(epRendererAugmentation); + endpointRpcRegistry.register(null); + + Assert.assertEquals(1, EndpointRpcRegistry.registeredRenderers.size()); + } + + @Test + public void testRegisterEndpoint() throws Exception { + WriteTransaction wt = newWriteTransactionMock(); + + endpointRpcRegistry.registerEndpoint(setRegisterEndpointVariablesForTest()); + + verify(wt, times(1)).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), + any(DataObject.class), eq(true)); + + endpointRpcRegistry.registerEndpoint(setL3AddressVariableForTest()); + + verify(wt, times(2)).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), + any(DataObject.class), eq(true)); + } + + private RegisterEndpointInput setRegisterEndpointVariablesForTest() throws Exception { + RegisterEndpointInputBuilder registerEndpointInputBuilder = new RegisterEndpointInputBuilder(); + registerEndpointInputBuilder.setL2Context(new L2BridgeDomainId(L2_BD_ID)); + registerEndpointInputBuilder.setMacAddress(new MacAddress(MAC_ADDRESS)); + return registerEndpointInputBuilder.build(); + } + + private RegisterEndpointInput setL3AddressVariableForTest() { + RegisterEndpointInputBuilder registerEndpointInputBuilder = new RegisterEndpointInputBuilder(); + List l3AddressList = new ArrayList<>(); + l3AddressList.add(new L3AddressBuilder().setL3Context(new L3ContextId(L3_CX_ID)) + .setIpAddress(new IpAddress(new Ipv4Address(IPv4_ADDRESS))) + .build()); + registerEndpointInputBuilder.setL3Address(l3AddressList); + return registerEndpointInputBuilder.build(); + } + + @Test + public void testRegisterL3PrefixEndpoint() throws Exception { + WriteTransaction wt = newWriteTransactionMock(); + + endpointRpcRegistry.registerL3PrefixEndpoint(setL3PrefixTestVariables()); + + verify(wt, times(1)).put(eq(LogicalDatastoreType.OPERATIONAL), + Mockito.>any(), any(EndpointL3Prefix.class)); + } + + @Test + public void testRegisterL3PrefixEndpoint_BadData() { + WriteTransaction wt = newWriteTransactionMock(); + + endpointRpcRegistry.registerL3PrefixEndpoint(setL3PrefixTestVariables_NoIpPrefix()); + endpointRpcRegistry.registerL3PrefixEndpoint(setL3PrefixTestVariables_NoL3Context()); + endpointRpcRegistry.registerL3PrefixEndpoint(setL3PrefixTestVariables_NoTenant()); + + verify(wt, never()).put(eq(LogicalDatastoreType.OPERATIONAL), + Mockito.>any(), any(EndpointL3Prefix.class)); + } + + private RegisterL3PrefixEndpointInput setL3PrefixTestVariables() { + RegisterL3PrefixEndpointInputBuilder registerL3PrefixEndpointInputBuilder = + new RegisterL3PrefixEndpointInputBuilder(); + registerL3PrefixEndpointInputBuilder.setL3Context(new L3ContextId(L3_CX_ID)); + registerL3PrefixEndpointInputBuilder.setIpPrefix(new IpPrefix(new Ipv4Prefix(IPv4_PREFIX))); + registerL3PrefixEndpointInputBuilder.setTenant(new TenantId(TENANT_ID)); + return registerL3PrefixEndpointInputBuilder.build(); + } + + private RegisterL3PrefixEndpointInput setL3PrefixTestVariables_NoL3Context() { + RegisterL3PrefixEndpointInputBuilder registerL3PrefixEndpointInputBuilder = + new RegisterL3PrefixEndpointInputBuilder(); + registerL3PrefixEndpointInputBuilder.setIpPrefix(new IpPrefix(new Ipv4Prefix(IPv4_PREFIX))); + registerL3PrefixEndpointInputBuilder.setTenant(new TenantId(TENANT_ID)); + return registerL3PrefixEndpointInputBuilder.build(); + } + + private RegisterL3PrefixEndpointInput setL3PrefixTestVariables_NoIpPrefix() { + RegisterL3PrefixEndpointInputBuilder registerL3PrefixEndpointInputBuilder = + new RegisterL3PrefixEndpointInputBuilder(); + registerL3PrefixEndpointInputBuilder.setL3Context(new L3ContextId(L3_CX_ID)); + registerL3PrefixEndpointInputBuilder.setTenant(new TenantId(TENANT_ID)); + return registerL3PrefixEndpointInputBuilder.build(); + } + + private RegisterL3PrefixEndpointInput setL3PrefixTestVariables_NoTenant() { + RegisterL3PrefixEndpointInputBuilder registerL3PrefixEndpointInputBuilder = + new RegisterL3PrefixEndpointInputBuilder(); + registerL3PrefixEndpointInputBuilder.setL3Context(new L3ContextId(L3_CX_ID)); + registerL3PrefixEndpointInputBuilder.setIpPrefix(new IpPrefix(new Ipv4Prefix(IPv4_PREFIX))); + return registerL3PrefixEndpointInputBuilder.build(); + } + + @Test + public void testUnregisterEndpoint() throws Exception { + WriteTransaction wt = newWriteTransactionMock(); + + UnregisterEndpointInput unregisterEndpointInput = unregisterEndpointInput(); + + endpointRpcRegistry.unregisterEndpoint(unregisterEndpointInput); + + verify(wt, times(2)).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class)); + } + + private UnregisterEndpointInput unregisterEndpointInput() { + L2 l2 = new L2Builder().setL2Context(new L2BridgeDomainId(L2_BD_ID)) + .setMacAddress(new MacAddress(MAC_ADDRESS)) + .build(); + List l2List = new ArrayList<>(); + l2List.add(l2); + L3 l3 = new L3Builder().setL3Context(new L3ContextId(L3_CX_ID)) + .setIpAddress(new IpAddress(new Ipv4Address(IPv4_ADDRESS))) + .build(); + List l3List = new ArrayList<>(); + l3List.add(l3); + List l3PrefixList = new ArrayList<>(); + l3PrefixList.add(new L3PrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix(IPv4_PREFIX))).build()); + return new UnregisterEndpointInputBuilder().setL2(l2List).setL3(l3List).setL3Prefix(l3PrefixList).build(); + } + + @Test + public void testSetEndpointGroupConditions() throws Exception { + WriteTransaction wt = newWriteTransactionMock(); + + EndpointGroupId endpointGroupId = new EndpointGroupId(EPG_ID); + EndpointGroupCondition endpointGroupCondition = + new EndpointGroupConditionBuilder().setCondition(new ConditionName(CONDITION_NAME)).build(); + List endpointGroupConditionList = new ArrayList<>(); + endpointGroupConditionList.add(endpointGroupCondition); + + SetEndpointGroupConditionsInput setEndpointGroupConditionsInput = new SetEndpointGroupConditionsInputBuilder() + .setEndpointGroup(endpointGroupId).setEndpointGroupCondition(endpointGroupConditionList).build(); + + endpointRpcRegistry.setEndpointGroupConditions(setEndpointGroupConditionsInput); + + verify(wt, times(1)).put(eq(LogicalDatastoreType.OPERATIONAL), + Mockito.>any(), any(EndpointGroupCondition.class)); + } + + @Test + public void testUnsetEndpointGroupConditions() throws Exception { + WriteTransaction wt = newWriteTransactionMock(); + + EndpointGroupId endpointGroupId = new EndpointGroupId(EPG_ID); + EndpointGroupCondition endpointGroupCondition = + new EndpointGroupConditionBuilder().setCondition(new ConditionName(CONDITION_NAME)).build(); + List endpointGroupConditionList = new ArrayList<>(); + endpointGroupConditionList.add(endpointGroupCondition); + + UnsetEndpointGroupConditionsInput unsetEndpointGroupConditionsInput = + new UnsetEndpointGroupConditionsInputBuilder().setEndpointGroup(endpointGroupId) + .setEndpointGroupCondition(endpointGroupConditionList) + .build(); + + endpointRpcRegistry.unsetEndpointGroupConditions(unsetEndpointGroupConditionsInput); + + verify(wt, times(1)).delete(eq(LogicalDatastoreType.OPERATIONAL), + Mockito.>any()); + } + + private WriteTransaction newWriteTransactionMock() { + WriteTransaction wt = mock(WriteTransaction.class); + CheckedFuture f = mock(CheckedFuture.class); + + when(dataProvider.newWriteOnlyTransaction()).thenReturn(wt); + when(wt.submit()).thenReturn(f); + return wt; + } +} diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/EpgKeyDtoTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/EpgKeyDtoTest.java new file mode 100755 index 000000000..4604f698a --- /dev/null +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/EpgKeyDtoTest.java @@ -0,0 +1,72 @@ +package org.opendaylight.groupbasedpolicy.resolver; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.opendaylight.groupbasedpolicy.dto.EpgKeyDto; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId; + +public class EpgKeyDtoTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private EndpointGroupId endpointGroupId; + private TenantId tenantId; + EpgKeyDto epgKeyDto; + + @Before + public void init() { + endpointGroupId = new EndpointGroupId("epg-1"); + tenantId = new TenantId("tenant-1"); + + epgKeyDto = new EpgKeyDto(endpointGroupId, tenantId); + } + + @Test + public void testConstructor() { + new EpgKeyDto(endpointGroupId, tenantId); + } + + @Test + public void testConstructor_EpgIdIsNull() throws NullPointerException { + thrown.expect(NullPointerException.class); + new EpgKeyDto(null, tenantId); + } + + @Test + public void testConstructor_TenantIdIsNull() throws NullPointerException { + thrown.expect(NullPointerException.class); + new EpgKeyDto(endpointGroupId, null); + } + + @Test + public void testGetEpgId() { + assertEquals(endpointGroupId.getValue(), epgKeyDto.getEpgId().getValue()); + } + + @Test + public void testGetTenantId() { + assertEquals(tenantId.getValue(), epgKeyDto.getTenantId().getValue()); + } + + @Test + public void testHashCode() { + EpgKeyDto epgKeyDto2 = new EpgKeyDto(new EndpointGroupId("epg-1"), new TenantId("tenant-1")); + + assertEquals(epgKeyDto.hashCode(), epgKeyDto2.hashCode()); + + } + + @Test + public void testEquals() { + EpgKeyDto epgKeyDto2 = new EpgKeyDto(new EndpointGroupId("epg-1"), new TenantId("tenant-1")); + + assertEquals(epgKeyDto, epgKeyDto2); + } + +} diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyResolverTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyResolverTest.java new file mode 100755 index 000000000..bb8b88f8d --- /dev/null +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyResolverTest.java @@ -0,0 +1,100 @@ +package org.opendaylight.groupbasedpolicy.resolver; + +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.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.base.Optional; +import org.junit.After; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.groupbasedpolicy.test.GbpDataBrokerTest; +import org.opendaylight.groupbasedpolicy.util.DataStoreHelper; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(DataStoreHelper.class) +public class PolicyResolverTest extends GbpDataBrokerTest { + + private DataBroker dataProvider; + private PolicyResolver policyResolver; + private final TenantId tenantId = new TenantId("tenant-1"); + + @Before + public void init() { + dataProvider = getDataBroker(); + policyResolver = new PolicyResolver(dataProvider); + } + + @After + public void teardown() throws Exception { + policyResolver.close(); + } + + @Test + public void testConstructor() throws Exception { + PolicyResolver other = new PolicyResolver(dataProvider); + other.close(); + } + + @Test + public void testSubscribeTenant_Unknown() { + int oldSize = policyResolver.subscribersPerTenant.count(tenantId); + Assume.assumeTrue(oldSize == 0); + + policyResolver.subscribeTenant(tenantId); + + assertEquals(policyResolver.subscribersPerTenant.count(tenantId), oldSize + 1); + } + + @Test + public void testSubscribeTenant_Known() { + Tenant unresolvedTenant = new TenantBuilder().setId(tenantId).build(); + + Optional potentialTenant = mock(Optional.class); + when(potentialTenant.isPresent()).thenReturn(true); + when(potentialTenant.get()).thenReturn(unresolvedTenant); + + PowerMockito.mockStatic(DataStoreHelper.class); + when(DataStoreHelper.readFromDs(eq(LogicalDatastoreType.CONFIGURATION), + Mockito.>any(), any(ReadOnlyTransaction.class))).thenReturn(potentialTenant); + + PolicyResolver spy = spy(policyResolver); + + int oldSize = spy.subscribersPerTenant.count(tenantId); + + spy.subscribeTenant(tenantId); + + assertEquals(spy.subscribersPerTenant.count(tenantId), oldSize + 1); + verify(spy).updateTenant(eq(tenantId), any(Tenant.class)); + } + + @Test + public void testUnsubscribeTenant() { + int oldSize = policyResolver.subscribersPerTenant.count(tenantId); + Assume.assumeTrue(oldSize == 0); + + policyResolver.subscribeTenant(tenantId); + assertEquals(policyResolver.subscribersPerTenant.count(tenantId), oldSize + 1); + + policyResolver.unsubscribeTenant(tenantId); + assertEquals(policyResolver.subscribersPerTenant.count(tenantId), oldSize); + } + +} diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyTest.java old mode 100644 new mode 100755 index 2572ffe39..37eed708e --- a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyTest.java +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyTest.java @@ -1,31 +1,114 @@ package org.opendaylight.groupbasedpolicy.resolver; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.Table; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.opendaylight.groupbasedpolicy.dto.ConditionGroup; +import org.opendaylight.groupbasedpolicy.dto.ConditionSet; +import org.opendaylight.groupbasedpolicy.dto.EndpointConstraint; import org.opendaylight.groupbasedpolicy.dto.Policy; +import org.opendaylight.groupbasedpolicy.dto.RuleGroup; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefixBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.l3.endpoint.identification.constraints.PrefixConstraint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.l3.endpoint.identification.constraints.PrefixConstraintBuilder; public class PolicyTest { private Policy policy; @Before - public void initialisation() { + public void init() { policy = new Policy(null); } @Test - public void equalsTest() { - Assert.assertTrue(policy.equals(policy)); - Assert.assertFalse(policy.equals(null)); - Assert.assertFalse(policy.equals(new Object())); + public void testGetRules() { + ConditionSet conditionSet = newConditionSet(); + Set conditionSets = new HashSet<>(); + conditionSets.add(conditionSet); + + ConditionGroup fromCg = new ConditionGroup(conditionSets); + ConditionGroup toCg = new ConditionGroup(conditionSets); + + Set conditionSetsEmpty = new HashSet<>(); + ConditionGroup fromCgEmpty = new ConditionGroup(conditionSetsEmpty); + ConditionGroup toCgEmpty = new ConditionGroup(conditionSetsEmpty); + + EndpointConstraint endpointConstraint = new EndpointConstraint(conditionSet, null); + + List ruleGroups = new ArrayList<>(); + ruleGroups.add(mock(RuleGroup.class)); + + Table> ruleMap = HashBasedTable.create(1, 1); + ruleMap.put(endpointConstraint, endpointConstraint, ruleGroups); + + Policy p = new Policy(ruleMap); + + assertTrue(p.getRules(fromCg, toCg).size() == 1); + assertTrue(p.getRules(fromCg, toCgEmpty).isEmpty()); + assertTrue(p.getRules(fromCgEmpty, toCg).isEmpty()); + assertTrue(p.getRules(fromCgEmpty, toCgEmpty).isEmpty()); + } + + @Test + public void testGetIpPrefixesFrom() { + Set prefixConstraints = new HashSet<>(); + final IpPrefix ipPrefix1 = IpPrefixBuilder.getDefaultInstance("10.1.0.1/24"); + prefixConstraints.add(new PrefixConstraintBuilder().setIpPrefix(ipPrefix1).build()); + final IpPrefix ipPrefix2 = IpPrefixBuilder.getDefaultInstance("10.2.0.1/24"); + prefixConstraints.add(new PrefixConstraintBuilder().setIpPrefix(ipPrefix2).build()); + Set ipPrefixes = Policy.getIpPrefixesFrom(prefixConstraints); + + assertTrue(ipPrefixes.size() == 2); + assertTrue(ipPrefixes.contains(ipPrefix1)); + assertTrue(ipPrefixes.contains(ipPrefix2)); + } + + @Test + public void testEquals() { Policy other = new Policy(null); - Assert.assertTrue(policy.equals(other)); + + assertTrue(policy.equals(other)); } @Test - public void toStringTest() { + public void testToString() { Assert.assertNotNull(policy.toString()); } + + private ConditionSet newConditionSet(){ + Set all = new HashSet<>(); + Set none = new HashSet<>(); + Set> any = new HashSet<>(); + Set any1 = new HashSet<>(); + Set any2 = new HashSet<>(); + + all.add(new ConditionName("condition-all-1")); + all.add(new ConditionName("condition-all-2")); + + none.add(new ConditionName("condition-none-1")); + none.add(new ConditionName("condition-none-2")); + + any1.add(new ConditionName("condition-any1-1")); + any1.add(new ConditionName("condition-any1-2")); + any2.add(new ConditionName("condition-any2-1")); + any2.add(new ConditionName("condition-any2-2")); + any.add(any1); + any.add(any2); + + return new ConditionSet(all, none, any); + } } diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/sf/ClassifierInstanceValidatorTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/sf/ClassifierInstanceValidatorTest.java new file mode 100755 index 000000000..e91d5cb8c --- /dev/null +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/sf/ClassifierInstanceValidatorTest.java @@ -0,0 +1,105 @@ +package org.opendaylight.groupbasedpolicy.sf; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import com.google.common.collect.ImmutableList; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.groupbasedpolicy.dto.ValidationResultBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierDefinitionId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstanceBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.RendererName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.has.parameters.type.parameter.type.IntBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.capabilities.SupportedClassifierDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.capabilities.SupportedClassifierDefinitionBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.capabilities.supported.classifier.definition.SupportedParameterValues; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.capabilities.supported.classifier.definition.SupportedParameterValuesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.supported._int.value.fields.SupportedIntValueInRangeBuilder; + +public class ClassifierInstanceValidatorTest { + + private static final String RENDERER = "renderer"; + private static final String PARENT_CLASSIFIER_DEFINITION_ID = "cdId-0"; + private static final String CLASSIFIER_DEFINITION_ID = "cdId-1"; + private static final String PARAM_NAME = "param_name"; + private static final String CLASSIFIER1 = "classifier1"; + private ClassifierDefinitionId cdId; + private ClassifierDefinitionId parentCdId; + private List supportedParameterValues; + + @Before + public void init() { + cdId = new ClassifierDefinitionId(CLASSIFIER_DEFINITION_ID); + parentCdId = new ClassifierDefinitionId(PARENT_CLASSIFIER_DEFINITION_ID); + supportedParameterValues = + ImmutableList.of( + new SupportedParameterValuesBuilder().setParameterName(new ParameterName(PARAM_NAME)) + .setParameterType(new IntBuilder() + .setSupportedIntValueInRange(ImmutableList + .of(new SupportedIntValueInRangeBuilder().setMin(0l).setMax(1000l).build())) + .build()) + .build()); + } + + @Test + public void testConstructor() { + + SupportedClassifierDefinition constraint = new SupportedClassifierDefinitionBuilder() + .setClassifierDefinitionId(cdId) + .setParentClassifierDefinitionId(parentCdId) + .setSupportedParameterValues(supportedParameterValues) + .build(); + RendererName rendererName = new RendererName(RENDERER); + + ClassifierInstanceValidator validator = new ClassifierInstanceValidator(constraint, rendererName); + + assertEquals(CLASSIFIER_DEFINITION_ID, validator.getClassifierDefinitionId().getValue()); + assertNotNull(validator.getParentClassifierDefinitionId()); + assertEquals(PARENT_CLASSIFIER_DEFINITION_ID, validator.getParentClassifierDefinitionId().getValue()); + assertEquals(RENDERER, validator.getRendererName().getValue()); + assertEquals(supportedParameterValues.size(), validator.getSupportedParameters().size()); + } + + @Test + public void testConstructor_noSupportedParameters() { + + SupportedClassifierDefinition constraint = new SupportedClassifierDefinitionBuilder() + .setClassifierDefinitionId(cdId).setParentClassifierDefinitionId(parentCdId).build(); + RendererName rendererName = new RendererName(RENDERER); + + ClassifierInstanceValidator validator = new ClassifierInstanceValidator(constraint, rendererName); + + assertEquals(CLASSIFIER_DEFINITION_ID, validator.getClassifierDefinitionId().getValue()); + assertNotNull(validator.getParentClassifierDefinitionId()); + assertEquals(PARENT_CLASSIFIER_DEFINITION_ID, validator.getParentClassifierDefinitionId().getValue()); + assertEquals(RENDERER, validator.getRendererName().getValue()); + assertTrue(validator.getSupportedParameters().isEmpty()); + } + + @Test + public void testValidate() { + ClassifierInstanceBuilder ciBuilder = new ClassifierInstanceBuilder(); + ciBuilder.setName(new ClassifierName(CLASSIFIER1)); + ciBuilder.setParameterValue(ImmutableList + .of(new ParameterValueBuilder().setName(new ParameterName(PARAM_NAME)).setIntValue(100l).build())); + + SupportedClassifierDefinition constraint = new SupportedClassifierDefinitionBuilder() + .setClassifierDefinitionId(cdId) + .setParentClassifierDefinitionId(parentCdId) + .setSupportedParameterValues(supportedParameterValues) + .build(); + RendererName rendererName = new RendererName(RENDERER); + + ClassifierInstanceValidator validator = new ClassifierInstanceValidator(constraint, rendererName); + + assertEquals(new ValidationResultBuilder().success().build(), validator.validate(ciBuilder.build())); + } + +} diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/sf/SubjectFeatureDefinitionProviderTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/sf/SubjectFeatureDefinitionProviderTest.java new file mode 100755 index 000000000..4bc483abb --- /dev/null +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/sf/SubjectFeatureDefinitionProviderTest.java @@ -0,0 +1,74 @@ +package org.opendaylight.groupbasedpolicy.sf; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.util.concurrent.CheckedFuture; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.SubjectFeatureDefinitions; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +@SuppressWarnings({"unchecked"}) +@RunWith(MockitoJUnitRunner.class) +public class SubjectFeatureDefinitionProviderTest { + + public static final int NUM_OF_SUBJECT_FEATURE_DEFINITIONS = 5; + + @Mock + DataBroker dataProvider; + @Mock + WriteTransaction writeTransaction; + @Mock + CheckedFuture checkedFuture; + + @Before + public void init() { + when(writeTransaction.submit()).thenReturn(checkedFuture); + when(dataProvider.newWriteOnlyTransaction()).thenReturn(writeTransaction); + } + + @Test + public void testConstructor() throws TransactionCommitFailedException { + doNothing().when(writeTransaction).put(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class), + any(SubjectFeatureDefinitions.class)); + + SubjectFeatureDefinitionProvider provider = new SubjectFeatureDefinitionProvider(dataProvider); + + assertNotNull(provider); + verify(dataProvider).newWriteOnlyTransaction(); + verify(writeTransaction, times(NUM_OF_SUBJECT_FEATURE_DEFINITIONS)).put(eq(LogicalDatastoreType.CONFIGURATION), + any(InstanceIdentifier.class), any(SubjectFeatureDefinitions.class)); + + } + + @Test + public void testClose() throws Exception { + doNothing().when(writeTransaction).delete(eq(LogicalDatastoreType.CONFIGURATION), + any(InstanceIdentifier.class)); + + SubjectFeatureDefinitionProvider provider = new SubjectFeatureDefinitionProvider(dataProvider); + + assertNotNull(provider); + + provider.close(); + + verify(dataProvider, times(2)).newWriteOnlyTransaction(); + verify(writeTransaction, times(NUM_OF_SUBJECT_FEATURE_DEFINITIONS)) + .delete(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class)); + + } + +} diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/statistics/StatisticManagerImplTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/statistics/StatisticManagerImplTest.java index 4d2f69437..1268854e7 100755 --- a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/statistics/StatisticManagerImplTest.java +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/statistics/StatisticManagerImplTest.java @@ -8,13 +8,22 @@ package org.opendaylight.groupbasedpolicy.statistics; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import java.util.ArrayList; import java.util.Collections; +import java.util.List; +import java.util.concurrent.ExecutionException; +import com.google.common.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; -import org.mockito.Mockito; +import org.junit.rules.ExpectedException; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; @@ -35,6 +44,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.StatRecordsBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.EpToEpStatisticBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.EpToEpStatisticKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.EpEpgToEpEpgStatistic; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.EpEpgToEpEpgStatisticBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.EpEpgToEpEpgStatisticKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.ep.epg.to.ep.epg.statistic.MatchedRuleStatisticBuilder; @@ -51,68 +61,98 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.statistics.store.statistic.record.StatisticKey; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import com.google.common.base.Optional; -import com.google.common.util.concurrent.CheckedFuture; - public class StatisticManagerImplTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private static final String READING_EXCEPTION_MSG = "Reading throws exception"; + private DataBroker dataBroker; private StatisticsManagerImpl manager; private WriteTransaction wtx; private ReadOnlyTransaction rtx; + private MacAddress srcMac; + private MacAddress dstMac; + private L2BridgeDomainId srcL2C; + private L2BridgeDomainId dstL2C; + private EndpointGroupId srcEPG; + private EndpointGroupId dstEPG; + private TenantId srcTenant; + private TenantId dstTenant; + private ContractId contract; + private SubjectName subject; + private RuleName rule; + private ActionName action; + private ClassifierName classifier; @Before public void init() { - dataBroker = Mockito.mock(DataBroker.class); - wtx = Mockito.mock(WriteTransaction.class); - rtx = Mockito.mock(ReadOnlyTransaction.class); - Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx); - Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rtx); + srcMac = new MacAddress("00:00:00:00:00:01"); + dstMac = new MacAddress("00:00:00:00:00:02"); + srcL2C = new L2BridgeDomainId("srcL2C"); + dstL2C = new L2BridgeDomainId("dstL2C"); + srcEPG = new EndpointGroupId("srcEPG"); + dstEPG = new EndpointGroupId("dstEPG"); + srcTenant = new TenantId("srcTenant"); + dstTenant = new TenantId("dstTenant"); + contract = new ContractId("contract"); + subject = new SubjectName("subject"); + rule = new RuleName("rule"); + action = new ActionName("action"); + classifier = new ClassifierName("classifier"); + + dataBroker = mock(DataBroker.class); + wtx = mock(WriteTransaction.class); + rtx = mock(ReadOnlyTransaction.class); + when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx); + when(dataBroker.newReadOnlyTransaction()).thenReturn(rtx); + manager = new StatisticsManagerImpl(dataBroker); } @Test - public void writeStatTest() { - MacAddress srcMac = new MacAddress("00:00:00:00:00:01"); - MacAddress dstMac = new MacAddress("00:00:00:00:00:02"); - L2BridgeDomainId srcL2C = new L2BridgeDomainId("srcL2C"); - L2BridgeDomainId dstL2C = new L2BridgeDomainId("dstL2C"); - EndpointGroupId srcEPG = new EndpointGroupId("srcEPG"); - EndpointGroupId dstEPG = new EndpointGroupId("dstEPG"); - TenantId srcTenant = new TenantId("srcTenant"); - TenantId dstTenant = new TenantId("dstTenant"); - ContractId contract = new ContractId("contract"); - SubjectName subject = new SubjectName("subject"); - RuleName rule = new RuleName("rule"); - ActionName action = new ActionName("action"); - ClassifierName classifier = new ClassifierName("classifier"); - // input - StatRecordsBuilder recordsBuilder = new StatRecordsBuilder(); - EpToEpStatisticBuilder epBuilder = new EpToEpStatisticBuilder(); - EpEpgToEpEpgStatisticBuilder epgBuilder = new EpEpgToEpEpgStatisticBuilder(); - epgBuilder.setSrcEpg(srcEPG) - .setDstEpg(dstEPG) - .setKey(new EpEpgToEpEpgStatisticKey(dstEPG, srcEPG)) - .setMatchedRuleStatistic(Collections.singletonList(new MatchedRuleStatisticBuilder() - .setKey(new MatchedRuleStatisticKey(contract, rule, subject)) - .setContract(contract) - .setSubject(subject) - .setMatchedRule(rule) - .setAction(Collections.singletonList(action)) - .setClassifier(Collections.singletonList(classifier)) - .setByteCount(25l) - .setPacketCount(5l) - .build())); - epBuilder.setSrcMacAddress(srcMac) - .setDstMacAddress(dstMac) - .setSrcL2c(srcL2C) - .setDstL2c(dstL2C) - .setSrcTenant(srcTenant) - .setDstTenant(dstTenant) - .setKey(new EpToEpStatisticKey(dstL2C, dstMac, srcL2C, srcMac)) - .setEpEpgToEpEpgStatistic(Collections.singletonList(epgBuilder.build())); - recordsBuilder.setEpToEpStatistic(Collections.singletonList(epBuilder.build())); - // output + public void testWriteStat() { + StatRecords input = inputForWriting(); + StatisticRecordKey key = new StatisticRecordKey(new RecordId(0l)); + StatisticRecord output = outputForWriting(key); + + CheckedFuture future = mock(CheckedFuture.class); + when(wtx.submit()).thenReturn(future); + when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx); + + manager.writeStat(input); + verify(wtx).put(LogicalDatastoreType.OPERATIONAL, IidFactory.statisticRecordIid(key), output, true); + } + + @Test + public void testReadStats() throws Exception { + List stats = inputForReading(); + StatRecords statRecords = outputForReading(); + + CheckedFuture, ReadFailedException> future = mock(CheckedFuture.class); + when(future.get()).thenReturn(Optional.of(new StatisticsStoreBuilder().setStatisticRecord(stats).build())); + when(rtx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(StatisticsStore.class).build())) + .thenReturn(future); + + StatRecords read = manager.readStats(); + + Assert.assertEquals(statRecords, read); + } + + @Test + public void testReadStats_throwsException() throws ExecutionException, InterruptedException { + CheckedFuture, ReadFailedException> future = mock(CheckedFuture.class); + when(future.get()).thenThrow(new RuntimeException(READING_EXCEPTION_MSG)); + when(rtx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(StatisticsStore.class).build())) + .thenReturn(future); + + thrown.expect(RuntimeException.class); + thrown.expectMessage(READING_EXCEPTION_MSG); + manager.readStats(); + } + + private List inputForReading() { SrcEndpointBuilder srcBuilder = new SrcEndpointBuilder(); DstEndpointBuilder dstBuilder = new DstEndpointBuilder(); srcBuilder.setMacAddress(srcMac).setL2Context(srcL2C).setTenant(srcTenant); @@ -132,37 +172,33 @@ public class StatisticManagerImplTest { .setStatistic(Collections.singletonList(statBuilder.build())) .setSrcEndpoint(srcBuilder.build()) .setDstEndpoint(dstBuilder.build()); + List stats = new ArrayList<>(); + stats.add(statRecord.build()); + statRecord.setKey(new StatisticRecordKey(new RecordId(1l))); + stats.add(statRecord.build()); -// manager.writeStat(recordsBuilder.build()); -// Mockito.verify(wtx).put(LogicalDatastoreType.OPERATIONAL, -// IidFactory.statisticRecordIid(key), -// statRecord.build()); - CheckedFuture future = Mockito.mock(CheckedFuture.class); - Mockito.when(wtx.submit()).thenReturn(future); - Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx); - - manager.writeStat(recordsBuilder.build()); - Mockito.verify(wtx).put(LogicalDatastoreType.OPERATIONAL, - IidFactory.statisticRecordIid(key), - statRecord.build(), true); + return stats; } - @Test - public void readStatsTest() { - MacAddress srcMac = new MacAddress("00:00:00:00:00:01"); - MacAddress dstMac = new MacAddress("00:00:00:00:00:02"); - L2BridgeDomainId srcL2C = new L2BridgeDomainId("srcL2C"); - L2BridgeDomainId dstL2C = new L2BridgeDomainId("dstL2C"); - EndpointGroupId srcEPG = new EndpointGroupId("srcEPG"); - EndpointGroupId dstEPG = new EndpointGroupId("dstEPG"); - TenantId srcTenant = new TenantId("srcTenant"); - TenantId dstTenant = new TenantId("dstTenant"); - ContractId contract = new ContractId("contract"); - SubjectName subject = new SubjectName("subject"); - RuleName rule = new RuleName("rule"); - ActionName action = new ActionName("action"); - ClassifierName classifier = new ClassifierName("classifier"); - // input + private StatRecords outputForReading() { + StatRecordsBuilder recordsBuilder = new StatRecordsBuilder(); + EpEpgToEpEpgStatisticBuilder epgBuilder = newEpEpgToEpEpgStatisticBuilder(50, 10); + EpToEpStatisticBuilder epBuilder = newEpToEpStatisticBuilder(Collections.singletonList(epgBuilder.build())); + recordsBuilder.setEpToEpStatistic(Collections.singletonList(epBuilder.build())); + + return recordsBuilder.build(); + } + + private StatRecords inputForWriting() { + StatRecordsBuilder recordsBuilder = new StatRecordsBuilder(); + EpEpgToEpEpgStatisticBuilder epgBuilder = newEpEpgToEpEpgStatisticBuilder(25, 5); + EpToEpStatisticBuilder epBuilder = newEpToEpStatisticBuilder(Collections.singletonList(epgBuilder.build())); + recordsBuilder.setEpToEpStatistic(Collections.singletonList(epBuilder.build())); + + return recordsBuilder.build(); + } + + private StatisticRecord outputForWriting(StatisticRecordKey key) { SrcEndpointBuilder srcBuilder = new SrcEndpointBuilder(); DstEndpointBuilder dstBuilder = new DstEndpointBuilder(); srcBuilder.setMacAddress(srcMac).setL2Context(srcL2C).setTenant(srcTenant); @@ -177,53 +213,39 @@ public class StatisticManagerImplTest { .setAction(Collections.singletonList(action)) .setByteCount(25l) .setPacketCount(5l); - StatisticRecordKey key = new StatisticRecordKey(new RecordId(0l)); StatisticRecordBuilder statRecord = new StatisticRecordBuilder().setKey(key) .setStatistic(Collections.singletonList(statBuilder.build())) .setSrcEndpoint(srcBuilder.build()) .setDstEndpoint(dstBuilder.build()); - ArrayList stats = new ArrayList<>(); - stats.add(statRecord.build()); - statRecord.setKey(new StatisticRecordKey(new RecordId(1l))); - stats.add(statRecord.build()); - // output - StatRecordsBuilder recordsBuilder = new StatRecordsBuilder(); - EpToEpStatisticBuilder epBuilder = new EpToEpStatisticBuilder(); - EpEpgToEpEpgStatisticBuilder epgBuilder = new EpEpgToEpEpgStatisticBuilder(); - epgBuilder.setSrcEpg(srcEPG) + + return statRecord.build(); + } + + private EpEpgToEpEpgStatisticBuilder newEpEpgToEpEpgStatisticBuilder(long byteCount, long packetCount) { + return new EpEpgToEpEpgStatisticBuilder().setSrcEpg(srcEPG) .setDstEpg(dstEPG) .setKey(new EpEpgToEpEpgStatisticKey(dstEPG, srcEPG)) - .setMatchedRuleStatistic(Collections.singletonList(new MatchedRuleStatisticBuilder() - .setKey(new MatchedRuleStatisticKey(contract, rule, subject)) - .setContract(contract) - .setSubject(subject) - .setMatchedRule(rule) - .setAction(Collections.singletonList(action)) - .setClassifier(Collections.singletonList(classifier)) - .setByteCount(50l) - .setPacketCount(10l) - .build())); - epBuilder.setSrcMacAddress(srcMac) + .setMatchedRuleStatistic(Collections.singletonList( + new MatchedRuleStatisticBuilder().setKey(new MatchedRuleStatisticKey(contract, rule, subject)) + .setContract(contract) + .setSubject(subject) + .setMatchedRule(rule) + .setAction(Collections.singletonList(action)) + .setClassifier(Collections.singletonList(classifier)) + .setByteCount(byteCount) + .setPacketCount(packetCount) + .build())); + } + + private EpToEpStatisticBuilder newEpToEpStatisticBuilder(List list) { + return new EpToEpStatisticBuilder().setSrcMacAddress(srcMac) .setDstMacAddress(dstMac) .setSrcL2c(srcL2C) .setDstL2c(dstL2C) .setSrcTenant(srcTenant) .setDstTenant(dstTenant) .setKey(new EpToEpStatisticKey(dstL2C, dstMac, srcL2C, srcMac)) - .setEpEpgToEpEpgStatistic(Collections.singletonList(epgBuilder.build())); - recordsBuilder.setEpToEpStatistic(Collections.singletonList(epBuilder.build())); - - CheckedFuture, ReadFailedException> future = Mockito.mock(CheckedFuture.class); - try { - Mockito.when(future.get()) - .thenReturn(Optional.of(new StatisticsStoreBuilder().setStatisticRecord(stats).build())); - Mockito.when(rtx.read(LogicalDatastoreType.OPERATIONAL, - InstanceIdentifier.builder(StatisticsStore.class).build())) - .thenReturn(future); - StatRecords read = manager.readStats(); - Assert.assertEquals(recordsBuilder.build(), read); - } catch (Exception e) { - } - + .setEpEpgToEpEpgStatistic(list); } + } diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/test/GbpDataBrokerTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/test/GbpDataBrokerTest.java old mode 100644 new mode 100755 index 5300147ac..caa579308 --- a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/test/GbpDataBrokerTest.java +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/test/GbpDataBrokerTest.java @@ -10,14 +10,13 @@ package org.opendaylight.groupbasedpolicy.test; import java.util.Collection; +import com.google.common.collect.ImmutableList; import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.Renderers; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies; -import com.google.common.collect.ImmutableList; - /** * Loads only modules of GBP and it's dependencies for data broker. *
diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/util/IidFactoryTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/util/IidFactoryTest.java old mode 100644 new mode 100755 index ef11a29cc..1479001d3 --- a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/util/IidFactoryTest.java +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/util/IidFactoryTest.java @@ -16,7 +16,9 @@ import org.junit.Test; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix; 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.groupbasedpolicy.common.rev140421.ActionDefinitionId; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierDefinitionId; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId; @@ -35,6 +37,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.r import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ActionDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ClassifierDefinition; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain; @@ -42,6 +46,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ExternalImplicitGroup; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Subject; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.Rule; @@ -49,6 +54,13 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderNamedSelector; 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.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.RendererName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.Renderers; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.Renderer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.capabilities.SupportedActionDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.capabilities.SupportedClassifierDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.interests.followed.tenants.FollowedTenant; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.interests.followed.tenants.followed.tenant.FollowedEndpointGroup; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; public class IidFactoryTest { @@ -58,6 +70,7 @@ public class IidFactoryTest { private ContractId contractId; private SubjectName subjectName; private RuleName ruleName; + private RendererName rendererName; @Before public void initialise() { @@ -66,30 +79,37 @@ public class IidFactoryTest { contractId = mock(ContractId.class); subjectName = mock(SubjectName.class); ruleName = mock(RuleName.class); + rendererName = mock(RendererName.class); } @Test - public void tenantIdTest() { + public void testTenantId() { InstanceIdentifier identifier = IidFactory.tenantIid(tenantId); Assert.assertEquals(tenantId, InstanceIdentifier.keyOf(identifier).getId()); } @Test - public void endpointGroupIidTest() { + public void testEndpointGroupIid() { InstanceIdentifier identifier = IidFactory.endpointGroupIid(tenantId, epgId); Assert.assertEquals(epgId, InstanceIdentifier.keyOf(identifier).getId()); Assert.assertEquals(tenantId, identifier.firstKeyOf(Tenant.class).getId()); } @Test - public void contractIidTest() { + public void testContractIid() { InstanceIdentifier identifier = IidFactory.contractIid(tenantId, contractId); Assert.assertEquals(contractId, InstanceIdentifier.keyOf(identifier).getId()); Assert.assertEquals(tenantId, identifier.firstKeyOf(Tenant.class).getId()); } @Test - public void subjectIidTest() { + public void testContractWildcardIid() { + InstanceIdentifier identifier = IidFactory.contractWildcardIid(tenantId); + Assert.assertEquals(tenantId, identifier.firstKeyOf(Tenant.class).getId()); + } + + @Test + public void testSubjectIid() { SubjectName subjectName = mock(SubjectName.class); InstanceIdentifier identifier = IidFactory.subjectIid(tenantId, contractId, subjectName); Assert.assertEquals(subjectName, InstanceIdentifier.keyOf(identifier).getName()); @@ -98,7 +118,7 @@ public class IidFactoryTest { } @Test - public void providerNamedSelectorIidTest() { + public void testProviderNamedSelectorIid() { SelectorName providerSelectorName = mock(SelectorName.class); InstanceIdentifier identifier = IidFactory.providerNamedSelectorIid(tenantId, epgId, providerSelectorName); @@ -108,7 +128,7 @@ public class IidFactoryTest { } @Test - public void consumerNamedSelectorIidTest() { + public void testConsumerNamedSelectorIid() { SelectorName consumerSelectorName = mock(SelectorName.class); InstanceIdentifier identifier = IidFactory.consumerNamedSelectorIid(tenantId, epgId, consumerSelectorName); @@ -118,7 +138,7 @@ public class IidFactoryTest { } @Test - public void clauseIidTest() { + public void testClauseIid() { ClauseName clauseName = mock(ClauseName.class); InstanceIdentifier identifier = IidFactory.clauseIid(tenantId, contractId, clauseName); Assert.assertEquals(clauseName, InstanceIdentifier.keyOf(identifier).getName()); @@ -127,7 +147,7 @@ public class IidFactoryTest { } @Test - public void ruleIdTest() { + public void testRuleId() { InstanceIdentifier identifier = IidFactory.ruleIid(tenantId, contractId, subjectName, ruleName); Assert.assertEquals(ruleName, InstanceIdentifier.keyOf(identifier).getName()); Assert.assertEquals(subjectName, identifier.firstKeyOf(Subject.class).getName()); @@ -136,7 +156,7 @@ public class IidFactoryTest { } @Test - public void actionInstanceIidTest() { + public void testActionInstanceIid() { ActionName actionName = mock(ActionName.class); InstanceIdentifier identifier = IidFactory.actionInstanceIid(tenantId, actionName); Assert.assertEquals(actionName, InstanceIdentifier.keyOf(identifier).getName()); @@ -144,7 +164,7 @@ public class IidFactoryTest { } @Test - public void classifierInstanceIidTest() { + public void testClassifierInstanceIid() { ClassifierName classifierName = mock(ClassifierName.class); InstanceIdentifier identifier = IidFactory.classifierInstanceIid(tenantId, classifierName); Assert.assertEquals(classifierName, InstanceIdentifier.keyOf(identifier).getName()); @@ -152,7 +172,7 @@ public class IidFactoryTest { } @Test - public void classifierRefIidTest() { + public void testClassifierRefIid() { ClassifierName classifierRefName = mock(ClassifierName.class); InstanceIdentifier identifier = IidFactory.classifierRefIid(tenantId, contractId, subjectName, ruleName, classifierRefName); @@ -164,7 +184,7 @@ public class IidFactoryTest { } @Test - public void l2FloodDomainIidTest() { + public void testL2FloodDomainIid() { L2FloodDomainId l2FloodDomainId = mock(L2FloodDomainId.class); InstanceIdentifier identifier = IidFactory.l2FloodDomainIid(tenantId, l2FloodDomainId); Assert.assertEquals(l2FloodDomainId, InstanceIdentifier.keyOf(identifier).getId()); @@ -172,7 +192,7 @@ public class IidFactoryTest { } @Test - public void l2BridgeDomainIidTest() { + public void testL2BridgeDomainIid() { L2BridgeDomainId l2BridgeDomainId = mock(L2BridgeDomainId.class); InstanceIdentifier identifier = IidFactory.l2BridgeDomainIid(tenantId, l2BridgeDomainId); Assert.assertEquals(l2BridgeDomainId, InstanceIdentifier.keyOf(identifier).getId()); @@ -180,7 +200,7 @@ public class IidFactoryTest { } @Test - public void l3ContextIidTest() { + public void testL3ContextIid() { L3ContextId l3ContextId = mock(L3ContextId.class); InstanceIdentifier identifier = IidFactory.l3ContextIid(tenantId, l3ContextId); Assert.assertEquals(l3ContextId, InstanceIdentifier.keyOf(identifier).getId()); @@ -188,7 +208,7 @@ public class IidFactoryTest { } @Test - public void endpointIidTest() { + public void testEndpointIid() { L2BridgeDomainId l2Context = mock(L2BridgeDomainId.class); MacAddress macAddress = mock(MacAddress.class); InstanceIdentifier identifier = IidFactory.endpointIid(l2Context, macAddress); @@ -201,7 +221,7 @@ public class IidFactoryTest { } @Test - public void l3EndpointIidTest() { + public void testL3EndpointIid() { L3ContextId l3ContextId = mock(L3ContextId.class); IpAddress ipAddress = mock(IpAddress.class); InstanceIdentifier identifier = IidFactory.l3EndpointIid(l3ContextId, ipAddress); @@ -210,13 +230,13 @@ public class IidFactoryTest { } @Test - public void l3EndpointIidWildcardTest() { + public void testL3EndpointIidWildcard() { InstanceIdentifier identifier = IidFactory.l3EndpointsIidWildcard(); Assert.assertNotNull(identifier); } @Test - public void endpointL3PrefixIidTest() { + public void testEndpointL3PrefixIid() { L3ContextId l3Context = mock(L3ContextId.class); IpPrefix ipPrefix = mock(IpPrefix.class); InstanceIdentifier identifier = IidFactory.endpointL3PrefixIid(l3Context, ipPrefix); @@ -225,17 +245,88 @@ public class IidFactoryTest { } @Test - public void endpointIidWildcardTest() { + public void testEndpointIidWildcard() { InstanceIdentifier identifier = IidFactory.endpointsIidWildcard(); Assert.assertNotNull(identifier); } @Test - public void subnetIidTest() { + public void testSubnetIid() { SubnetId subnetId = mock(SubnetId.class); InstanceIdentifier identifier = IidFactory.subnetIid(tenantId, subnetId); Assert.assertEquals(tenantId, identifier.firstKeyOf(Tenant.class).getId()); Assert.assertEquals(subnetId, identifier.firstKeyOf(Subnet.class).getId()); } + @Test + public void testSubnetWildcardIid() { + InstanceIdentifier identifier = IidFactory.subnetWildcardIid(tenantId); + Assert.assertEquals(tenantId, identifier.firstKeyOf(Tenant.class).getId()); + } + + @Test + public void testClassifierDefinitionIid() { + ClassifierDefinitionId classifierDefinitionId = mock(ClassifierDefinitionId.class); + InstanceIdentifier identifier = IidFactory.classifierDefinitionIid( + classifierDefinitionId); + Assert.assertEquals(classifierDefinitionId, identifier.firstKeyOf(ClassifierDefinition.class).getId()); + } + + @Test + public void testActionDefinitionIid() { + ActionDefinitionId actionDefinitionId = mock(ActionDefinitionId.class); + InstanceIdentifier identifier = IidFactory.actionDefinitionIid( + actionDefinitionId); + Assert.assertEquals(actionDefinitionId, identifier.firstKeyOf(ActionDefinition.class).getId()); + } + + @Test + public void testSupportedActionDefinitionIidWildcard() { + InstanceIdentifier + identifier = IidFactory.supportedActionDefinitionIidWildcard(); + Assert.assertNotNull(identifier); + } + + @Test + public void testSupportedClassifierDefinitionIidWildcard() { + InstanceIdentifier + identifier = IidFactory.supportedClassifierDefinitionIidWildcard(); + Assert.assertNotNull(identifier); + } + + @Test + public void testFollowedEndpointgroupIid(){ + InstanceIdentifier + identifier = IidFactory.followedEndpointgroupIid(rendererName, tenantId, epgId); + Assert.assertEquals(epgId, identifier.firstKeyOf(FollowedEndpointGroup.class).getId()); + } + + @Test + public void testFollowedTenantIid(){ + InstanceIdentifier + identifier = IidFactory.followedTenantIid(rendererName, tenantId); + Assert.assertEquals(tenantId, identifier.firstKeyOf(FollowedTenant.class).getId()); + } + + @Test + public void testRendererIid(){ + InstanceIdentifier + identifier = IidFactory.rendererIid(rendererName); + Assert.assertEquals(rendererName, identifier.firstKeyOf(Renderer.class).getName()); + } + + @Test + public void testRenderersIid(){ + InstanceIdentifier + identifier = IidFactory.renderersIid(); + Assert.assertNotNull(identifier); + } + + @Test + public void testeExternalImplicitGroupIid(){ + InstanceIdentifier + identifier = IidFactory.externalImplicitGroupIid(tenantId, epgId); + Assert.assertEquals(epgId, identifier.firstKeyOf(ExternalImplicitGroup.class).getId()); + } + } diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/util/SingletonTaskTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/util/SingletonTaskTest.java old mode 100644 new mode 100755 index 0fb4e1269..0b1af0fa4 --- a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/util/SingletonTaskTest.java +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/util/SingletonTaskTest.java @@ -21,12 +21,12 @@ import org.junit.Test; public class SingletonTaskTest { - public int ran = 0; - public int finished = 0; - public long time = 0; + private int ran; + private int finished; + private long time; @Before - public void setUp() throws Exception { + public void init() { ran = 0; finished = 0; time = 0; @@ -54,73 +54,66 @@ public class SingletonTaskTest { public void testDelay() throws InterruptedException { ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); + ran = 0; SingletonTask st1 = new SingletonTask(ses, new Runnable() { @Override public void run() { - ran += 1; + ran++; time = System.nanoTime(); } }); long start = System.nanoTime(); st1.reschedule(10, TimeUnit.MILLISECONDS); - assertFalse("Check that task hasn't run yet", ran > 0); + assertFalse("Task has run already", ran > 0); ses.shutdown(); ses.awaitTermination(5, TimeUnit.SECONDS); - assertEquals("Check that task ran", 1, ran); + assertEquals("Check that task ran only once failed", 1, ran); assertTrue("Check that time passed appropriately", (time - start) >= TimeUnit.NANOSECONDS.convert(10, TimeUnit.MILLISECONDS)); } @Test public void testReschedule() throws InterruptedException { - ScheduledExecutorService ses = - Executors.newSingleThreadScheduledExecutor(); + ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); + ran = 0; + time = 0; + + final long EPSILON_NS = 1500000; // 1.5 ms + final int DELAY_MS = 20; + final int NUM_OF_ITERATIONS = 8; + final int SLEEP_MS = 5; + final int AWAIT_SEC = 5; + final int EXPECTED_PASS_TIME = + NUM_OF_ITERATIONS * SLEEP_MS + (DELAY_MS - SLEEP_MS); final Object tc = this; SingletonTask st1 = new SingletonTask(ses, new Runnable() { + @Override public void run() { synchronized (tc) { - ran += 1; + ran++; } time = System.nanoTime(); } }); + long start = System.nanoTime(); - st1.reschedule(20, TimeUnit.MILLISECONDS); - Thread.sleep(5); - assertFalse("Check that task hasn't run yet", ran > 0); - st1.reschedule(20, TimeUnit.MILLISECONDS); - Thread.sleep(5); - assertFalse("Check that task hasn't run yet", ran > 0); - st1.reschedule(20, TimeUnit.MILLISECONDS); - Thread.sleep(5); - assertFalse("Check that task hasn't run yet", ran > 0); - st1.reschedule(20, TimeUnit.MILLISECONDS); - Thread.sleep(5); - assertFalse("Check that task hasn't run yet", ran > 0); - st1.reschedule(20, TimeUnit.MILLISECONDS); - Thread.sleep(5); - assertFalse("Check that task hasn't run yet", ran > 0); - st1.reschedule(20, TimeUnit.MILLISECONDS); - Thread.sleep(5); - assertFalse("Check that task hasn't run yet", ran > 0); - st1.reschedule(20, TimeUnit.MILLISECONDS); - Thread.sleep(5); - assertFalse("Check that task hasn't run yet", ran > 0); - st1.reschedule(20, TimeUnit.MILLISECONDS); - Thread.sleep(5); - assertFalse("Check that task hasn't run yet", ran > 0); + for (int i = 0; i < NUM_OF_ITERATIONS; i++) { + st1.reschedule(DELAY_MS, TimeUnit.MILLISECONDS); + Thread.sleep(SLEEP_MS); + assertFalse("Task has run already", ran > 0); + } ses.shutdown(); - ses.awaitTermination(5, TimeUnit.SECONDS); + ses.awaitTermination(AWAIT_SEC, TimeUnit.SECONDS); - assertEquals("Check that task ran only once", 1, ran); + assertEquals("Check that task ran only once failed", 1, ran); assertTrue("Check that time passed appropriately: " + (time - start), - (time - start) >= TimeUnit.NANOSECONDS.convert(55, TimeUnit.MILLISECONDS)); + (time - start) >= 1000000 * EXPECTED_PASS_TIME - EPSILON_NS); } @Test -- 2.36.6