add PackageControllerTest as WebMvcTest through the REST API

This commit is contained in:
Michael Hoennig 2022-07-30 15:25:44 +02:00
parent 5b8688cd63
commit 2610ecd311
3 changed files with 70 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package net.hostsharing.hsadminng.hscustomer; package net.hostsharing.hsadminng.hscustomer;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*; import javax.persistence.*;
import java.util.UUID; import java.util.UUID;
@ -8,6 +10,8 @@ import java.util.UUID;
@Entity @Entity
@Table(name = "customer_rv") @Table(name = "customer_rv")
@Getter @Getter
@NoArgsConstructor
@AllArgsConstructor
public class CustomerEntity { public class CustomerEntity {
private @Id UUID uuid; private @Id UUID uuid;
private String prefix; private String prefix;

View File

@ -1,6 +1,8 @@
package net.hostsharing.hsadminng.hspackage; package net.hostsharing.hsadminng.hspackage;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import net.hostsharing.hsadminng.hscustomer.CustomerEntity; import net.hostsharing.hsadminng.hscustomer.CustomerEntity;
import javax.persistence.*; import javax.persistence.*;
@ -9,6 +11,8 @@ import java.util.UUID;
@Entity @Entity
@Table(name = "package_rv") @Table(name = "package_rv")
@Getter @Getter
@NoArgsConstructor
@AllArgsConstructor
public class PackageEntity { public class PackageEntity {
private @Id UUID uuid; private @Id UUID uuid;

View File

@ -0,0 +1,62 @@
package net.hostsharing.hsadminng.hspackage;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hscustomer.CustomerEntity;
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.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.UUID;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
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)
class PackageControllerTest {
final CustomerEntity cust = new CustomerEntity(UUID.randomUUID(), "xyz", 10001, "xyz@example.com");
final PackageEntity pac00 = new PackageEntity(UUID.randomUUID(), "xyz00", cust);
final PackageEntity pac01 = new PackageEntity(UUID.randomUUID(), "xyz01", cust);
final PackageEntity pac02 = new PackageEntity(UUID.randomUUID(), "xyz02", cust);
@Autowired
MockMvc mockMvc;
@MockBean
private Context contextMock;
@MockBean
private PackageRepository packageRepositoryMock;
@Test
void findAll() throws Exception {
// given
final var givenPacs = asList(pac00, pac01, pac02);
when(packageRepositoryMock.findAll()).thenReturn(givenPacs);
// when
final var pacs = mockMvc.perform(MockMvcRequestBuilders
.get("/api/package")
.header("current-user", "mike@hostsharing.net")
.header("assumed-roles", "customer#xyz.admin")
.accept(MediaType.APPLICATION_JSON))
// then
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].name", is("xyz00")))
.andExpect(jsonPath("$[1].uuid", is(pac01.getUuid().toString())))
.andExpect(jsonPath("$[2].customer.prefix", is("xyz")));
verify(contextMock).setCurrentUser("mike@hostsharing.net");
verify(contextMock).assumeRoles("customer#xyz.admin");
}
}