Skip to content

Commit

Permalink
[FLINK-7725] [REST] Add testbase for requests / allow exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
zentol committed Sep 28, 2017
1 parent 40948d3 commit 426137f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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:https://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.RequestBody;
import org.apache.flink.runtime.rest.util.RestMapperUtils;
import org.apache.flink.util.TestLogger;

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 RequestBody}s work properly.
*/
public abstract class RestRequestMarshallingTestBase<R extends RequestBody> extends TestLogger {

/**
* Returns the class of the test response.
*
* @return class of the test response type
*/
protected abstract Class<R> getTestRequestClass();

/**
* Returns an instance of a response to be tested.
*
* @return instance of the expected test response
*/
protected abstract R getTestRequestInstance() throws Exception;

/**
* Tests that we can marshal and unmarshal the response.
*/
@Test
public void testJsonMarshalling() throws Exception {
final R expected = getTestRequestInstance();

ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
JsonNode json = objectMapper.valueToTree(expected);

final R unmarshalled = objectMapper.treeToValue(json, getTestRequestClass());
Assert.assertEquals(expected, unmarshalled);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
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;

Expand All @@ -46,13 +44,13 @@ public abstract class RestResponseMarshallingTestBase<R extends ResponseBody> ex
*
* @return instance of the expected test response
*/
protected abstract R getTestResponseInstance();
protected abstract R getTestResponseInstance() throws Exception;

/**
* Tests that we can marshal and unmarshal the response.
*/
@Test
public void testJsonMarshalling() throws JsonProcessingException {
public void testJsonMarshalling() throws Exception {
final R expected = getTestResponseInstance();

ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();
Expand Down

0 comments on commit 426137f

Please sign in to comment.