BAEL-2414 Guide to problem-spring-web (#6147)
* Fix issue with package name and folder name mismatch * Add problem-spring-web code samples * Use the latest version of the problem-spring-web library. Add spring security exceptions handling samples * Fix issue with security configuration. Fix sample for forbidden operation * Update ProblemDemoConfiguration.java * Add integration tests to validate problems are correctly handled and responses are generated using the problem library
This commit is contained in:
parent
36eac84ff9
commit
7fa2aba180
@ -1,144 +1,156 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>spring-boot-libraries</artifactId>
|
<artifactId>spring-boot-libraries</artifactId>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
<name>spring-boot-libraries</name>
|
<name>spring-boot-libraries</name>
|
||||||
<description>This is simple boot application for Spring boot actuator test</description>
|
<description>This is simple boot application for Spring boot actuator test</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>parent-boot-2</artifactId>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-boot-2</relativePath>
|
<relativePath>../parent-boot-2</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||||
<scope>test</scope>
|
</dependency>
|
||||||
</dependency>
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Problem Spring Web -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.zalando</groupId>
|
||||||
|
<artifactId>problem-spring-web</artifactId>
|
||||||
|
<version>${problem-spring-web.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- ShedLock -->
|
<!-- ShedLock -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.javacrumbs.shedlock</groupId>
|
<groupId>net.javacrumbs.shedlock</groupId>
|
||||||
<artifactId>shedlock-spring</artifactId>
|
<artifactId>shedlock-spring</artifactId>
|
||||||
<version>2.1.0</version>
|
<version>2.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.javacrumbs.shedlock</groupId>
|
<groupId>net.javacrumbs.shedlock</groupId>
|
||||||
<artifactId>shedlock-provider-jdbc-template</artifactId>
|
<artifactId>shedlock-provider-jdbc-template</artifactId>
|
||||||
<version>2.1.0</version>
|
<version>2.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>spring-boot</finalName>
|
<finalName>spring-boot</finalName>
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>src/main/resources</directory>
|
<directory>src/main/resources</directory>
|
||||||
<filtering>true</filtering>
|
<filtering>true</filtering>
|
||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>pl.project13.maven</groupId>
|
<groupId>pl.project13.maven</groupId>
|
||||||
<artifactId>git-commit-id-plugin</artifactId>
|
<artifactId>git-commit-id-plugin</artifactId>
|
||||||
<version>${git-commit-id-plugin.version}</version>
|
<version>${git-commit-id-plugin.version}</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>get-the-git-infos</id>
|
<id>get-the-git-infos</id>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>revision</goal>
|
<goal>revision</goal>
|
||||||
</goals>
|
</goals>
|
||||||
<phase>initialize</phase>
|
<phase>initialize</phase>
|
||||||
</execution>
|
</execution>
|
||||||
<execution>
|
<execution>
|
||||||
<id>validate-the-git-infos</id>
|
<id>validate-the-git-infos</id>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>validateRevision</goal>
|
<goal>validateRevision</goal>
|
||||||
</goals>
|
</goals>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
<configuration>
|
<configuration>
|
||||||
<generateGitPropertiesFile>true</generateGitPropertiesFile>
|
<generateGitPropertiesFile>true</generateGitPropertiesFile>
|
||||||
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
|
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
|
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<profiles>
|
<profiles>
|
||||||
<profile>
|
<profile>
|
||||||
<id>autoconfiguration</id>
|
<id>autoconfiguration</id>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<phase>integration-test</phase>
|
<phase>integration-test</phase>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>test</goal>
|
<goal>test</goal>
|
||||||
</goals>
|
</goals>
|
||||||
<configuration>
|
<configuration>
|
||||||
<excludes>
|
<excludes>
|
||||||
<exclude>**/*LiveTest.java</exclude>
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
<exclude>**/*IntegrationTest.java</exclude>
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
<exclude>**/*IntTest.java</exclude>
|
<exclude>**/*IntTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
<includes>
|
<includes>
|
||||||
<include>**/AutoconfigurationTest.java</include>
|
<include>**/AutoconfigurationTest.java</include>
|
||||||
</includes>
|
</includes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
<configuration>
|
<configuration>
|
||||||
<systemPropertyVariables>
|
<systemPropertyVariables>
|
||||||
<test.mime>json</test.mime>
|
<test.mime>json</test.mime>
|
||||||
</systemPropertyVariables>
|
</systemPropertyVariables>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</profile>
|
</profile>
|
||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- The main class to start by executing java -jar -->
|
<!-- The main class to start by executing java -jar -->
|
||||||
<start-class>com.baeldung.intro.App</start-class>
|
<start-class>com.baeldung.intro.App</start-class>
|
||||||
<tomee-servlet-api.version>8.5.11</tomee-servlet-api.version>
|
<tomee-servlet-api.version>8.5.11</tomee-servlet-api.version>
|
||||||
<togglz.version>2.4.1.Final</togglz.version>
|
<togglz.version>2.4.1.Final</togglz.version>
|
||||||
<rome.version>1.9.0</rome.version>
|
<rome.version>1.9.0</rome.version>
|
||||||
<chaos.monkey.version>2.0.0</chaos.monkey.version>
|
<chaos.monkey.version>2.0.0</chaos.monkey.version>
|
||||||
<graphql-spring-boot-starter.version>5.0.2</graphql-spring-boot-starter.version>
|
<graphql-spring-boot-starter.version>5.0.2</graphql-spring-boot-starter.version>
|
||||||
<graphiql-spring-boot-starter.version>5.0.2</graphiql-spring-boot-starter.version>
|
<graphiql-spring-boot-starter.version>5.0.2</graphiql-spring-boot-starter.version>
|
||||||
<graphql-java-tools.version>5.2.4</graphql-java-tools.version>
|
<graphql-java-tools.version>5.2.4</graphql-java-tools.version>
|
||||||
<guava.version>18.0</guava.version>
|
<guava.version>18.0</guava.version>
|
||||||
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
|
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
|
||||||
<modelmapper.version>2.3.2</modelmapper.version>
|
<modelmapper.version>2.3.2</modelmapper.version>
|
||||||
</properties>
|
<problem-spring-web.version>0.23.0</problem-spring-web.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.boot;
|
package com.baeldung.boot;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.boot.problem;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableAutoConfiguration(exclude = ErrorMvcAutoConfiguration.class)
|
||||||
|
@ComponentScan("com.baeldung.boot.problem")
|
||||||
|
public class SpringProblemApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.setProperty("spring.profiles.active", "problem");
|
||||||
|
SpringApplication.run(SpringProblemApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.boot.problem.advice;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.zalando.problem.spring.web.advice.ProblemHandling;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class ExceptionHandler implements ProblemHandling {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.boot.problem.advice;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.zalando.problem.spring.web.advice.security.SecurityAdviceTrait;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class SecurityExceptionHandler implements SecurityAdviceTrait {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.boot.problem.configuration;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.zalando.problem.ProblemModule;
|
||||||
|
import org.zalando.problem.validation.ConstraintViolationProblemModule;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class ProblemDemoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ObjectMapper objectMapper() {
|
||||||
|
return new ObjectMapper().registerModules(new ProblemModule(), new ConstraintViolationProblemModule());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.boot.problem.configuration;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.zalando.problem.spring.web.advice.security.SecurityProblemSupport;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebSecurity
|
||||||
|
@Import(SecurityProblemSupport.class)
|
||||||
|
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SecurityProblemSupport problemSupport;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
http.csrf().disable();
|
||||||
|
|
||||||
|
http.authorizeRequests()
|
||||||
|
.antMatchers("/")
|
||||||
|
.permitAll();
|
||||||
|
|
||||||
|
http.exceptionHandling()
|
||||||
|
.authenticationEntryPoint(problemSupport)
|
||||||
|
.accessDeniedHandler(problemSupport);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
package com.baeldung.boot.problem.controller;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.baeldung.boot.problem.dto.Task;
|
||||||
|
import com.baeldung.boot.problem.problems.TaskNotFoundProblem;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/tasks")
|
||||||
|
public class ProblemDemoController {
|
||||||
|
|
||||||
|
private static final Map<Long, Task> MY_TASKS;
|
||||||
|
|
||||||
|
static {
|
||||||
|
MY_TASKS = new HashMap<>();
|
||||||
|
MY_TASKS.put(1L, new Task(1L, "My first task"));
|
||||||
|
MY_TASKS.put(2L, new Task(2L, "My second task"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
public List<Task> getTasks() {
|
||||||
|
return new ArrayList<>(MY_TASKS.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
public Task getTasks(@PathVariable("id") Long taskId) {
|
||||||
|
if (MY_TASKS.containsKey(taskId)) {
|
||||||
|
return MY_TASKS.get(taskId);
|
||||||
|
} else {
|
||||||
|
throw new TaskNotFoundProblem(taskId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public void updateTask(@PathVariable("id") Long id) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void deleteTask(@PathVariable("id") Long id) {
|
||||||
|
throw new AccessDeniedException("You can't delete this task");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.boot.problem.dto;
|
||||||
|
|
||||||
|
public class Task {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
public Task() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task(Long id, String description) {
|
||||||
|
this.id = id;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.boot.problem.problems;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import org.zalando.problem.AbstractThrowableProblem;
|
||||||
|
import org.zalando.problem.Status;
|
||||||
|
|
||||||
|
public class TaskNotFoundProblem extends AbstractThrowableProblem {
|
||||||
|
|
||||||
|
private static final URI TYPE = URI.create("https://example.org/not-found");
|
||||||
|
|
||||||
|
public TaskNotFoundProblem(Long taskId) {
|
||||||
|
super(TYPE, "Not found", Status.NOT_FOUND, String.format("Task '%s' not found", taskId));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
spring.resources.add-mappings=false
|
||||||
|
spring.mvc.throw-exception-if-no-handler-found=true
|
||||||
|
spring.http.encoding.force=true
|
@ -0,0 +1,75 @@
|
|||||||
|
package com.baeldung.boot.problem.controller;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import com.baeldung.boot.problem.SpringProblemApplication;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = SpringProblemApplication.class)
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
public class ProblemDemoControllerIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRequestingAllTasks_thenReturnSuccessfulResponseWithArrayWithTwoTasks() throws Exception {
|
||||||
|
mockMvc.perform(get("/tasks").contentType(MediaType.APPLICATION_JSON_VALUE))
|
||||||
|
.andDo(print())
|
||||||
|
.andExpect(jsonPath("$.length()", equalTo(2)))
|
||||||
|
.andExpect(status().isOk());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRequestingExistingTask_thenReturnSuccessfulResponse() throws Exception {
|
||||||
|
mockMvc.perform(get("/tasks/1").contentType(MediaType.APPLICATION_JSON_VALUE))
|
||||||
|
.andDo(print())
|
||||||
|
.andExpect(jsonPath("$.id", equalTo(1)))
|
||||||
|
.andExpect(status().isOk());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRequestingMissingTask_thenReturnNotFoundProblemResponse() throws Exception {
|
||||||
|
mockMvc.perform(get("/tasks/5").contentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE))
|
||||||
|
.andDo(print())
|
||||||
|
.andExpect(jsonPath("$.title", equalTo("Not found")))
|
||||||
|
.andExpect(jsonPath("$.status", equalTo(404)))
|
||||||
|
.andExpect(jsonPath("$.detail", equalTo("Task '5' not found")))
|
||||||
|
.andExpect(status().isNotFound());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMakePutCall_thenReturnNotImplementedProblemResponse() throws Exception {
|
||||||
|
mockMvc.perform(put("/tasks/1").contentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE))
|
||||||
|
.andDo(print())
|
||||||
|
.andExpect(jsonPath("$.title", equalTo("Not Implemented")))
|
||||||
|
.andExpect(jsonPath("$.status", equalTo(501)))
|
||||||
|
.andExpect(status().isNotImplemented());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMakeDeleteCall_thenReturnForbiddenProblemResponse() throws Exception {
|
||||||
|
mockMvc.perform(delete("/tasks/2").contentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE))
|
||||||
|
.andDo(print())
|
||||||
|
.andExpect(jsonPath("$.title", equalTo("Forbidden")))
|
||||||
|
.andExpect(jsonPath("$.status", equalTo(403)))
|
||||||
|
.andExpect(jsonPath("$.detail", equalTo("You can't delete this task")))
|
||||||
|
.andExpect(status().isForbidden());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user