Java tutorial
/* * Copyright (c) 2017 sainth (sainth@sainth.de) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ package de.sainth.recipe.backend.rest.controller; import com.jayway.restassured.RestAssured; import com.jayway.restassured.response.Header; import de.sainth.recipe.backend.Application; import de.sainth.recipe.backend.db.repositories.BasicUnitRepository; import de.sainth.recipe.backend.db.repositories.FoodRepository; import de.sainth.recipe.backend.rest.views.BasicUnit; import de.sainth.recipe.backend.rest.views.Food; import org.apache.http.HttpStatus; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import static com.jayway.restassured.RestAssured.given; import static com.jayway.restassured.RestAssured.when; import static com.jayway.restassured.config.JsonConfig.jsonConfig; import static com.jayway.restassured.config.RestAssuredConfig.newConfig; import static com.jayway.restassured.path.json.config.JsonPathConfig.NumberReturnType.BIG_DECIMAL; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.is; import static org.hamcrest.core.IsNull.nullValue; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class FoodControllerTest extends AuthorizedTest { private static final String BASE_URI = "/foods"; @Autowired private BasicUnitRepository basicUnitRepository; @Autowired private FoodRepository foodRepository; private BasicUnit gram; private Food apple; private Food pear; @Before public void setUp() { super.setUp(); foodRepository.deleteAll(); basicUnitRepository.deleteAll(); gram = basicUnitRepository.save(new BasicUnit("g", "gram")); apple = foodRepository.save(new Food("Apple", 1., 100., gram.getShortname())); pear = foodRepository.save(new Food("Pear", 1., 100., gram.getShortname())); RestAssured.port = port; } @After public void cleanUp() { super.cleanUp(); foodRepository.deleteAll(); basicUnitRepository.deleteAll(); } @Test public void whenAnonymousFetchesAllFoodThenResponseIsForbidden() { when().get(BASE_URI).then().statusCode(HttpStatus.SC_FORBIDDEN); } @Test public void whenUserFetchesAllFoodThenAllPresentFoodIsReturned() { given().auth().preemptive().basic(user.getUsername(), PASSWORD) .config(newConfig().jsonConfig(jsonConfig().numberReturnType(BIG_DECIMAL))).when().get(BASE_URI) .then().statusCode(HttpStatus.SC_OK).body("name", hasItems("Apple", "Pear")); } @Test public void whenAdminFetchesAllFoodThenAllPresentFoodIsReturned() { given().auth().preemptive().basic(admin.getUsername(), PASSWORD) .config(newConfig().jsonConfig(jsonConfig().numberReturnType(BIG_DECIMAL))).when().get(BASE_URI) .then().statusCode(HttpStatus.SC_OK).body("name", hasItems("Apple", "Pear")); } @Test public void whenAnonymousFetchesAppleThenResponseIsForbidden() { when().get(BASE_URI + "/{id}", apple.getId()).then().statusCode(HttpStatus.SC_FORBIDDEN); } @Test public void whenUserFetchesAppleThenAppleIsReturned() { Long appleId = apple.getId(); given().auth().preemptive().basic(user.getUsername(), PASSWORD).when().get(BASE_URI + "/{id}", appleId) .then().statusCode(HttpStatus.SC_OK).body("id", is(appleId.intValue())).body("name", is("Apple")) .body("points", is(1.0f)).body("pointsBaseAmount", is(100.0f)); } @Test public void whenAdminFetchesAppleThenGramIsReturned() { Long appleId = apple.getId(); given().auth().preemptive().basic(admin.getUsername(), PASSWORD).when().get(BASE_URI + "/{id}", appleId) .then().statusCode(HttpStatus.SC_OK).body("id", is(appleId.intValue())).body("name", is("Apple")) .body("points", is(1.0f)).body("pointsBaseAmount", is(100.0f)); } @Test public void whenAnonymousDeletesPearThenResponseIsForbidden() { when().delete(BASE_URI + "/{id}", pear.getId()).then().statusCode(HttpStatus.SC_FORBIDDEN); } @Test public void whenUserDeletesPearThenResponseIsForbidden() { given().auth().preemptive().basic(user.getUsername(), PASSWORD).when() .delete(BASE_URI + "/{id}", pear.getId()).then().statusCode(HttpStatus.SC_FORBIDDEN); } @Test public void whenAdminDeletesPearThenResponseIsNoContentAndPearWasDeleted() { given().auth().preemptive().basic(admin.getUsername(), PASSWORD).when() .delete(BASE_URI + "/{id}", pear.getId()).then().statusCode(HttpStatus.SC_NO_CONTENT); assertThat(foodRepository.findOne(pear.getId()), nullValue()); } @Test public void whenAnonymousAddsFoodThenResponseIsForbidden() { Food flour = new Food("Flour", 0., 0., gram.getShortname()); given().header(new Header("Accept", "application/json")) .header(new Header("Content-Type", "application/json")).body(flour).when().post(BASE_URI).then() .statusCode(HttpStatus.SC_FORBIDDEN); } @Test public void whenUserAddsFoodThenResponseIsForbidden() { Food flour = new Food("Flour", 0., 0., gram.getShortname()); given().auth().preemptive().basic(user.getUsername(), PASSWORD) .header(new Header("Accept", "application/json")) .header(new Header("Content-Type", "application/json")).body(flour).when().post(BASE_URI).then() .statusCode(HttpStatus.SC_FORBIDDEN); } @Test public void whenAdminAddsFlourThenFlourIsReturned() { Food flour = new Food("Flour", 0., 0., gram.getShortname()); given().auth().preemptive().basic(admin.getUsername(), PASSWORD) .header(new Header("Accept", "application/json")) .header(new Header("Content-Type", "application/json")).body(flour).when().post(BASE_URI).then() .statusCode(HttpStatus.SC_CREATED).body("name", is("Flour")).body("points", is(0.0f)) .body("pointsBaseAmount", is(0.0f)); } @Test public void whenAnonymousUpdatesAppleThenResponseIsForbidden() { apple.setPoints(0.5); given().header(new Header("Accept", "application/json")) .header(new Header("Content-Type", "application/json")).body(apple).when() .put(BASE_URI + "/{id}", apple.getId()).then().statusCode(HttpStatus.SC_FORBIDDEN); } @Test public void whenUserUpdatesAppleThenResponseIsForbidden() { apple.setPoints(0.5); given().auth().preemptive().basic(user.getUsername(), PASSWORD) .header(new Header("Accept", "application/json")) .header(new Header("Content-Type", "application/json")).body(apple).when() .put(BASE_URI + "/{id}", apple.getId()).then().statusCode(HttpStatus.SC_FORBIDDEN); } @Test public void whenAdminUpdatesAppleThenResponseIsUpdatedApple() { apple.setPoints(0.5); given().auth().preemptive().basic(admin.getUsername(), PASSWORD) .header(new Header("Accept", "application/json")) .header(new Header("Content-Type", "application/json")).body(apple).when() .put(BASE_URI + "/{id}", apple.getId()).then().statusCode(HttpStatus.SC_OK) .body("id", is(apple.getId().intValue())).body("name", is("Apple")).body("points", is(0.5f)) .body("pointsBaseAmount", is(100.0f)); Food result = foodRepository.findOne(apple.getId()); assertThat(apple.getName(), is(result.getName())); assertThat(apple.getPoints(), is(result.getPoints())); assertThat(apple.getPointsBaseAmount(), is(result.getPointsBaseAmount())); assertThat(apple.getBasicUnit(), is(result.getBasicUnit())); } @Test public void whenAdminUpdateNonExistingBasicUnitThenResponseIsBadRequest() { given().auth().preemptive().basic(admin.getUsername(), PASSWORD) .header(new Header("Accept", "application/json")) .header(new Header("Content-Type", "application/json")).body(pear).when() .put(BASE_URI + "/{id}", pear.getId() + 1).then().statusCode(HttpStatus.SC_BAD_REQUEST); } }