You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tz...@apache.org on 2017/09/26 16:04:51 UTC

[9/9] flink git commit: [FLINK-7647] [flip6] Introduce test base for REST response marshalling

[FLINK-7647] [flip6] Introduce test base for REST response marshalling

Introduces a common test base that for all REST responses, a subclass
should be implemented to verify that the response can be correctly
marshalled and unmarshalled.

This closes #4691.
This closes #4720.


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/4ba3eecd
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/4ba3eecd
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/4ba3eecd

Branch: refs/heads/master
Commit: 4ba3eecd8f1111c39af3f1949827b3d73addec77
Parents: 2b0008c
Author: Tzu-Li (Gordon) Tai <tz...@apache.org>
Authored: Wed Sep 20 19:07:11 2017 +0200
Committer: Tzu-Li (Gordon) Tai <tz...@apache.org>
Committed: Tue Sep 26 18:04:07 2017 +0200

----------------------------------------------------------------------
 .../messages/ClusterConfigurationInfoTest.java  | 32 +++-------
 .../messages/DashboardConfigurationTest.java    | 34 +++-------
 .../RestResponseMarshallingTestBase.java        | 65 ++++++++++++++++++++
 .../messages/StatusOverviewWithVersionTest.java | 34 +++-------
 4 files changed, 92 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/4ba3eecd/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/ClusterConfigurationInfoTest.java
----------------------------------------------------------------------
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/ClusterConfigurationInfoTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/ClusterConfigurationInfoTest.java
index 1d7d9a4..8e7092b 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/ClusterConfigurationInfoTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/ClusterConfigurationInfoTest.java
@@ -18,36 +18,22 @@
 
 package org.apache.flink.runtime.rest.handler.legacy.messages;
 
-import org.apache.flink.runtime.rest.util.RestMapperUtils;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
 /**
  * Tests for the {@link ClusterConfigurationInfo}.
  */
-public class ClusterConfigurationInfoTest {
+public class ClusterConfigurationInfoTest extends RestResponseMarshallingTestBase<ClusterConfigurationInfo> {
 
-	/**
-	 * Tests that we can marshal and unmarshal {@link ClusterConfigurationInfo} objects.
-	 */
-	@Test
-	public void testJsonMarshalling() throws JsonProcessingException {
+	@Override
+	protected Class<ClusterConfigurationInfo> getTestResponseClass() {
+		return ClusterConfigurationInfo.class;
+	}
+
+	@Override
+	protected ClusterConfigurationInfo getTestResponseInstance() {
 		final ClusterConfigurationInfo expected = new ClusterConfigurationInfo(2);
 		expected.add(new ClusterConfigurationInfoEntry("key1", "value1"));
 		expected.add(new ClusterConfigurationInfoEntry("key2", "value2"));
 
-		final ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
-
-		JsonNode marshaled = objectMapper.valueToTree(expected);
-
-		final ClusterConfigurationInfo unmarshaled = objectMapper.treeToValue(marshaled, ClusterConfigurationInfo.class);
-
-		assertEquals(expected, unmarshaled);
+		return expected;
 	}
 }

http://git-wip-us.apache.org/repos/asf/flink/blob/4ba3eecd/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/DashboardConfigurationTest.java
----------------------------------------------------------------------
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/DashboardConfigurationTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/DashboardConfigurationTest.java
index 9a9046b..bb1a6ec 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/DashboardConfigurationTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/DashboardConfigurationTest.java
@@ -18,39 +18,23 @@
 
 package org.apache.flink.runtime.rest.handler.legacy.messages;
 
-import org.apache.flink.runtime.rest.util.RestMapperUtils;
-import org.apache.flink.util.TestLogger;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
 /**
  * Tests for the {@link DashboardConfiguration}.
  */
-public class DashboardConfigurationTest extends TestLogger {
+public class DashboardConfigurationTest extends RestResponseMarshallingTestBase<DashboardConfiguration> {
+
+	@Override
+	protected Class<DashboardConfiguration> getTestResponseClass() {
+		return DashboardConfiguration.class;
+	}
 
-	/**
-	 * Tests that we can marshal and unmarshal {@link DashboardConfiguration} objects.
-	 */
-	@Test
-	public void testJsonMarshalling() throws JsonProcessingException {
-		final DashboardConfiguration expected = new DashboardConfiguration(
+	@Override
+	protected DashboardConfiguration getTestResponseInstance() {
+		return new DashboardConfiguration(
 			1L,
 			"foobar",
 			42,
 			"version",
 			"revision");
-
-		final ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
-
-		JsonNode marshaled = objectMapper.valueToTree(expected);
-
-		final DashboardConfiguration unmarshaled = objectMapper.treeToValue(marshaled, DashboardConfiguration.class);
-
-		assertEquals(expected, unmarshaled);
 	}
 }

http://git-wip-us.apache.org/repos/asf/flink/blob/4ba3eecd/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/RestResponseMarshallingTestBase.java
----------------------------------------------------------------------
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/RestResponseMarshallingTestBase.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/RestResponseMarshallingTestBase.java
new file mode 100644
index 0000000..fce60ac
--- /dev/null
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/RestResponseMarshallingTestBase.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.flink.runtime.rest.handler.legacy.messages;
+
+import org.apache.flink.runtime.rest.messages.ResponseBody;
+import org.apache.flink.runtime.rest.util.RestMapperUtils;
+import org.apache.flink.util.TestLogger;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test base for verifying that marshalling / unmarshalling REST {@link ResponseBody}s work properly.
+ */
+public abstract class RestResponseMarshallingTestBase<R extends ResponseBody> extends TestLogger {
+
+	/**
+	 * Returns the class of the test response.
+	 *
+	 * @return class of the test response type
+	 */
+	protected abstract Class<R> getTestResponseClass();
+
+	/**
+	 * Returns an instance of a response to be tested.
+	 *
+	 * @return instance of the expected test response
+	 */
+	protected abstract R getTestResponseInstance();
+
+	/**
+	 * Tests that we can marshal and unmarshal the response.
+	 */
+	@Test
+	public void testJsonMarshalling() throws JsonProcessingException {
+		final R expected = getTestResponseInstance();
+
+		ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
+		JsonNode json = objectMapper.valueToTree(expected);
+
+		final R unmarshalled = objectMapper.treeToValue(json, getTestResponseClass());
+		Assert.assertEquals(expected, unmarshalled);
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/flink/blob/4ba3eecd/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/StatusOverviewWithVersionTest.java
----------------------------------------------------------------------
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/StatusOverviewWithVersionTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/StatusOverviewWithVersionTest.java
index a1bbc9a..6b01dbe 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/StatusOverviewWithVersionTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/StatusOverviewWithVersionTest.java
@@ -18,27 +18,19 @@
 
 package org.apache.flink.runtime.rest.handler.legacy.messages;
 
-import org.apache.flink.runtime.rest.util.RestMapperUtils;
-import org.apache.flink.util.TestLogger;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
 /**
  * Tests for the {@link StatusOverviewWithVersion}.
  */
-public class StatusOverviewWithVersionTest extends TestLogger {
+public class StatusOverviewWithVersionTest extends RestResponseMarshallingTestBase<StatusOverviewWithVersion> {
+
+	@Override
+	protected Class<StatusOverviewWithVersion> getTestResponseClass() {
+		return StatusOverviewWithVersion.class;
+	}
 
-	/**
-	 * Tests that we can marshal and unmarshal StatusOverviewWithVersion.
-	 */
-	@Test
-	public void testJsonMarshalling() throws JsonProcessingException {
-		final StatusOverviewWithVersion expected = new StatusOverviewWithVersion(
+	@Override
+	protected StatusOverviewWithVersion getTestResponseInstance() {
+		return new StatusOverviewWithVersion(
 			1,
 			3,
 			3,
@@ -48,13 +40,5 @@ public class StatusOverviewWithVersionTest extends TestLogger {
 			0,
 			"version",
 			"commit");
-
-		ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
-
-		JsonNode json = objectMapper.valueToTree(expected);
-
-		final StatusOverviewWithVersion unmarshalled = objectMapper.treeToValue(json, StatusOverviewWithVersion.class);
-
-		assertEquals(expected, unmarshalled);
 	}
 }