Java tutorial
/* * Copyright 2014 the original author or authors. * * Licensed 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 com.carlomicieli.jtrains.infrastructure.web; import org.junit.Test; import org.springframework.hateoas.Link; import org.springframework.hateoas.VndErrors; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; public class ResponsesTests { @Test public void shouldCreateResponsesForErrors() { ResponseEntity<VndErrors> error = Responses.error(new RuntimeException("Error message"), HttpStatus.INTERNAL_SERVER_ERROR, "details"); assertThat(error).isNotNull(); assertThat(error.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); assertThat(error.getBody()).isEqualTo(new VndErrors("details", "Error message")); } @Test public void shouldCreateResponsesForNotFound() { ResponseEntity<VndErrors> notFound = Responses.notFound("details"); assertThat(notFound).isNotNull(); assertThat(notFound.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(notFound.getBody()).isEqualTo(new VndErrors("details", "Resource not found")); } @Test public void shouldCreateResponsesForOk() { ResponseEntity<String> resp = Responses.ok("Body"); assertThat(resp).isNotNull(); assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(resp.getBody()).isEqualTo("Body"); } @Test public void shouldCreateResponsesForCreated() { ResponseEntity<Void> resp = Responses.created(new Link("http://localhost", "self")); assertThat(resp).isNotNull(); assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.CREATED); assertThat(resp.getHeaders()).containsEntry("Location", Arrays.asList("http://localhost")); } @Test public void shouldCreateResponsesForNoContent() { ResponseEntity<Void> resp = Responses.noContent(); assertThat(resp).isNotNull(); assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); } }