From 4ba3eecd8f1111c39af3f1949827b3d73addec77 Mon Sep 17 00:00:00 2001 From: "Tzu-Li (Gordon) Tai" Date: Wed, 20 Sep 2017 19:07:11 +0200 Subject: [PATCH] [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. --- .../ClusterConfigurationInfoTest.java | 32 +++------ .../messages/DashboardConfigurationTest.java | 34 +++------- .../RestResponseMarshallingTestBase.java | 65 +++++++++++++++++++ .../StatusOverviewWithVersionTest.java | 34 +++------- 4 files changed, 92 insertions(+), 73 deletions(-) create mode 100644 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/ClusterConfigurationInfoTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/legacy/messages/ClusterConfigurationInfoTest.java index 1d7d9a40302..8e7092b2f4d 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 { - /** - * Tests that we can marshal and unmarshal {@link ClusterConfigurationInfo} objects. - */ - @Test - public void testJsonMarshalling() throws JsonProcessingException { + @Override + protected Class 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; } } 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 9a9046b6280..bb1a6ec9b7b 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 { + + @Override + protected Class 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); } } 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 00000000000..fce60ac8150 --- /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 extends TestLogger { + + /** + * Returns the class of the test response. + * + * @return class of the test response type + */ + protected abstract Class 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); + } + +} 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 a1bbc9a2227..6b01dbe9526 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 { + + @Override + protected Class 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); } } -- GitLab