[ONOS-2225] Add codecs with unit test for FlowObjective REST API
* Add codec for FilteringObjective
* Add codec for ForwardingObjective
* Add codec for NextObjective
Change-Id: I715aa7f1969697468692459052fd27cc65ca2363
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveCodecTest.java
new file mode 100644
index 0000000..06c6c9c
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/NextObjectiveCodecTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.codec.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flowobjective.DefaultNextObjective;
+import org.onosproject.net.flowobjective.NextObjective;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.codec.impl.NextObjectiveJsonMatcher.matchesNextObjective;
+import static org.onosproject.net.NetTestTools.APP_ID;
+
+/**
+ * Unit tests for NextObjective Codec.
+ */
+public class NextObjectiveCodecTest {
+
+ MockCodecContext context;
+ JsonCodec<NextObjective> nextObjectiveCodec;
+ final CoreService mockCoreService = createMock(CoreService.class);
+
+ /**
+ * Sets up for each test.
+ * Creates a context and fetches the NextObjective codec.
+ */
+ @Before
+ public void setUp() {
+ context = new MockCodecContext();
+ nextObjectiveCodec = context.codec(NextObjective.class);
+ assertThat(nextObjectiveCodec, notNullValue());
+
+ expect(mockCoreService.registerApplication(NextObjectiveCodec.REST_APP_ID))
+ .andReturn(APP_ID).anyTimes();
+ replay(mockCoreService);
+ context.registerService(CoreService.class, mockCoreService);
+ }
+
+ /**
+ * Tests encoding of a NextObjective object.
+ */
+ @Test
+ public void testNextObjectiveEncode() {
+
+ TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
+
+ NextObjective nextObj = DefaultNextObjective.builder()
+ .makePermanent()
+ .withType(NextObjective.Type.HASHED)
+ .fromApp(APP_ID)
+ .withPriority(60)
+ .withId(5)
+ .addTreatment(treatment)
+ .add();
+
+ ObjectNode nextObjJson = nextObjectiveCodec.encode(nextObj, context);
+ assertThat(nextObjJson, matchesNextObjective(nextObj));
+ }
+
+ /**
+ * Test decoding of a NextObjective object.
+ */
+ @Test
+ public void testNextObjectiveDecode() throws IOException {
+ NextObjective nextObjective = getNextObjective("NextObjective.json");
+
+ assertThat(nextObjective.type(), is(NextObjective.Type.FAILOVER));
+ assertThat(nextObjective.op(), is(NextObjective.Operation.ADD));
+ }
+
+ /**
+ * Reads in a nextObjective from the given resource and decodes it.
+ *
+ * @param resourceName resource to use to read the JSON for the rule
+ * @return decoded nextObjective
+ * @throws IOException if processing the resource fails
+ */
+ private NextObjective getNextObjective(String resourceName) throws IOException {
+ InputStream jsonStream = NextObjectiveCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ NextObjective nextObjective = nextObjectiveCodec.decode((ObjectNode) json, context);
+ assertThat(nextObjective, notNullValue());
+ return nextObjective;
+ }
+}