提交 4ba3eecd 编写于 作者: T Tzu-Li (Gordon) Tai

[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.
上级 2b0008c5
......@@ -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;
}
}
......@@ -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);
}
}
/*
* 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);
}
}
......@@ -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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册