add updatePackage (description) using JsonNullableModule and HTTP-to-DB test with RestAssured
This commit is contained in:
@ -0,0 +1,192 @@
|
||||
package net.hostsharing.hsadminng.hs.hspackage;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import net.hostsharing.hsadminng.HsadminNgApplication;
|
||||
import net.hostsharing.hsadminng.config.JsonObjectMapperConfiguration;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static java.lang.String.format;
|
||||
import static org.assertj.core.api.Assumptions.assumeThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
@SpringBootTest(
|
||||
webEnvironment = WebEnvironment.RANDOM_PORT,
|
||||
classes = HsadminNgApplication.class
|
||||
)
|
||||
// classes = { PackageController.class, JsonObjectMapperConfiguration.class },
|
||||
@Import(JsonObjectMapperConfiguration.class)
|
||||
@Transactional
|
||||
class PackageControllerAcceptanceTest {
|
||||
|
||||
@LocalServerPort
|
||||
private Integer port;
|
||||
|
||||
@Nested
|
||||
class ListPackages {
|
||||
|
||||
@Test
|
||||
void withoutNameParameter() throws Exception {
|
||||
// @formatter:off
|
||||
RestAssured
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#aaa.admin")
|
||||
.when()
|
||||
.get("http://localhost:" + port + "/api/packages")
|
||||
.then().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("[0].name", is("aaa00"))
|
||||
.body("[0].customer.reference", is(10000))
|
||||
.body("[1].name", is("aaa01"))
|
||||
.body("[1].customer.reference", is(10000))
|
||||
.body("[2].name", is("aaa02"))
|
||||
.body("[2].customer.reference", is(10000));
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
void withNameParameter() throws Exception {
|
||||
// @formatter:off
|
||||
RestAssured
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#aaa.admin")
|
||||
.when()
|
||||
.get("http://localhost:" + port + "/api/packages?name=aaa01")
|
||||
.then().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("[0].name", is("aaa01"))
|
||||
.body("[0].customer.reference", is(10000));
|
||||
// @formatter:on
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class UpdatePackage {
|
||||
|
||||
@Test
|
||||
void withDescriptionUpdatesDescription() throws Exception {
|
||||
|
||||
assumeThat(getDescriptionOfPackage("aaa00"))
|
||||
.isEqualTo("Here can add your own description of package aaa00.");
|
||||
|
||||
final var randomDescription = RandomStringUtils.randomAlphanumeric(80);
|
||||
|
||||
// @formatter:off
|
||||
RestAssured
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#aaa.admin")
|
||||
.contentType(ContentType.JSON)
|
||||
.body(format("""
|
||||
{
|
||||
"description": "%s"
|
||||
}
|
||||
""", randomDescription))
|
||||
.when()
|
||||
.patch("http://localhost:" + port + "/api/packages/" + getUuidOfPackage("aaa00"))
|
||||
.then()
|
||||
.assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("name", is("aaa00"))
|
||||
.body("description", is(randomDescription));
|
||||
// @formatter:on
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void withNullDescriptionUpdatesDescriptionToNull() throws Exception {
|
||||
|
||||
assumeThat(getDescriptionOfPackage("aaa01"))
|
||||
.isEqualTo("Here can add your own description of package aaa01.");
|
||||
|
||||
// @formatter:off
|
||||
RestAssured
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#aaa.admin")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"description": null
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.patch("http://localhost:" + port + "/api/packages/" + getUuidOfPackage("aaa01"))
|
||||
.then()
|
||||
.assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("name", is("aaa01"))
|
||||
.body("description", equalTo(null));
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutDescriptionDoesNothing() throws Exception {
|
||||
|
||||
assumeThat(getDescriptionOfPackage("aaa02"))
|
||||
.isEqualTo("Here can add your own description of package aaa02.");
|
||||
|
||||
// @formatter:off
|
||||
RestAssured
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#aaa.admin")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("{}")
|
||||
.when()
|
||||
.patch("http://localhost:" + port + "/api/packages/" + getUuidOfPackage("aaa02"))
|
||||
.then().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("name", is("aaa02"))
|
||||
.body("description", is("Here can add your own description of package aaa02.")); // unchanged
|
||||
// @formatter:on
|
||||
}
|
||||
}
|
||||
|
||||
UUID getUuidOfPackage(final String packageName) {
|
||||
// @formatter:off
|
||||
return UUID.fromString(RestAssured
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#aaa.admin")
|
||||
.when()
|
||||
.get("http://localhost:" + port + "/api/packages?name=" + packageName)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.extract().path("[0].uuid"));
|
||||
// @formatter:om
|
||||
}
|
||||
|
||||
String getDescriptionOfPackage(final String packageName) {
|
||||
// @formatter:off
|
||||
return RestAssured
|
||||
.given()
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#aaa.admin")
|
||||
.when()
|
||||
.get("http://localhost:" + port + "/api/packages?name=" + packageName)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.extract().path("[0].description");
|
||||
// @formatter:om
|
||||
}
|
||||
}
|
@ -1,11 +1,14 @@
|
||||
package net.hostsharing.hsadminng.hs.hspackage;
|
||||
|
||||
import net.hostsharing.hsadminng.config.JsonObjectMapperConfiguration;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
@ -13,12 +16,15 @@ import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@WebMvcTest(PackageController.class)
|
||||
@ContextConfiguration(classes = { PackageController.class, JsonObjectMapperConfiguration.class })
|
||||
class PackageControllerRestTest {
|
||||
|
||||
@Autowired
|
||||
@ -28,51 +34,135 @@ class PackageControllerRestTest {
|
||||
@MockBean
|
||||
PackageRepository packageRepositoryMock;
|
||||
|
||||
@Test
|
||||
void listPackagesWithoutNameParameter() throws Exception {
|
||||
// @Autowired
|
||||
// ObjectMapper objectMapper;
|
||||
//
|
||||
// @Autowired
|
||||
// private Jackson2ObjectMapperBuilder jacksonObjectMapper;
|
||||
//
|
||||
// @Autowired
|
||||
// private PackageController restController;
|
||||
|
||||
// given
|
||||
final var givenPacs = List.of(TestPackage.xxx00, TestPackage.xxx01, TestPackage.xxx02);
|
||||
when(packageRepositoryMock.findAllByOptionalNameLike(null)).thenReturn(givenPacs);
|
||||
// @Before
|
||||
// public void init(){
|
||||
//
|
||||
// objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
|
||||
// objectMapper.registerModule(new JsonNullableModule());
|
||||
// }
|
||||
|
||||
// when
|
||||
final var pacs = mockMvc.perform(MockMvcRequestBuilders
|
||||
.get("/api/packages")
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#xxx.admin")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
@Nested
|
||||
class ListPackages {
|
||||
|
||||
// then
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", hasSize(3)))
|
||||
.andExpect(jsonPath("$[0].name", is("xxx00")))
|
||||
.andExpect(jsonPath("$[1].uuid", is(TestPackage.xxx01.getUuid().toString())))
|
||||
.andExpect(jsonPath("$[2].customer.prefix", is("xxx")));
|
||||
@Test
|
||||
void withoutNameParameter() throws Exception {
|
||||
|
||||
verify(contextMock).setCurrentUser("mike@hostsharing.net");
|
||||
verify(contextMock).assumeRoles("customer#xxx.admin");
|
||||
// given
|
||||
final var givenPacs = List.of(TestPackage.xxx00, TestPackage.xxx01, TestPackage.xxx02);
|
||||
when(packageRepositoryMock.findAllByOptionalNameLike(null)).thenReturn(givenPacs);
|
||||
|
||||
// when
|
||||
final var pacs = mockMvc.perform(MockMvcRequestBuilders
|
||||
.get("/api/packages")
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#xxx.admin")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
|
||||
// then
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", hasSize(3)))
|
||||
.andExpect(jsonPath("$[0].name", is("xxx00")))
|
||||
.andExpect(jsonPath("$[1].uuid", is(TestPackage.xxx01.getUuid().toString())))
|
||||
.andExpect(jsonPath("$[2].customer.prefix", is("xxx")));
|
||||
|
||||
verify(contextMock).setCurrentUser("mike@hostsharing.net");
|
||||
verify(contextMock).assumeRoles("customer#xxx.admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void withNameParameter() throws Exception {
|
||||
|
||||
// given
|
||||
final var givenPacs = List.of(TestPackage.xxx01);
|
||||
when(packageRepositoryMock.findAllByOptionalNameLike("xxx01")).thenReturn(givenPacs);
|
||||
|
||||
// when
|
||||
final var pacs = mockMvc.perform(MockMvcRequestBuilders
|
||||
.get("/api/packages?name=xxx01")
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#xxx.admin")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
|
||||
// then
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", hasSize(1)))
|
||||
.andExpect(jsonPath("$[0].name", is("xxx01")));
|
||||
|
||||
verify(contextMock).setCurrentUser("mike@hostsharing.net");
|
||||
verify(contextMock).assumeRoles("customer#xxx.admin");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackagesWithNameParameter() throws Exception {
|
||||
@Nested
|
||||
class updatePackage {
|
||||
|
||||
// given
|
||||
final var givenPacs = List.of(TestPackage.xxx01);
|
||||
when(packageRepositoryMock.findAllByOptionalNameLike("xxx01")).thenReturn(givenPacs);
|
||||
@Test
|
||||
void withDescriptionUpdatesDescription() throws Exception {
|
||||
|
||||
// when
|
||||
final var pacs = mockMvc.perform(MockMvcRequestBuilders
|
||||
.get("/api/packages?name=xxx01")
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#xxx.admin")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
// given
|
||||
final var givenPac = TestPackage.xxx01;
|
||||
when(packageRepositoryMock.findByUuid(givenPac.getUuid())).thenReturn(givenPac);
|
||||
when(packageRepositoryMock.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
// then
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", hasSize(1)))
|
||||
.andExpect(jsonPath("$[0].name", is("xxx01")));
|
||||
// when
|
||||
final var pacs = mockMvc.perform(MockMvcRequestBuilders
|
||||
.patch("/api/packages/" + givenPac.getUuid().toString())
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#xxx.admin")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("""
|
||||
{
|
||||
"description": "some description"
|
||||
}
|
||||
""")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
|
||||
verify(contextMock).setCurrentUser("mike@hostsharing.net");
|
||||
verify(contextMock).assumeRoles("customer#xxx.admin");
|
||||
// then
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("description", is("some description")));
|
||||
|
||||
verify(contextMock).setCurrentUser("mike@hostsharing.net");
|
||||
verify(contextMock).assumeRoles("customer#xxx.admin");
|
||||
verify(packageRepositoryMock).save(argThat(entity ->
|
||||
entity.getDescription().equals("some description") &&
|
||||
entity.getUuid().equals(givenPac.getUuid())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutDescriptionDoesNothing() throws Exception {
|
||||
|
||||
// given
|
||||
final var givenPac = TestPackage.xxx01;
|
||||
when(packageRepositoryMock.findByUuid(givenPac.getUuid())).thenReturn(givenPac);
|
||||
when(packageRepositoryMock.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
// when
|
||||
final var pacs = mockMvc.perform(MockMvcRequestBuilders
|
||||
.patch("/api/packages/" + givenPac.getUuid().toString())
|
||||
.header("current-user", "mike@hostsharing.net")
|
||||
.header("assumed-roles", "customer#xxx.admin")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{}")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
|
||||
// then
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("description", is(givenPac.getDescription())));
|
||||
|
||||
verify(contextMock).setCurrentUser("mike@hostsharing.net");
|
||||
verify(contextMock).assumeRoles("customer#xxx.admin");
|
||||
verify(packageRepositoryMock).save(argThat(entity ->
|
||||
entity.getDescription() == givenPac.getDescription() &&
|
||||
entity.getUuid().equals(givenPac.getUuid())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,6 @@ public class TestPackage {
|
||||
public static final PackageEntity xxx02 = hsPackage(TestCustomer.xxx, "xxx02");
|
||||
|
||||
public static PackageEntity hsPackage(final CustomerEntity customer, final String name) {
|
||||
return new PackageEntity(randomUUID(), name, customer);
|
||||
return new PackageEntity(randomUUID(), customer, name, "initial description of package " + name);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user