[ONOS-4160] pce rest and cli

Change-Id: Icc1ec3070fe595bfc1439048234d6a6f23127c13
diff --git a/apps/pce/src/test/java/org/onosproject/pce/pceservice/DefaultPcePathTest.java b/apps/pce/src/test/java/org/onosproject/pce/pceservice/DefaultPcePathTest.java
new file mode 100644
index 0000000..2f5347d
--- /dev/null
+++ b/apps/pce/src/test/java/org/onosproject/pce/pceservice/DefaultPcePathTest.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2016 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.pce.pceservice;
+
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import org.onosproject.incubator.net.tunnel.TunnelId;
+
+/**
+ * Unit tests for DefaultPcePath class.
+ */
+public class DefaultPcePathTest {
+    /**
+     * Checks the operation of equals() methods.
+     */
+    @Test
+    public void testEquals() {
+        // create same two pce-path objects.
+        final String cost1 = "1";
+        final String bandwidth1 = "200";
+        final String src1 = "foo";
+        final String dst1 = "bee";
+        final String type1 = "1";
+        final String name1 = "pcc";
+
+        PcePath path1 = DefaultPcePath.builder()
+                .source(src1)
+                .destination(dst1)
+                .lspType(type1)
+                .name(name1)
+                .costConstraint(cost1)
+                .bandwidthConstraint(bandwidth1)
+                .build();
+        path1.id(TunnelId.valueOf("1"));
+
+        // create same as above object
+        PcePath samePath1 = DefaultPcePath.builder()
+                .source(src1)
+                .destination(dst1)
+                .lspType(type1)
+                .name(name1)
+                .costConstraint(cost1)
+                .bandwidthConstraint(bandwidth1)
+                .build();
+        samePath1.id(TunnelId.valueOf("1"));
+
+        // Create different pce-path object.
+        final String cost2 = "1";
+        final String bandwidth2 = "200";
+        final String src2 = "google";
+        final String dst2 = "yahoo";
+        final String type2 = "2";
+        final String name2 = "pcc2";
+
+        PcePath path2 = DefaultPcePath.builder()
+                .source(src2)
+                .destination(dst2)
+                .lspType(type2)
+                .name(name2)
+                .costConstraint(cost2)
+                .bandwidthConstraint(bandwidth2)
+                .build();
+        path2.id(TunnelId.valueOf("2"));
+        //TODO: will be uncommented below line once CostConstraint and LocalBandwidthConstraint classes are ready
+        //new EqualsTester().addEqualityGroup(path1, samePath1).addEqualityGroup(path2).testEquals();
+    }
+
+    /**
+     * Checks the construction of a DefaultPcePath object.
+     */
+    @Test
+    public void testConstruction() {
+        final String cost = "1";
+        final String bandwidth = "600";
+        final String src = "indiatimes";
+        final String dst = "deccan";
+        final String type = "3";
+        final String name = "pcc4";
+
+        PcePath path = DefaultPcePath.builder()
+                .source(src)
+                .destination(dst)
+                .lspType(type)
+                .name(name)
+                .costConstraint(cost)
+                .bandwidthConstraint(bandwidth)
+                .build();
+
+        assertThat(src, is(path.source()));
+        assertThat(dst, is(path.destination()));
+        assertThat(LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR, is(path.lspType()));
+        assertThat(name, is(path.name()));
+        //TODO: will be uncommented below lines once CostConstraint and LocalBandwidthConstraint classes are ready
+        //assertThat(cost, is(path.costConstraint().toString()));
+        //assertThat(bandwidth, is(path.bandwidthConstraint().toString()));
+    }
+}
diff --git a/apps/pce/src/test/java/org/onosproject/pce/web/MockPceCodecContext.java b/apps/pce/src/test/java/org/onosproject/pce/web/MockPceCodecContext.java
new file mode 100644
index 0000000..1aa90aa
--- /dev/null
+++ b/apps/pce/src/test/java/org/onosproject/pce/web/MockPceCodecContext.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2016 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.pce.web;
+
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * Mock codec context for use in codec unit tests.
+ */
+public class MockPceCodecContext implements CodecContext {
+
+    private final ObjectMapper mapper = new ObjectMapper();
+    private final CodecManager codecManager = new CodecManager();
+    private final PceCodecRegistrator manager = new PceCodecRegistrator();
+
+    /**
+     * Constructs a new mock codec context.
+     */
+    public MockPceCodecContext() {
+        codecManager.activate();
+        manager.codecService = codecManager;
+        manager.activate();
+    }
+
+    @Override
+    public ObjectMapper mapper() {
+        return mapper;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <T> T getService(Class<T> serviceClass) {
+        // TODO
+        return null;
+    }
+
+    @Override
+    public <T> JsonCodec<T> codec(Class<T> entityClass) {
+        return codecManager.getCodec(entityClass);
+    }
+
+    /**
+     * Get the codec manager.
+     *
+     * @return instance of codec manager
+     */
+    public CodecService codecManager() {
+        return codecManager;
+    }
+}
diff --git a/apps/pce/src/test/java/org/onosproject/pce/web/PcePathCodecTest.java b/apps/pce/src/test/java/org/onosproject/pce/web/PcePathCodecTest.java
new file mode 100644
index 0000000..64f1c48
--- /dev/null
+++ b/apps/pce/src/test/java/org/onosproject/pce/web/PcePathCodecTest.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2016 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.pce.web;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.pce.pceservice.PcePath;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * PCE path codec unit tests.
+ */
+public class PcePathCodecTest {
+
+    MockPceCodecContext context;
+    JsonCodec<PcePath> pcePathCodec;
+    /**
+     * Sets up for each test. Creates a context and fetches the PCE path codec.
+     */
+    @Before
+    public void setUp() {
+        context = new MockPceCodecContext();
+        pcePathCodec = context.codec(PcePath.class);
+        assertThat(pcePathCodec, notNullValue());
+    }
+
+    /**
+     * Reads in a pce-path from the given resource and decodes it.
+     *
+     * @param resourceName resource to use to read the json for the pce-path
+     * @return decoded pce-path
+     * @throws IOException if processing the resource fails
+     */
+    private PcePath getPcePath(String resourceName) throws IOException {
+        InputStream jsonStream = PcePathCodecTest.class
+                .getResourceAsStream(resourceName);
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode json = mapper.readTree(jsonStream);
+        assertThat(json, notNullValue());
+        PcePath pcePath = pcePathCodec.decode((ObjectNode) json, context);
+        assertThat(pcePath, notNullValue());
+        return pcePath;
+    }
+
+    /**
+     * Checks that a simple pce-path is decoded properly.
+     *
+     * @throws IOException if the resource cannot be processed
+     */
+    @Test
+    public void codecPcePathTest() throws IOException {
+
+        PcePath pcePath = getPcePath("pcePath.json");
+
+        assertThat(pcePath, notNullValue());
+
+        assertThat(pcePath.source().toString(), is("11.0.0.1"));
+        assertThat(pcePath.destination(), is("11.0.0.2"));
+        assertThat(pcePath.lspType().toString(), is("SR_WITHOUT_SIGNALLING"));
+        //TODO: uncomment below lines once CostConstraint and LocalBandwidthConstraint are ready
+        //assertThat(pcePath.costConstraint().toString(), is(2));
+        //assertThat(pcePath.bandwidthConstraint().toString(), is(200.0));
+    }
+}
diff --git a/apps/pce/src/test/java/org/onosproject/pce/web/PcePathResourceTest.java b/apps/pce/src/test/java/org/onosproject/pce/web/PcePathResourceTest.java
new file mode 100644
index 0000000..181f864
--- /dev/null
+++ b/apps/pce/src/test/java/org/onosproject/pce/web/PcePathResourceTest.java
@@ -0,0 +1,306 @@
+/*
+ * Copyright 2016 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.pce.web;
+
+//import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createMock;
+//import static org.easymock.EasyMock.expect;
+//import static org.easymock.EasyMock.replay;
+//import static org.hamcrest.Matchers.containsString;
+//import static org.hamcrest.Matchers.is;
+//import static org.hamcrest.Matchers.notNullValue;
+//import static org.junit.Assert.assertThat;
+//import static org.junit.Assert.fail;
+
+//import javax.ws.rs.NotFoundException;
+//import javax.ws.rs.client.Entity;
+//import javax.ws.rs.client.WebTarget;
+//import javax.ws.rs.core.MediaType;
+//import javax.ws.rs.core.Response;
+//import java.io.InputStream;
+//import java.net.HttpURLConnection;
+//import java.util.HashSet;
+//import java.util.List;
+//import java.util.Objects;
+//import java.util.Optional;
+//import java.util.Set;
+
+//import com.eclipsesource.json.Json;
+//import com.eclipsesource.json.JsonObject;
+//import com.google.common.collect.ImmutableList;
+//import com.google.common.collect.Lists;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.rest.BaseResource;
+import org.onosproject.codec.CodecService;
+import org.onosproject.incubator.net.tunnel.TunnelId;
+import org.onosproject.net.intent.Constraint;
+import org.onosproject.pce.pceservice.PcePath;
+import org.onosproject.pce.pceservice.LspType;
+import org.onosproject.pce.pceservice.api.PceService;
+
+/**
+ * Unit tests for pce path REST APIs.
+ */
+public class PcePathResourceTest extends PceResourceTest {
+    final PceService pceService = createMock(PceService.class);
+    final TunnelId pcePathId1 = TunnelId.valueOf("1");
+    //TODO: will be uncommented below lines once CostConstraint and LocalBandwidthConstraint classes are ready
+    final Constraint costConstraint = null; //CostConstraint.of("2");
+    final Constraint bandwidthConstraint = null; //LocalBandwidthConstraint.of("200.0");
+    final LspType lspType = LspType.WITH_SIGNALLING;
+    final MockPcePath pcePath1 = new MockPcePath(pcePathId1, "11.0.0.1", "11.0.0.2", lspType, "pcc2",
+                                                 costConstraint, bandwidthConstraint);
+
+    /**
+     * Mock class for a pce path.
+     */
+    private static class MockPcePath implements PcePath {
+        private TunnelId id;
+        private String source;
+        private String destination;
+        private LspType lspType;
+        private String name;
+        private Constraint costConstraint;
+        private Constraint bandwidthConstraint;
+
+        /**
+         * Constructor to initialize member variables.
+         *
+         * @param id pce path id
+         * @param src source device
+         * @param dst destination device
+         * @param type lsp type
+         * @param name symbolic path name
+         * @param constrnt pce constraint
+         */
+        public MockPcePath(TunnelId id, String src, String dst, LspType type, String name,
+                           Constraint costConstrnt, Constraint bandwidthConstrnt) {
+            this.id = id;
+            this.source = src;
+            this.destination = dst;
+            this.name = name;
+            this.lspType = type;
+            this.costConstraint = costConstrnt;
+            this.bandwidthConstraint = bandwidthConstrnt;
+        }
+
+        @Override
+        public TunnelId id() {
+            return id;
+        }
+
+        @Override
+        public void id(TunnelId id) {
+            this.id = id;
+        }
+
+        @Override
+        public String source() {
+            return source;
+        }
+
+        @Override
+        public void source(String src) {
+            this.source = src;
+        }
+
+        @Override
+        public String destination() {
+            return destination;
+        }
+
+        @Override
+        public void destination(String dst) {
+            this.destination = dst;
+        }
+
+        @Override
+        public LspType lspType() {
+            return lspType;
+        }
+
+        @Override
+        public String name() {
+            return name;
+        }
+
+        @Override
+        public Constraint costConstraint() {
+            return costConstraint;
+        }
+
+        @Override
+        public Constraint bandwidthConstraint() {
+            return bandwidthConstraint;
+        }
+
+        @Override
+        public PcePath copy(PcePath path) {
+            if (null != path.source()) {
+                this.source = path.source();
+            }
+            if (null != path.destination()) {
+                this.destination = path.destination();
+            }
+            if (this.lspType != path.lspType()) {
+                this.lspType = path.lspType();
+            }
+            if (null != path.name()) {
+                this.name = path.name();
+            }
+            if (null != path.costConstraint()) {
+                this.costConstraint = path.costConstraint();
+            }
+            if (null != path.bandwidthConstraint()) {
+                this.bandwidthConstraint = path.bandwidthConstraint();
+            }
+            return this;
+        }
+    }
+
+    /**
+     * Sets up the global values for all the tests.
+     */
+    @Before
+    public void setUpTest() {
+        MockPceCodecContext context = new MockPceCodecContext();
+        ServiceDirectory testDirectory = new TestServiceDirectory().add(PceService.class, pceService)
+                .add(CodecService.class, context.codecManager());
+        BaseResource.setServiceDirectory(testDirectory);
+    }
+
+    /**
+     * Cleans up.
+     */
+    @After
+    public void tearDownTest() {
+    }
+
+    /**
+     * Tests the result of the rest api GET when there are no pce paths.
+     */
+    @Test
+    public void testPcePathsEmpty() {
+        //TODO: will be uncommented below code once PceService is ready
+        //expect(pceService.queryAllPath()).andReturn(null).anyTimes();
+        //replay(pceService);
+        //final WebTarget wt = target();
+        //final String response = wt.path("path").request().get(String.class);
+        //assertThat(response, is("{\"paths\":[]}"));
+    }
+
+    /**
+     * Tests the result of a rest api GET for pce path id.
+     */
+    @Test
+    public void testGetTunnelId() {
+        //TODO: will be uncommented below code once PceService is ready
+        //final Set<PcePath> pcePaths = new HashSet<>();
+        //pcePaths.add(pcePath1);
+
+        //expect(pceService.queryPath(anyObject())).andReturn(pcePath1).anyTimes();
+        //replay(pceService);
+
+        //final WebTarget wt = target();
+        //final String response = wt.path("path/1").request().get(String.class);
+        //final JsonObject result = Json.parse(response).asObject();
+        //assertThat(result, notNullValue());
+    }
+
+    /**
+     * Tests that a fetch of a non-existent pce path object throws an exception.
+     */
+    @Test
+    public void testBadGet() {
+        //TODO: will be uncommented below code once PceService is ready
+        //expect(pceService.queryPath(anyObject()))
+        //        .andReturn(null).anyTimes();
+        //replay(pceService);
+
+        //WebTarget wt = target();
+        //try {
+        //    wt.path("path/1").request().get(String.class);
+        //    fail("Fetch of non-existent pce path did not throw an exception");
+        //} catch (NotFoundException ex) {
+        //    assertThat(ex.getMessage(),
+        //               containsString("HTTP 404 Not Found"));
+        //}
+    }
+
+    /**
+     * Tests creating a pce path with POST.
+     */
+    @Test
+    public void testPost() {
+        //TODO: will be uncommented below code once PceService is ready
+        //expect(pceService.setupPath(anyObject()))
+        //        .andReturn(true).anyTimes();
+        //replay(pceService);
+
+        //WebTarget wt = target();
+        //InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
+
+        //Response response = wt.path("path")
+        //        .request(MediaType.APPLICATION_JSON_TYPE)
+        //        .post(Entity.json(jsonStream));
+        //assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
+    }
+
+    /**
+     * Tests creating a pce path with PUT.
+     */
+    @Test
+    public void testPut() {
+        //TODO: will be uncommented below code once PceService is ready
+        //expect(pceService.updatePath(anyObject()))
+        //        .andReturn(true).anyTimes();
+        //replay(pceService);
+
+        //WebTarget wt = target();
+        //InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
+
+        //Response response = wt.path("path/1")
+        //        .request(MediaType.APPLICATION_JSON_TYPE)
+        //        .put(Entity.json(jsonStream));
+        //assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
+    }
+
+    /**
+     * Tests deleting a pce path.
+     */
+    @Test
+    public void testDelete() {
+        //TODO: will be uncommented below code once PceService is ready
+        //expect(pceService.releasePath(anyObject()))
+        //        .andReturn(true).anyTimes();
+        //replay(pceService);
+
+        //WebTarget wt = target();
+
+        //String location = "path/1";
+
+        //Response deleteResponse = wt.path(location)
+        //        .request(MediaType.APPLICATION_JSON_TYPE)
+        //        .delete();
+        //assertThat(deleteResponse.getStatus(),
+        //           is(HttpURLConnection.HTTP_OK));
+    }
+}
diff --git a/apps/pce/src/test/java/org/onosproject/pce/web/PceResourceTest.java b/apps/pce/src/test/java/org/onosproject/pce/web/PceResourceTest.java
new file mode 100644
index 0000000..d7a29f5
--- /dev/null
+++ b/apps/pce/src/test/java/org/onosproject/pce/web/PceResourceTest.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2016 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.pce.web;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.test.JerseyTest;
+
+/**
+ * Base class for pce rest api tests.  Performs common configuration operations.
+ */
+public class PceResourceTest extends JerseyTest {
+
+    /**
+     * Creates a new web-resource test.
+     */
+    public PceResourceTest() {
+        super(ResourceConfig.forApplicationClass(PceWebApplication.class));
+    }
+}