From f9dd6e38c38c901c518f35eefdb25f866b563bf3 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Mon, 5 Oct 2020 17:56:35 +0200 Subject: [PATCH] 3/4 Commisions test --- .../plannaplan/services/CommisionService.java | 3 +- .../controllers/CommisionControllerTest.java | 65 +++++++++++++++---- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java index e650a31..96ebd69 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java @@ -25,8 +25,7 @@ public class CommisionService { } public List getUsersCommisions(User user) { - Long id = user.getId(); - return this.repo.getUsers(id); + return this.repo.getUsers(user.getId()); } public Optional getNewestCommision(User user) { diff --git a/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java index 19561ea..fded47a 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/CommisionControllerTest.java @@ -1,36 +1,79 @@ package com.plannaplan.controllers; -import static org.junit.jupiter.api.Assertions.assertTrue; - 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.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.plannaplan.entities.User; +import com.plannaplan.services.UserService; +import com.plannaplan.types.UserRoles; + +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration public class CommisionControllerTest { + @Autowired + private WebApplicationContext webApplicationContext; + + @Autowired + private UserService service; + + private static User user; + + private static final String ADD_COMMISION_ENDPOINT = "/api/v1/commisions/add"; + private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/getAllCommisions"; + private static final String TEST_COMMISIONS_EMAIL = "commisions@notexisting.domain"; + @Test - public void shouldReturn4xxAddingCommision() { - assertTrue(false); + public void shouldReturn4xxAddingCommision() throws Exception { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(ADD_COMMISION_ENDPOINT)).andExpect(status().is4xxClientError()); + } @Test - public void shouldReturnOkAddingCommision() { - assertTrue(false); + public void shouldReturnOkAddingCommision() throws Exception { + this.checkUser(); + final String token = this.service.login(TEST_COMMISIONS_EMAIL); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)) + .andExpect(status().isOk()); } @Test - public void shouldReturn4xxGettingAllCommisions() { - assertTrue(false); + public void shouldReturn4xxGettingAllCommisions() throws Exception { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(GET_COMMISIONS_ENDPOINT)).andExpect(status().is4xxClientError()); } @Test - public void shouldReturnOkGettingAllCommisions() { - assertTrue(false); + public void shouldReturnOkGettingAllCommisions() throws Exception { + this.checkUser(); + final String token = this.service.login(TEST_COMMISIONS_EMAIL); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token)) + .andExpect(status().isOk()); + } + + private void checkUser() { + if (CommisionControllerTest.user == null) { + CommisionControllerTest.user = new User(null, null, TEST_COMMISIONS_EMAIL, UserRoles.TEST_USER); + this.service.save(user); + } } }