Skip to content

Commit

Permalink
feat : group tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 3, 2023
1 parent cb87ffb commit 1b7b2e2
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -56,33 +58,36 @@ void shouldFetchAllCustomers() throws Exception {
.andExpect(jsonPath("$.hasPrevious", is(false)));
}

@Test
void shouldFindCustomerById() throws Exception {
Customer customer = customerList.get(0);
Long customerId = customer.getId();

this.mockMvc
.perform(get("/api/customers/{id}", customerId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(customer.getId()), Long.class))
.andExpect(jsonPath("$.name", is(customer.getName())))
.andExpect(jsonPath("$.address", is(customer.getAddress())));
}

@Test
void shouldFindCustomerRevisionsById() throws Exception {
Customer customer = customerList.get(0);
Long customerId = customer.getId();

this.mockMvc
.perform(get("/api/customers/{id}/revisions", customerId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.size()", is(1)))
.andExpect(jsonPath("$[0].entity.id", is(customer.getId()), Long.class))
.andExpect(jsonPath("$[0].entity.name", is(customer.getName())))
.andExpect(jsonPath("$[0].entity.address", is(customer.getAddress())))
.andExpect(jsonPath("$[0].revisionNumber", notNullValue()))
.andExpect(jsonPath("$[0].revisionType", is("INSERT")));
@Nested
@DisplayName("find methods")
class Find {

@Test
void shouldFindCustomerById() throws Exception {
Customer customer = customerList.get(0);
Long customerId = customer.getId();

mockMvc.perform(get("/api/customers/{id}", customerId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(customer.getId()), Long.class))
.andExpect(jsonPath("$.name", is(customer.getName())))
.andExpect(jsonPath("$.address", is(customer.getAddress())));
}

@Test
void shouldFindCustomerRevisionsById() throws Exception {
Customer customer = customerList.get(0);
Long customerId = customer.getId();

mockMvc.perform(get("/api/customers/{id}/revisions", customerId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.size()", is(1)))
.andExpect(jsonPath("$[0].entity.id", is(customer.getId()), Long.class))
.andExpect(jsonPath("$[0].entity.name", is(customer.getName())))
.andExpect(jsonPath("$[0].entity.address", is(customer.getAddress())))
.andExpect(jsonPath("$[0].revisionNumber", notNullValue()))
.andExpect(jsonPath("$[0].revisionType", is("INSERT")));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
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;
Expand Down Expand Up @@ -83,136 +85,144 @@ void shouldFetchAllCustomers() throws Exception {
.andExpect(jsonPath("$.hasPrevious", is(false)));
}

@Test
void shouldFindCustomerById() throws Exception {
Long customerId = 1L;
CustomerResponse customer = new CustomerResponse(customerId, "text 1", "Junit Address");
given(customerService.findCustomerById(customerId)).willReturn(Optional.of(customer));

this.mockMvc
.perform(get("/api/customers/{id}", customerId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(customer.name())))
.andExpect(jsonPath("$.address", is(customer.address())));
}

@Test
void shouldReturn404WhenFetchingNonExistingCustomer() throws Exception {
Long customerId = 1L;
given(customerService.findCustomerById(customerId)).willReturn(Optional.empty());

this.mockMvc
.perform(get("/api/customers/{id}", customerId))
.andExpect(status().isNotFound())
.andExpect(header().string("Content-Type", is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("http://api.boot-data-envers.com/errors/not-found")))
.andExpect(jsonPath("$.title", is("Not Found")))
.andExpect(jsonPath("$.status", is(404)))
.andExpect(jsonPath("$.detail").value("Customer with Id '%d' not found".formatted(customerId)));
}

@Test
void shouldCreateNewCustomer() throws Exception {

CustomerResponse customer = new CustomerResponse(1L, "some text", "Junit Address");
CustomerRequest customerRequest = new CustomerRequest("some text", "Junit Address");
given(customerService.saveCustomer(any(CustomerRequest.class))).willReturn(customer);

this.mockMvc
.perform(post("/api/customers")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isCreated())
.andExpect(header().exists(HttpHeaders.LOCATION))
.andExpect(jsonPath("$.id", notNullValue()))
.andExpect(jsonPath("$.name", is(customer.name())))
.andExpect(jsonPath("$.address", is(customer.address())));
}

@Test
void shouldReturn400WhenCreateNewCustomerWithoutName() throws Exception {
CustomerRequest customerRequest = new CustomerRequest(null, null);

this.mockMvc
.perform(post("/api/customers")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isBadRequest())
.andExpect(header().string("Content-Type", is("application/problem+json")))
.andExpect(jsonPath("$.type", is("about:blank")))
.andExpect(jsonPath("$.title", is("Constraint Violation")))
.andExpect(jsonPath("$.status", is(400)))
.andExpect(jsonPath("$.detail", is("Invalid request content.")))
.andExpect(jsonPath("$.instance", is("/api/customers")))
.andExpect(jsonPath("$.violations", hasSize(1)))
.andExpect(jsonPath("$.violations[0].field", is("name")))
.andExpect(jsonPath("$.violations[0].message", is("Name cannot be empty")))
.andReturn();
@Nested
@DisplayName("find methods")
class Find {
@Test
void shouldFindCustomerById() throws Exception {
Long customerId = 1L;
CustomerResponse customer = new CustomerResponse(customerId, "text 1", "Junit Address");
given(customerService.findCustomerById(customerId)).willReturn(Optional.of(customer));

mockMvc.perform(get("/api/customers/{id}", customerId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(customer.name())))
.andExpect(jsonPath("$.address", is(customer.address())));
}

@Test
void shouldReturn404WhenFetchingNonExistingCustomer() throws Exception {
Long customerId = 1L;
given(customerService.findCustomerById(customerId)).willReturn(Optional.empty());

mockMvc.perform(get("/api/customers/{id}", customerId))
.andExpect(status().isNotFound())
.andExpect(header().string("Content-Type", is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("http://api.boot-data-envers.com/errors/not-found")))
.andExpect(jsonPath("$.title", is("Not Found")))
.andExpect(jsonPath("$.status", is(404)))
.andExpect(jsonPath("$.detail").value("Customer with Id '%d' not found".formatted(customerId)));
}
}

@Test
void shouldUpdateCustomer() throws Exception {
Long customerId = 1L;
CustomerResponse customer = new CustomerResponse(customerId, "Updated text", "Junit Address");
CustomerRequest customerRequest = new CustomerRequest("Updated text", "Junit Address");
given(customerService.updateCustomer(eq(customerId), any(CustomerRequest.class)))
.willReturn(customer);

this.mockMvc
.perform(put("/api/customers/{id}", customerId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(customerId), Long.class))
.andExpect(jsonPath("$.name", is(customer.name())))
.andExpect(jsonPath("$.address", is(customer.address())));
}

@Test
void shouldReturn404WhenUpdatingNonExistingCustomer() throws Exception {
Long customerId = 1L;
CustomerRequest customerRequest = new CustomerRequest("Updated text", "Junit Address");
given(customerService.updateCustomer(eq(customerId), any(CustomerRequest.class)))
.willThrow(new CustomerNotFoundException(customerId));

this.mockMvc
.perform(put("/api/customers/{id}", customerId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isNotFound())
.andExpect(header().string("Content-Type", is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("http://api.boot-data-envers.com/errors/not-found")))
.andExpect(jsonPath("$.title", is("Not Found")))
.andExpect(jsonPath("$.status", is(404)))
.andExpect(jsonPath("$.detail").value("Customer with Id '%d' not found".formatted(customerId)));
@Nested
@DisplayName("save methods")
class Save {
@Test
void shouldCreateNewCustomer() throws Exception {

CustomerResponse customer = new CustomerResponse(1L, "some text", "Junit Address");
CustomerRequest customerRequest = new CustomerRequest("some text", "Junit Address");
given(customerService.saveCustomer(any(CustomerRequest.class))).willReturn(customer);

mockMvc.perform(post("/api/customers")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isCreated())
.andExpect(header().exists(HttpHeaders.LOCATION))
.andExpect(jsonPath("$.id", notNullValue()))
.andExpect(jsonPath("$.name", is(customer.name())))
.andExpect(jsonPath("$.address", is(customer.address())));
}

@Test
void shouldReturn400WhenCreateNewCustomerWithoutName() throws Exception {
CustomerRequest customerRequest = new CustomerRequest(null, null);

mockMvc.perform(post("/api/customers")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isBadRequest())
.andExpect(header().string("Content-Type", is("application/problem+json")))
.andExpect(jsonPath("$.type", is("about:blank")))
.andExpect(jsonPath("$.title", is("Constraint Violation")))
.andExpect(jsonPath("$.status", is(400)))
.andExpect(jsonPath("$.detail", is("Invalid request content.")))
.andExpect(jsonPath("$.instance", is("/api/customers")))
.andExpect(jsonPath("$.violations", hasSize(1)))
.andExpect(jsonPath("$.violations[0].field", is("name")))
.andExpect(jsonPath("$.violations[0].message", is("Name cannot be empty")))
.andReturn();
}
}

@Test
void shouldDeleteCustomer() throws Exception {
Long customerId = 1L;
CustomerResponse customer = new CustomerResponse(customerId, "Some text", "Junit Address");
given(customerService.findCustomerById(customerId)).willReturn(Optional.of(customer));
doNothing().when(customerService).deleteCustomerById(customerId);

this.mockMvc
.perform(delete("/api/customers/{id}", customerId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(customer.name())))
.andExpect(jsonPath("$.address", is(customer.address())));
@Nested
@DisplayName("update methods")
class Update {
@Test
void shouldUpdateCustomer() throws Exception {
Long customerId = 1L;
CustomerResponse customer = new CustomerResponse(customerId, "Updated text", "Junit Address");
CustomerRequest customerRequest = new CustomerRequest("Updated text", "Junit Address");
given(customerService.updateCustomer(eq(customerId), any(CustomerRequest.class)))
.willReturn(customer);

mockMvc.perform(put("/api/customers/{id}", customerId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(customerId), Long.class))
.andExpect(jsonPath("$.name", is(customer.name())))
.andExpect(jsonPath("$.address", is(customer.address())));
}

@Test
void shouldReturn404WhenUpdatingNonExistingCustomer() throws Exception {
Long customerId = 1L;
CustomerRequest customerRequest = new CustomerRequest("Updated text", "Junit Address");
given(customerService.updateCustomer(eq(customerId), any(CustomerRequest.class)))
.willThrow(new CustomerNotFoundException(customerId));

mockMvc.perform(put("/api/customers/{id}", customerId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isNotFound())
.andExpect(header().string("Content-Type", is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("http://api.boot-data-envers.com/errors/not-found")))
.andExpect(jsonPath("$.title", is("Not Found")))
.andExpect(jsonPath("$.status", is(404)))
.andExpect(jsonPath("$.detail").value("Customer with Id '%d' not found".formatted(customerId)));
}
}

@Test
void shouldReturn404WhenDeletingNonExistingCustomer() throws Exception {
Long customerId = 1L;
given(customerService.findCustomerById(customerId)).willReturn(Optional.empty());

this.mockMvc
.perform(delete("/api/customers/{id}", customerId))
.andExpect(header().string("Content-Type", is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("http://api.boot-data-envers.com/errors/not-found")))
.andExpect(jsonPath("$.title", is("Not Found")))
.andExpect(jsonPath("$.status", is(404)))
.andExpect(jsonPath("$.detail").value("Customer with Id '%d' not found".formatted(customerId)));
@Nested
@DisplayName("delete methods")
class Delete {
@Test
void shouldDeleteCustomer() throws Exception {
Long customerId = 1L;
CustomerResponse customer = new CustomerResponse(customerId, "Some text", "Junit Address");
given(customerService.findCustomerById(customerId)).willReturn(Optional.of(customer));
doNothing().when(customerService).deleteCustomerById(customerId);

mockMvc.perform(delete("/api/customers/{id}", customerId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(customer.name())))
.andExpect(jsonPath("$.address", is(customer.address())));
}

@Test
void shouldReturn404WhenDeletingNonExistingCustomer() throws Exception {
Long customerId = 1L;
given(customerService.findCustomerById(customerId)).willReturn(Optional.empty());

mockMvc.perform(delete("/api/customers/{id}", customerId))
.andExpect(header().string("Content-Type", is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("http://api.boot-data-envers.com/errors/not-found")))
.andExpect(jsonPath("$.title", is("Not Found")))
.andExpect(jsonPath("$.status", is(404)))
.andExpect(jsonPath("$.detail").value("Customer with Id '%d' not found".formatted(customerId)));
}
}

List<CustomerResponse> getCustomerResponseList() {
Expand Down

0 comments on commit 1b7b2e2

Please sign in to comment.