[BAEL-10982] - Moved live tests to spring-rest-simple and added missing methods in FooController for that

This commit is contained in:
amit2103 2019-03-17 17:37:42 +05:30
parent 92809e96e3
commit 78bb153e4d
3 changed files with 44 additions and 8 deletions

View File

@ -2,12 +2,21 @@ package org.baeldung.web.controller;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import java.util.List;
import org.baeldung.web.dto.Foo; import org.baeldung.web.dto.Foo;
import org.baeldung.web.dto.FooProtos; import org.baeldung.web.dto.FooProtos;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.google.common.collect.Lists;
@Controller @Controller
public class FooController { public class FooController {
@ -16,6 +25,12 @@ public class FooController {
super(); super();
} }
@RequestMapping(method = RequestMethod.GET, value = "/foos")
@ResponseBody
public List<Foo> findListOfFoo() {
return Lists.newArrayList(new Foo(1, randomAlphabetic(4)));
}
// API - read // API - read
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
@ -32,6 +47,27 @@ public class FooController {
public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) { public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
return foo; return foo;
} }
@RequestMapping(method = RequestMethod.PATCH, value = "/foos/{id}")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Foo patchFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
return foo;
}
@RequestMapping(method = RequestMethod.POST, value = "/foos")
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public Foo postFoo(@RequestBody final Foo foo) {
return foo;
}
@RequestMapping(method = RequestMethod.HEAD, value = "/foos")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Foo headFoo() {
return new Foo(1, randomAlphabetic(4));
}
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" }) @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" })
@ResponseBody @ResponseBody

View File

@ -1,7 +1,6 @@
package org.baeldung.client; package org.baeldung.web.test;
import static org.apache.commons.codec.binary.Base64.encodeBase64; import static org.apache.commons.codec.binary.Base64.encodeBase64;
import static org.baeldung.client.Consts.APPLICATION_PORT;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
@ -14,7 +13,7 @@ import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
import java.util.Set; import java.util.Set;
import org.baeldung.resttemplate.web.dto.Foo; import org.baeldung.web.dto.Foo;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
@ -34,12 +33,13 @@ import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
public class RestTemplateBasicLiveTest { public class RestTemplateBasicLiveTest {
private RestTemplate restTemplate; private RestTemplate restTemplate;
private static final String fooResourceUrl = "http://localhost:" + APPLICATION_PORT + "/spring-rest/foos"; private static final String fooResourceUrl = "http://localhost:8082/spring-rest/foos";
@Before @Before
public void beforeTest() { public void beforeTest() {
@ -61,7 +61,7 @@ public class RestTemplateBasicLiveTest {
final RestTemplate template = new RestTemplate(); final RestTemplate template = new RestTemplate();
final ResponseEntity<String> response = template.getForEntity(fooResourceUrl + "/1", String.class); final ResponseEntity<String> response = template.getForEntity(fooResourceUrl + "/1", String.class);
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new XmlMapper();
final JsonNode root = mapper.readTree(response.getBody()); final JsonNode root = mapper.readTree(response.getBody());
final JsonNode name = root.path("name"); final JsonNode name = root.path("name");
assertThat(name.asText(), notNullValue()); assertThat(name.asText(), notNullValue());

View File

@ -1,4 +1,4 @@
package org.baeldung.client; package org.baeldung.web.test;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;