Add unit test for MPLS path intent installer

- refactored common code from intent installer tests into
  a base class.

Change-Id: Iab4d01531748dd014ba73cc2fbed5930d8018977
diff --git a/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java b/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java
index f56632c..ac4b44b 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/IntentTestsMocks.java
@@ -52,6 +52,8 @@
 import org.onosproject.net.resource.LinkResourceListener;
 import org.onosproject.net.resource.LinkResourceRequest;
 import org.onosproject.net.resource.LinkResourceService;
+import org.onosproject.net.resource.MplsLabel;
+import org.onosproject.net.resource.MplsLabelResourceAllocation;
 import org.onosproject.net.resource.ResourceAllocation;
 import org.onosproject.net.resource.ResourceRequest;
 import org.onosproject.net.resource.ResourceType;
@@ -63,6 +65,7 @@
 import org.onosproject.store.Timestamp;
 
 import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableSet;
 
 /**
  * Common mocks used by the intent framework tests.
@@ -152,7 +155,7 @@
     public static class MockLinkResourceAllocations implements LinkResourceAllocations {
         @Override
         public Set<ResourceAllocation> getResourceAllocation(Link link) {
-            return null;
+            return ImmutableSet.of(new MplsLabelResourceAllocation(MplsLabel.valueOf(10)));
         }
 
         @Override
@@ -254,17 +257,19 @@
 
         @Override
         public Iterable<LinkResourceAllocations> getAllocations() {
-            return null;
+            return ImmutableSet.of(
+                    new IntentTestsMocks.MockLinkResourceAllocations());
         }
 
         @Override
         public Iterable<LinkResourceAllocations> getAllocations(Link link) {
-            return null;
+            return ImmutableSet.of(
+                    new IntentTestsMocks.MockLinkResourceAllocations());
         }
 
         @Override
         public LinkResourceAllocations getAllocations(IntentId intentId) {
-            return null;
+            return new IntentTestsMocks.MockLinkResourceAllocations();
         }
 
         @Override
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/IntentInstallerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/IntentInstallerTest.java
new file mode 100644
index 0000000..b80ef2f
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/IntentInstallerTest.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.intent.impl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.IdGenerator;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.flow.FlowRuleOperation;
+import org.onosproject.net.intent.FakeIntentManager;
+import org.onosproject.net.intent.Intent;
+import org.onosproject.net.intent.IntentInstaller;
+import org.onosproject.net.intent.IntentTestsMocks;
+import org.onosproject.net.intent.MockIdGenerator;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.onosproject.net.NetTestTools.APP_ID;
+import static org.onosproject.net.NetTestTools.connectPoint;
+
+/**
+ * Base class for intent installer tests.
+ */
+public class IntentInstallerTest {
+
+    /**
+     * Mock for core service.
+     */
+    static class TestCoreService extends CoreServiceAdapter {
+
+        String registeredId = "";
+
+        @Override
+        public ApplicationId registerApplication(String identifier) {
+            registeredId = identifier;
+            return APP_ID;
+        }
+    }
+
+    /**
+     * Mock for intent manager service. Checks that the PathIntent
+     * installer installs and uninstalls properly.
+     */
+    static class MockIntentManager extends FakeIntentManager {
+
+        boolean installerRegistered = false;
+        final Class expectedClass;
+
+        private MockIntentManager() {
+            expectedClass = null;
+        }
+
+        MockIntentManager(Class expectedInstaller) {
+            this.expectedClass = expectedInstaller;
+        }
+
+        @Override
+        public <T extends Intent> void registerInstaller(
+                Class<T> cls,
+                IntentInstaller<T> installer) {
+            assertThat(cls, equalTo(expectedClass));
+            installerRegistered = true;
+        }
+
+        @Override
+        public <T extends Intent> void unregisterInstaller(Class<T> cls) {
+            assertThat(cls, equalTo(expectedClass));
+            assertThat(installerRegistered, is(true));
+        }
+
+    }
+
+    CoreService testCoreService;
+    IdGenerator idGenerator = new MockIdGenerator();
+    IntentInstaller installer;
+
+    final IntentTestsMocks.MockSelector selector = new IntentTestsMocks.MockSelector();
+    final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
+    final ConnectPoint d1p1 = connectPoint("s1", 0);
+    final ConnectPoint d2p0 = connectPoint("s2", 0);
+    final ConnectPoint d2p1 = connectPoint("s2", 1);
+    final ConnectPoint d3p1 = connectPoint("s3", 1);
+    final ConnectPoint d3p0 = connectPoint("s3", 10);
+    final ConnectPoint d1p0 = connectPoint("s1", 10);
+
+    /**
+     * Configures objects used in all the test cases.
+     */
+    @Before
+    public void setUp() {
+        testCoreService = new TestCoreService();
+        Intent.bindIdGenerator(idGenerator);
+    }
+
+    /**
+     * Tears down objects used in all the test cases.
+     */
+    @After
+    public void tearDown() {
+        Intent.unbindIdGenerator(idGenerator);
+    }
+
+    /**
+     * Checks that a flow operation contains the correct values.
+     *
+     * @param op flow rule operation to check
+     * @param type type the flow rule operation should have
+     * @param deviceId device id the flow rule operation should have
+     */
+    void checkFlowOperation(FlowRuleOperation op,
+                                    FlowRuleOperation.Type type,
+                                    DeviceId deviceId) {
+        assertThat(op.type(), is(type));
+        assertThat(op.rule().deviceId(), equalTo(deviceId));
+    }
+
+}
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/MplsPathIntentInstallerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/MplsPathIntentInstallerTest.java
new file mode 100644
index 0000000..c2f616a
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/MplsPathIntentInstallerTest.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.intent.impl;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.MplsLabel;
+import org.onosproject.net.DefaultLink;
+import org.onosproject.net.DefaultPath;
+import org.onosproject.net.Link;
+import org.onosproject.net.flow.FlowRuleOperation;
+import org.onosproject.net.intent.IntentTestsMocks;
+import org.onosproject.net.intent.MplsPathIntent;
+import org.onosproject.store.trivial.impl.SimpleLinkStore;
+
+import com.google.common.collect.ImmutableList;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.net.Link.Type.DIRECT;
+import static org.onosproject.net.NetTestTools.APP_ID;
+import static org.onosproject.net.NetTestTools.PID;
+
+/**
+ * Unit tests for path intent installer.
+ */
+public class  MplsPathIntentInstallerTest extends IntentInstallerTest {
+
+    MplsPathIntentInstaller installer;
+
+    private final Optional<MplsLabel> ingressLabel =
+            Optional.ofNullable(MplsLabel.mplsLabel(10));
+    private final Optional<MplsLabel> egressLabel =
+            Optional.ofNullable(MplsLabel.mplsLabel(20));
+
+    private final List<Link> links = Arrays.asList(
+            new DefaultLink(PID, d1p1, d2p0, DIRECT),
+            new DefaultLink(PID, d2p1, d3p1, DIRECT)
+    );
+    private final int hops = links.size() - 1;
+    private MplsPathIntent intent;
+
+    /**
+     * Configures objects used in all the test cases.
+     */
+    @Before
+    public void localSetUp() {
+        installer = new MplsPathIntentInstaller();
+        installer.coreService = testCoreService;
+        installer.intentManager = new MockIntentManager(MplsPathIntent.class);
+        installer.linkStore = new SimpleLinkStore();
+        installer.resourceService = new IntentTestsMocks.MockResourceService();
+
+        intent = new MplsPathIntent(APP_ID, selector, treatment,
+                new DefaultPath(PID, links, hops),
+                ingressLabel,
+                egressLabel,
+                ImmutableList.of());
+    }
+
+    /**
+     * Tests activation and deactivation of the installer.
+     */
+    @Test
+    public void activateDeactivate() {
+        installer.activate();
+        installer.deactivate();
+    }
+
+    /**
+     * Tests installation operation of the MPLS path intent installer.
+     */
+    @Test
+    public void install() {
+        installer.activate();
+
+        List<Collection<FlowRuleOperation>> operations =
+                installer.install(intent);
+        assertThat(operations, notNullValue());
+        assertThat(operations, hasSize(1));
+
+        Collection<FlowRuleOperation> flowRuleOpsCollection = operations.get(0);
+        assertThat(flowRuleOpsCollection, hasSize(hops));
+        FlowRuleOperation[] flowRuleOps =
+                flowRuleOpsCollection.toArray(new FlowRuleOperation[hops]);
+
+        FlowRuleOperation op0 = flowRuleOps[0];
+        checkFlowOperation(op0, FlowRuleOperation.Type.ADD, d2p0.deviceId());
+
+        installer.deactivate();
+    }
+
+    /**
+     * Checks the uninstall operation of the path intent installer.
+     */
+    @Test
+    public void uninstall() {
+        installer.activate();
+
+        List<Collection<FlowRuleOperation>> operations =
+                installer.uninstall(intent);
+        assertThat(operations, notNullValue());
+        assertThat(operations, hasSize(1));
+
+        Collection<FlowRuleOperation> flowRuleOpsCollection = operations.get(0);
+        assertThat(flowRuleOpsCollection, hasSize(hops));
+        FlowRuleOperation[] flowRuleOps =
+                flowRuleOpsCollection.toArray(new FlowRuleOperation[hops]);
+
+        FlowRuleOperation op0 = flowRuleOps[0];
+        checkFlowOperation(op0, FlowRuleOperation.Type.REMOVE, d2p0.deviceId());
+
+        installer.deactivate();
+    }
+}
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/PathIntentInstallerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/PathIntentInstallerTest.java
index e80e65f..ecdee64 100644
--- a/core/net/src/test/java/org/onosproject/net/intent/impl/PathIntentInstallerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/PathIntentInstallerTest.java
@@ -19,54 +19,31 @@
 import java.util.Collection;
 import java.util.List;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.onosproject.core.ApplicationId;
-import org.onosproject.core.CoreService;
-import org.onosproject.core.CoreServiceAdapter;
-import org.onosproject.core.IdGenerator;
-import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DefaultLink;
 import org.onosproject.net.DefaultPath;
-import org.onosproject.net.DeviceId;
 import org.onosproject.net.Link;
 import org.onosproject.net.flow.FlowRuleOperation;
-import org.onosproject.net.intent.FakeIntentManager;
-import org.onosproject.net.intent.Intent;
-import org.onosproject.net.intent.IntentInstaller;
-import org.onosproject.net.intent.IntentTestsMocks;
-import org.onosproject.net.intent.MockIdGenerator;
 import org.onosproject.net.intent.PathIntent;
 
 import com.google.common.collect.ImmutableList;
 
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.*;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.notNullValue;
 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
 import static org.onosproject.net.Link.Type.DIRECT;
 import static org.onosproject.net.NetTestTools.APP_ID;
 import static org.onosproject.net.NetTestTools.PID;
-import static org.onosproject.net.NetTestTools.connectPoint;
 
 /**
  * Unit tests for path intent installer.
  */
-public class PathIntentInstallerTest {
+public class PathIntentInstallerTest extends IntentInstallerTest {
 
-    CoreService testCoreService;
-    IdGenerator idGenerator = new MockIdGenerator();
     PathIntentInstaller installer;
 
-    private final IntentTestsMocks.MockSelector selector = new IntentTestsMocks.MockSelector();
-    private final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
-    private final ConnectPoint d1p1 = connectPoint("s1", 0);
-    private final ConnectPoint d2p0 = connectPoint("s2", 0);
-    private final ConnectPoint d2p1 = connectPoint("s2", 1);
-    private final ConnectPoint d3p1 = connectPoint("s3", 1);
-    private final ConnectPoint d3p0 = connectPoint("s3", 10);
-    private final ConnectPoint d1p0 = connectPoint("s1", 10);
-
     private final List<Link> links = Arrays.asList(
             createEdgeLink(d1p0, true),
             new DefaultLink(PID, d1p1, d2p0, DIRECT),
@@ -80,65 +57,15 @@
      * Configures objects used in all the test cases.
      */
     @Before
-    public void localSetup() {
-        testCoreService = new TestCoreService();
-        Intent.bindIdGenerator(idGenerator);
+    public void localSetUp() {
         installer = new PathIntentInstaller();
         installer.coreService = testCoreService;
-        installer.intentManager = new MockIntentManager();
+        installer.intentManager = new MockIntentManager(PathIntent.class);
         intent = new PathIntent(APP_ID, selector, treatment,
                 new DefaultPath(PID, links, hops), ImmutableList.of());
     }
 
     /**
-     * Tears down objects used in all the test cases.
-     */
-    @After
-    public void localTearDown() {
-        Intent.unbindIdGenerator(idGenerator);
-    }
-
-    /**
-     * Mock for core service.
-     */
-    private static class TestCoreService extends CoreServiceAdapter {
-
-        String registeredId = "";
-
-        @Override
-        public ApplicationId registerApplication(String identifier) {
-            registeredId = identifier;
-            return APP_ID;
-        }
-    }
-
-    /**
-     * Mock for intent manager service. Checks that the PathIntent
-     * installer installs and uninstalls properly.
-     */
-    private static class MockIntentManager extends FakeIntentManager {
-
-        boolean installerRegistered = false;
-
-        @Override
-        public <T extends Intent> void registerInstaller(
-                Class<T> cls,
-                IntentInstaller<T> installer) {
-            assertThat(cls.getCanonicalName(),
-                    equalTo("org.onosproject.net.intent.PathIntent"));
-            installerRegistered = true;
-        }
-
-        @Override
-        public <T extends Intent> void unregisterInstaller(Class<T> cls) {
-            assertThat(cls.getCanonicalName(),
-                    equalTo("org.onosproject.net.intent.PathIntent"));
-            assertThat(installerRegistered, is(true));
-        }
-
-    }
-
-    /**
      * Tests activation and deactivation of the installer.
      */
     @Test
@@ -148,20 +75,6 @@
     }
 
     /**
-     * Checks that a flow operation contains the correct values.
-     *
-     * @param op flow rule operation to check
-     * @param type type the flow rule operation should have
-     * @param deviceId device id the flow rule operation should have
-     */
-    private void checkFlowOperation(FlowRuleOperation op,
-                                    FlowRuleOperation.Type type,
-                                    DeviceId deviceId) {
-        assertThat(op.type(), is(type));
-        assertThat(op.rule().deviceId(), equalTo(deviceId));
-    }
-
-    /**
      * Tests installation operation of the path intent installer.
      */
     @Test