Fix fileEncoding violations for checkstyle
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / ChainActionTest.java
index ee8ae00c9b27ddd350b1acc22c5105905700630c..e4b4fb72daf229795654c8706a380203276e91a0 100755 (executable)
@@ -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,45 +8,59 @@
 
 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
 
+import static org.junit.Assert.assertEquals;
 import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doReturn;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
-import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 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;
+import org.junit.runner.RunWith;
 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.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.EndpointManager;
-import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
+import org.opendaylight.groupbasedpolicy.api.sf.ChainActionDefinition;
+import org.opendaylight.groupbasedpolicy.dto.EgKey;
 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
+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.flow.PolicyEnforcer.NetworkElements;
-import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.PolicyEnforcer.PolicyPair;
-import org.opendaylight.groupbasedpolicy.resolver.EgKey;
+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.rev130715.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.subject.feature.instances.ActionInstance;
+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;
 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;
 
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
-
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ChainAction.class)
 public class ChainActionTest {
 
     private ChainAction chainAction;
@@ -62,18 +76,21 @@ public class ChainActionTest {
     private NodeId nodeId;
     private EndpointFwdCtxOrdinals endpointFwdCtxOrdinals;
     private Endpoint endpoint;
-    private PolicyPair policyPair;
     private EndpointManager endpointManager;
     private EgKey egKey;
+    private TenantId tenantId = new TenantId("e09a2308-6ffa-40af-92a2-69f54b2cf3e4");
 
     @SuppressWarnings("unchecked")
     @Before
-    public void initialise() throws Exception {
-        chainAction = spy(new ChainAction());
+    public void init() throws Exception {
+        chainAction = new ChainAction();
 
-        sfcPath = mock(ServiceFunctionPath.class);
-        when(sfcPath.getName()).thenReturn("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);
@@ -84,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);
 
@@ -92,93 +108,95 @@ public class ChainActionTest {
         nodeId = mock(NodeId.class);
         when(netElements.getDstNodeId()).thenReturn(nodeId);
         endpointFwdCtxOrdinals = mock(EndpointFwdCtxOrdinals.class);
-        when(netElements.getSrcEpOrds()).thenReturn(endpointFwdCtxOrdinals);
-        endpoint = mock(Endpoint.class);
+        when(netElements.getSrcEpOrdinals()).thenReturn(endpointFwdCtxOrdinals);
+        when(netElements.getDstEpOrdinals()).thenReturn(endpointFwdCtxOrdinals);
         when(netElements.getSrcEp()).thenReturn(endpoint);
-        policyPair = mock(PolicyPair.class);
-        when(policyPair.getConsumerEpgId()).thenReturn(Integer.valueOf(5));
 
         endpointManager = mock(EndpointManager.class);
         when(ctx.getEndpointManager()).thenReturn(endpointManager);
         egKey = mock(EgKey.class);
-        Set<EgKey> keysForEndpoint = new HashSet<EgKey>();
+        Set<EgKey> 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<ActionBuilder> actions = Arrays.asList(actionBuilder);
-        Map<String, Object> params = new HashMap<String, Object>();
+        List<ActionBuilder> actions = Collections.singletonList(actionBuilder);
+        Map<String, Object> params = new HashMap<>();
         String chainName = "chainName";
-        params.put(ChainAction.SFC_CHAIN_NAME, chainName);
-        Integer order = Integer.valueOf(0);
+        params.put(ChainActionDefinition.SFC_CHAIN_NAME, chainName);
+        Integer order = 0;
         OfWriter ofWriter = mock(OfWriter.class);
 
-        doReturn(sfcPath).when(chainAction).getSfcPath(chainName);
+        PowerMockito.mockStatic(ChainAction.class);
+        SfcName sfcName = new SfcName(chainName);
+        when(ChainAction.getSfcPath(eq(sfcName))).thenReturn(sfcPath);
+        chainAction.setResolvedSymmetricChains(Collections.singletonList(chainName));
 
-        List<ActionBuilder> result = chainAction.updateAction(actions, params, order, netElements, policyPair,
-                ofWriter,
-                ctx, Direction.Out);
+        List<ActionBuilder> result =
+                chainAction.updateAction(actions, params, order, netElements, ofWriter, ctx, Direction.Out);
         Assert.assertNull(result);
     }
 
     @Test
-    public void updateActionTestParamsNull() {
+    public void testUpdateAction_ParamsNull() {
         ActionBuilder actionBuilder = mock(ActionBuilder.class);
-        List<ActionBuilder> actions = Arrays.asList(actionBuilder);
-        Integer order = Integer.valueOf(0);
+        List<ActionBuilder> actions = Collections.singletonList(actionBuilder);
+        Integer order = 0;
         OfWriter ofWriter = mock(OfWriter.class);
 
-        List<ActionBuilder> result = chainAction.updateAction(actions, null, order, netElements, policyPair,
-                ofWriter,
-                ctx, Direction.In);
+        List<ActionBuilder> result =
+                chainAction.updateAction(actions, null, order, netElements, ofWriter, ctx, Direction.In);
         Assert.assertNull(result);
     }
 
     @Test
-    public void updateActionTestChainNameNull() {
+    public void testUpdateAction_ChainNameNull() {
         ActionBuilder actionBuilder = mock(ActionBuilder.class);
-        List<ActionBuilder> actions = Arrays.asList(actionBuilder);
-        Map<String, Object> params = new HashMap<String, Object>();
-        params.put(ChainAction.SFC_CHAIN_NAME, null);
-        Integer order = Integer.valueOf(0);
+        List<ActionBuilder> actions = Collections.singletonList(actionBuilder);
+        Map<String, Object> params = new HashMap<>();
+        params.put(ChainActionDefinition.SFC_CHAIN_NAME, null);
+        Integer order = 0;
         NetworkElements netElements = mock(NetworkElements.class);
-        PolicyPair policyPair = mock(PolicyPair.class);
         OfWriter ofWriter = mock(OfWriter.class);
 
-        chainAction.updateAction(actions, params, order, netElements, policyPair, ofWriter, ctx, Direction.In);
+        List<ActionBuilder> result =
+                chainAction.updateAction(actions, params, order, netElements, ofWriter, ctx, Direction.In);
+
+        Assert.assertNull(result);
     }
 
     @Test
-    public void updateActionTest() {
+    public void testUpdateAction() {
         ActionBuilder actionBuilder = mock(ActionBuilder.class);
-        List<ActionBuilder> actions = Arrays.asList(actionBuilder);
-        Map<String, Object> params = new HashMap<String, Object>();
+        List<ActionBuilder> actions = Collections.singletonList(actionBuilder);
+        Map<String, Object> params = new HashMap<>();
         String chainName = "chainName";
-        params.put(ChainAction.SFC_CHAIN_NAME, chainName);
-        Integer order = Integer.valueOf(0);
+        params.put(ChainActionDefinition.SFC_CHAIN_NAME, chainName);
+        Integer order = 0;
         OfWriter ofWriter = mock(OfWriter.class);
 
-        doReturn(sfcPath).when(chainAction).getSfcPath(chainName);
-        when(sfcPath.getName()).thenReturn(null);
+        ServiceFunctionPath sfcPathNameNull = new ServiceFunctionPathBuilder().setName(null).setSymmetric(true).build();
 
-        List<ActionBuilder> result = chainAction.updateAction(actions, params, order, netElements, policyPair,
-                ofWriter,
-                ctx, Direction.Out);
+        PowerMockito.mockStatic(ChainAction.class);
+        when(ChainAction.getSfcPath(new SfcName(chainName))).thenReturn(sfcPathNameNull);
+
+        List<ActionBuilder> result =
+                chainAction.updateAction(actions, params, order, netElements, ofWriter, ctx, Direction.Out);
         Assert.assertNull(result);
     }
 
-     @Test
-    public void isValidTestParameterValueNull() {
-         ActionInstance actionInstance = mock(ActionInstance.class);
-         Assert.assertFalse(chainAction.isValid(actionInstance));
+    @Test
+    public void testIsValid_ParameterValueNull() {
+        ActionInstance actionInstance = mock(ActionInstance.class);
+        Assert.assertFalse(chainAction.validate(actionInstance).isValid());
     }
 }