From 874811302ed2b5768e56e70df12728a856829cb4 Mon Sep 17 00:00:00 2001 From: "o.mohkamkar" Date: Sat, 13 Apr 2024 15:07:07 +0330 Subject: [PATCH 1/3] Add test example --- .../omidmk/iamapi/IAMApiApplicationTests.java | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java b/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java index 613ee42..85b8215 100644 --- a/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java +++ b/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java @@ -1,13 +1,49 @@ package com.omidmk.iamapi; +import com.omidmk.iamapi.service.CustomerService; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.test.web.servlet.MockMvc; -@SpringBootTest +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureMockMvc class IAMApiApplicationTests { + @MockBean + private CustomerService customerService; + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private MockMvc mockMvc; + @Test void contextLoads() { + assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/", String.class)).contains("Hello, World"); +// assertThat(customerService).isNotNull(); +// Assertions.assertNull(customerService.findUserByEmail("omid@gmail.com").orElse(null)); + } + + @Test + void shouldReturnDefaultMessage() throws Exception { + this.mockMvc.perform(get("/v1/plans/beginner")).andDo(print()).andExpect(status().is4xxClientError()) + .andExpect(content().string(containsString(""))); } } From a72eab50ff2c39f2b021b2561e79b44e65f181dd Mon Sep 17 00:00:00 2001 From: Omid Mohkamkar Date: Mon, 15 Apr 2024 01:41:31 +0330 Subject: [PATCH 2/3] Update test --- .../test/java/com/omidmk/iamapi/IAMApiApplicationTests.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java b/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java index 85b8215..8589341 100644 --- a/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java +++ b/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java @@ -35,8 +35,7 @@ class IAMApiApplicationTests { @Test void contextLoads() { - assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/", String.class)).contains("Hello, World"); -// assertThat(customerService).isNotNull(); + assertThat(customerService).isNotNull(); // Assertions.assertNull(customerService.findUserByEmail("omid@gmail.com").orElse(null)); } From 563cbb7ccb548c47f7abafc4f7372f0b2e666e78 Mon Sep 17 00:00:00 2001 From: Omid Mohkamkar Date: Wed, 24 Apr 2024 09:46:31 +0330 Subject: [PATCH 3/3] Add unit test --- .../omidmk/iamapi/IAMApiApplicationTests.java | 101 ++++++++++++++++-- 1 file changed, 90 insertions(+), 11 deletions(-) diff --git a/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java b/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java index 8589341..f6bc302 100644 --- a/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java +++ b/iam-api/src/test/java/com/omidmk/iamapi/IAMApiApplicationTests.java @@ -1,29 +1,54 @@ package com.omidmk.iamapi; -import com.omidmk.iamapi.service.CustomerService; -import org.junit.jupiter.api.Test; +import com.omidmk.iamapi.exception.DeploymentNotFoundException; +import com.omidmk.iamapi.exception.RealmAlreadyExistException; +import com.omidmk.iamapi.exception.TicketNotFoundException; +import com.omidmk.iamapi.exception.UserNotFoundException; +import com.omidmk.iamapi.model.deployment.DeploymentModel; +import com.omidmk.iamapi.model.deployment.PlanDV; +import com.omidmk.iamapi.model.ticket.DialogModel; +import com.omidmk.iamapi.model.ticket.TicketModel; +import com.omidmk.iamapi.model.user.UserModel; +import com.omidmk.iamapi.service.*; +import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.data.domain.Pageable; import org.springframework.test.web.servlet.MockMvc; -import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.Matchers.containsString; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) class IAMApiApplicationTests { - @MockBean +// @MockBean + @Autowired private CustomerService customerService; + @Autowired + private DeploymentService deploymentService; + + @Autowired + private KeycloakService keycloakService; + + @Autowired + private MailService mailService; + + @Autowired + private TicketService ticketService; + @LocalServerPort private int port; @@ -34,15 +59,69 @@ class IAMApiApplicationTests { private MockMvc mockMvc; @Test + @Order(0) void contextLoads() { assertThat(customerService).isNotNull(); -// Assertions.assertNull(customerService.findUserByEmail("omid@gmail.com").orElse(null)); + assertThat(deploymentService).isNotNull(); + assertThat(keycloakService).isNotNull(); + assertThat(mailService).isNotNull(); + assertThat(ticketService).isNotNull(); } @Test - void shouldReturnDefaultMessage() throws Exception { - this.mockMvc.perform(get("/v1/plans/beginner")).andDo(print()).andExpect(status().is4xxClientError()) - .andExpect(content().string(containsString(""))); + @Order(1) + void customerServiceTest() { + assertThatExceptionOfType(UserNotFoundException.class) + .isThrownBy(() -> customerService.findUserById(UUID.randomUUID())); + UserModel user = customerService.saveUser(new UserModel("test@test.com", false, "firstnameTest", "lastnameTest", 100f)); + assertThat(user).isNotNull(); + assertThat(user.getId()).isNotNull(); + assertThat(user.getEmail()).isEqualTo("test@test.com"); + assertThat(user.getFirstName()).isEqualTo("firstnameTest"); + Optional foundUser = customerService.findUserByEmail("test@test.com"); + assertThat(foundUser).isPresent(); + assertThat(foundUser.get().getId()).isEqualTo(user.getId()); + assertThat(customerService.findAll(Pageable.unpaged())).hasSize(1); + } + + @Test + @Order(2) + void deploymentServiceTest() throws RealmAlreadyExistException { + assertThatExceptionOfType(DeploymentNotFoundException.class) + .isThrownBy(() -> deploymentService.findDeploymentById(UUID.randomUUID())); + assertThat(deploymentService.findAllDeployments(Pageable.unpaged())).isEmpty(); + assertThat(deploymentService.isRealmAvailable("test")).isTrue(); + assertThat(deploymentService.isRealmAvailable("master")).isFalse(); + UserModel user = customerService.findAllCustomers(Pageable.unpaged()).toList().get(0); + DeploymentModel deployment = deploymentService.createDeployment(user, "test", PlanDV.BEGINNER); + assertThat(deployment).isNotNull(); + assertThat(deployment.getId()).isNotNull(); + assertThat(deployment.getRealmName()).isEqualTo("test"); + assertThat(deployment.getState().toString()).isEqualTo("DEPLOYING"); + } + + @Test + @Order(3) + void ticketServiceTest() { + assertThatExceptionOfType(TicketNotFoundException.class) + .isThrownBy(() -> ticketService.findTicketById(UUID.randomUUID())); + assertThat(ticketService.findAllTickets(Pageable.unpaged())).isEmpty(); + assertThat(ticketService.findClosedTickets(Pageable.unpaged())).isEmpty(); + UserModel user = customerService.findAllCustomers(Pageable.unpaged()).toList().get(0); + TicketModel ticket = new TicketModel(); + ticket.setCustomer(user); + ticket.setDialogs(List.of(new DialogModel(user, "Hello, test."))); + ticket.setState(TicketModel.State.WAITING_FOR_ADMIN_RESPONSE); + ticket = ticketService.saveTicket(ticket); + assertThat(ticket).isNotNull(); + assertThat(ticket.getId()).isNotNull(); + assertThat(ticket.getDialogs()).hasSize(1); + assertThat(ticket.getDialogs().get(0).getText()).isEqualTo("Hello, test."); + assertThat(ticket.getState().toString()).isEqualTo("WAITING_FOR_ADMIN_RESPONSE"); } + @Test + void shouldReturnDefaultMessage() throws Exception { + this.mockMvc.perform(get("/v1/plans/beginner")).andDo(print()).andExpect(status().is4xxClientError()); + } }