Skip to content

Commit

Permalink
Merge pull request #1007 from puzzle/1005/test_quality_controller_pac…
Browse files Browse the repository at this point in the history
…kage

1005/test quality controller package
  • Loading branch information
clean-coder authored Sep 25, 2024
2 parents dbc7e20 + 9ffef7c commit 8d0473f
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,10 @@ class NonMetricOrOrdinalKeyResult extends KeyResult {
.with(SecurityMockMvcRequestPostProcessors.csrf()).content(JSON))
.andExpect(MockMvcResultMatchers.status().is4xxClientError());
}

@Test
void shouldDeleteCheckInById() throws Exception {
mvc.perform(delete(CHECK_IN_5_URL).with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ch.puzzle.okr.controller;

import ch.puzzle.okr.dto.ClientConfigDto;
import ch.puzzle.okr.service.clientconfig.ClientConfigService;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.BDDMockito;
import org.mockito.junit.jupiter.MockitoExtension;
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.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.Map;

import static ch.puzzle.okr.controller.OverviewControllerIT.JSON_PATH_ROOT;
import static org.mockito.ArgumentMatchers.anyString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@WithMockUser(value = "spring")
@ExtendWith(MockitoExtension.class)
@WebMvcTest(ClientConfigController.class)
public class ClientConfigControllerIT {

@Autowired
private MockMvc mvc;
@MockBean
private ClientConfigService configService;

@Test
void shouldGetClientConfig() throws Exception {
BDDMockito.given(configService.getConfigBasedOnActiveEnv(anyString())).willReturn(createClientConfigDto());

mvc.perform(get("/config").contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath(JSON_PATH_ROOT, Matchers.aMapWithSize(9)))
.andExpect(jsonPath("$.activeProfile", Matchers.is("Active_Profile")))
.andExpect(jsonPath("$.issuer", Matchers.is("Issuer")))
.andExpect(jsonPath("$.clientId", Matchers.is("Client_Id")))
.andExpect(jsonPath("$.favicon", Matchers.is("Favicon")))
.andExpect(jsonPath("$.logo", Matchers.is("Logo")))
.andExpect(jsonPath("$.triangles", Matchers.is("Triangles")))
.andExpect(jsonPath("$.backgroundLogo", Matchers.is("Background_Logo")))
.andExpect(jsonPath("$.title", Matchers.is("Title")))
.andExpect(jsonPath("$.customStyles.font-family", Matchers.is("verdana")))
.andExpect(jsonPath("$.customStyles.font-size", Matchers.is("20px")));
}

private ClientConfigDto createClientConfigDto() {
Map<String, String> customStyles = Map.of("font-family", "verdana", "font-size", "20px");
return new ClientConfigDto("Active_Profile", "Issuer", "Client_Id", "Favicon", "Logo", "Triangles",
"Background_Logo", "Title", customStyles);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TeamControllerIT {
private static final String BASE_URL = "/api/v2/teams";
private static final String URL_TEAM_1 = "/api/v2/teams/1";
public static final String PUZZLE = "Puzzle";
public static final String SUB_URL_USER_5 = "/user/5";
static Team teamPuzzle = Team.Builder.builder().withId(5L).withName(PUZZLE).build();
static Team teamOKR = Team.Builder.builder().withId(7L).withName("OKR").build();
static List<Team> teamList = Arrays.asList(teamPuzzle, teamOKR);
Expand Down Expand Up @@ -182,4 +183,18 @@ void addUsersToTeam_shouldReturnOk() throws Exception {
mvc.perform(put(URL_TEAM_1 + "/addusers").contentType(MediaType.APPLICATION_JSON).content(ADD_USERS)
.with(SecurityMockMvcRequestPostProcessors.csrf())).andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
void removeUserFromTeam_shouldReturnOk() throws Exception {
mvc.perform(put(URL_TEAM_1 + SUB_URL_USER_5 + "/removeuser").contentType(MediaType.APPLICATION_JSON)
.content(ADD_USERS).with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
void updateOrAddTeamMembership_shouldReturnOk() throws Exception {
mvc.perform(put(URL_TEAM_1 + SUB_URL_USER_5 + "/updateaddteammembership/true")
.contentType(MediaType.APPLICATION_JSON).content(ADD_USERS)
.with(SecurityMockMvcRequestPostProcessors.csrf())).andExpect(MockMvcResultMatchers.status().isOk());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.puzzle.okr.dto.UserDto;
import ch.puzzle.okr.mapper.UserMapper;
import ch.puzzle.okr.models.User;
import ch.puzzle.okr.models.authorization.AuthorizationUser;
import ch.puzzle.okr.service.authorization.AuthorizationService;
import ch.puzzle.okr.service.authorization.UserAuthorizationService;
import org.hamcrest.Matchers;
Expand All @@ -17,6 +18,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

Expand All @@ -25,7 +27,9 @@
import java.util.Collections;
import java.util.List;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static ch.puzzle.okr.controller.ActionControllerIT.SUCCESSFUL_UPDATE_BODY;
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@WithMockUser(value = "spring")
Expand Down Expand Up @@ -81,4 +85,82 @@ void shouldGetAllUsersIfNoUserExists() throws Exception {
mvc.perform(get("/api/v1/users").contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk()).andExpect(jsonPath("$", Matchers.hasSize(0)));
}

@Test
void shouldReturnCurrentUser() throws Exception {
BDDMockito.given(authorizationService.updateOrAddAuthorizationUser())
.willReturn(new AuthorizationUser(userAlice));
BDDMockito.given(userMapper.toDto(userAlice)).willReturn(userAliceDto);

mvc.perform(get("/api/v1/users/current").contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk()) //
.andExpect(jsonPath("$", Matchers.aMapWithSize(7))) //
.andExpect(jsonPath("$.id", Is.is(2))) //
.andExpect(jsonPath("$.version", Is.is(3))) //
.andExpect(jsonPath("$.firstname", Is.is(FIRSTNAME_1))) //
.andExpect(jsonPath("$.lastname", Is.is(LASTNAME_1))) //
.andExpect(jsonPath("$.email", Is.is(EMAIL_1))) //
.andExpect(jsonPath("$.userTeamList", Matchers.empty())) //
.andExpect(jsonPath("$.isOkrChampion", Is.is(false)));
}

@Test
void shouldReturnUserById() throws Exception {
BDDMockito.given(userAuthorizationService.getById(2)).willReturn(userAlice);
BDDMockito.given(userMapper.toDto(userAlice)).willReturn(userAliceDto);

mvc.perform(get("/api/v1/users/2").contentType(MediaType.APPLICATION_JSON)) //
.andExpect(MockMvcResultMatchers.status().isOk()) //
.andExpect(jsonPath("$", Matchers.aMapWithSize(7))) //
.andExpect(jsonPath("$.id", Is.is(2))) //
.andExpect(jsonPath("$.version", Is.is(3))) //
.andExpect(jsonPath("$.firstname", Is.is(FIRSTNAME_1))) //
.andExpect(jsonPath("$.lastname", Is.is(LASTNAME_1))) //
.andExpect(jsonPath("$.email", Is.is(EMAIL_1))) //
.andExpect(jsonPath("$.userTeamList", Matchers.empty())) //
.andExpect(jsonPath("$.isOkrChampion", Is.is(false)));
}

@Test
void shouldSetOkrChampion() throws Exception {
BDDMockito.given(userAuthorizationService.setIsOkrChampion(2, true)).willReturn(userAlice);
BDDMockito.given(userMapper.toDto(userAlice)).willReturn(userAliceDto);

mvc.perform(put("/api/v1/users/2/isokrchampion/true") //
.content(SUCCESSFUL_UPDATE_BODY) //
.contentType(MediaType.APPLICATION_JSON) //
.with(SecurityMockMvcRequestPostProcessors.csrf()) //
) //
.andExpect(MockMvcResultMatchers.status().isOk()) //
.andExpect(jsonPath("$", Matchers.aMapWithSize(7))) //
.andExpect(jsonPath("$.id", Is.is(2))) //
.andExpect(jsonPath("$.version", Is.is(3))) //
.andExpect(jsonPath("$.firstname", Is.is(FIRSTNAME_1))) //
.andExpect(jsonPath("$.lastname", Is.is(LASTNAME_1))) //
.andExpect(jsonPath("$.email", Is.is(EMAIL_1))) //
.andExpect(jsonPath("$.userTeamList", Matchers.empty())) //
.andExpect(jsonPath("$.isOkrChampion", Is.is(false)));
}

@Test
void shouldCreateUsers() throws Exception {
BDDMockito.given(userAuthorizationService.createUsers(any())).willReturn(List.of(userAlice));
BDDMockito.given(userMapper.toDtos(List.of(userAlice))).willReturn(List.of(userAliceDto));

mvc.perform(post("/api/v1/users/createall") //
.content(SUCCESSFUL_UPDATE_BODY) //
.contentType(MediaType.APPLICATION_JSON) //
.with(SecurityMockMvcRequestPostProcessors.csrf()) //
) //
.andExpect(MockMvcResultMatchers.status().isOk()) //
.andExpect(jsonPath("$", Matchers.hasSize(1))) //
.andExpect(jsonPath("$[0].id", Is.is(2))) //
.andExpect(jsonPath("$[0].version", Is.is(3))) //
.andExpect(jsonPath("$[0].firstname", Is.is(FIRSTNAME_1))) //
.andExpect(jsonPath("$[0].lastname", Is.is(LASTNAME_1))) //
.andExpect(jsonPath("$[0].email", Is.is(EMAIL_1))) //
.andExpect(jsonPath("$[0].userTeamList", Matchers.empty())) //
.andExpect(jsonPath("$[0].isOkrChampion", Is.is(false)));
}

}

0 comments on commit 8d0473f

Please sign in to comment.