|
| 1 | +package com.codedifferently.studycrm.organizations.web.controllers; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.Mockito.reset; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 8 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 9 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 11 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 12 | + |
| 13 | +import com.codedifferently.studycrm.organizations.api.web.UserDetails; |
| 14 | +import com.codedifferently.studycrm.organizations.domain.Organization; |
| 15 | +import com.codedifferently.studycrm.organizations.domain.OrganizationService; |
| 16 | +import com.codedifferently.studycrm.organizations.sagas.OrganizationSagaService; |
| 17 | +import com.codedifferently.studycrm.organizations.web.TestConfiguration; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.UUID; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.mockito.stubbing.Answer; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.boot.test.context.SpringBootTest; |
| 26 | +import org.springframework.http.MediaType; |
| 27 | +import org.springframework.security.acls.AclPermissionEvaluator; |
| 28 | +import org.springframework.security.test.context.support.WithMockUser; |
| 29 | +import org.springframework.test.context.ContextConfiguration; |
| 30 | +import org.springframework.test.web.servlet.MockMvc; |
| 31 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 32 | +import org.springframework.web.context.WebApplicationContext; |
| 33 | + |
| 34 | +@SpringBootTest |
| 35 | +@ContextConfiguration(classes = TestConfiguration.class) |
| 36 | +public class OrganizationsControllerTest { |
| 37 | + |
| 38 | + @Autowired private AclPermissionEvaluator aclPermissionEvaluator; |
| 39 | + |
| 40 | + @Autowired private OrganizationService organizationService; |
| 41 | + |
| 42 | + @Autowired private OrganizationSagaService organizationSagaService; |
| 43 | + |
| 44 | + private MockMvc mockMvc; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void setUp(WebApplicationContext wac) { |
| 48 | + mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); |
| 49 | + reset(aclPermissionEvaluator, organizationService, organizationSagaService); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + @WithMockUser |
| 54 | + void testGetOrganization_WhenNotAuthorized_ReturnsNotAllowed() throws Exception { |
| 55 | + // Arrange |
| 56 | + when(aclPermissionEvaluator.hasPermission(any(), any(), any(), any())).thenReturn(false); |
| 57 | + |
| 58 | + // Act |
| 59 | + assertThrows( |
| 60 | + Exception.class, |
| 61 | + () -> mockMvc.perform(get("/organizations/{organizationId}", UUID.randomUUID()))); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @WithMockUser |
| 66 | + void testGetOrganization_WhenNotExists_ReturnsNotFound() throws Exception { |
| 67 | + // Arrange |
| 68 | + var orgId = UUID.randomUUID(); |
| 69 | + when(aclPermissionEvaluator.hasPermission(any(), any(), any(), any())).thenReturn(true); |
| 70 | + when(organizationService.findOrganizationById(orgId)).thenReturn(Optional.empty()); |
| 71 | + |
| 72 | + // Act |
| 73 | + mockMvc.perform(get("/organizations/{organizationId}", orgId)).andExpect(status().isNotFound()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + @WithMockUser |
| 78 | + void testGetOrganization_WhenExists_ReturnsOrganization() throws Exception { |
| 79 | + // Arrange |
| 80 | + var orgId = UUID.randomUUID(); |
| 81 | + var expected = Organization.builder().id(orgId).name("Test Organization").build(); |
| 82 | + when(aclPermissionEvaluator.hasPermission(any(), any(), any(), any())).thenReturn(true); |
| 83 | + when(organizationService.findOrganizationById(orgId)).thenReturn(Optional.of(expected)); |
| 84 | + |
| 85 | + // Act |
| 86 | + mockMvc |
| 87 | + .perform(get("/organizations/{organizationId}", orgId)) |
| 88 | + .andExpect(status().isOk()) |
| 89 | + .andExpect(jsonPath("$.organizationId").value(orgId.toString())) |
| 90 | + .andExpect(jsonPath("$.name").value(expected.getName())); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + @WithMockUser |
| 95 | + void testCreateOrganization_WhenNotValid_ReportsErrors() throws Exception { |
| 96 | + // Act |
| 97 | + mockMvc |
| 98 | + .perform(post("/organizations").contentType(MediaType.APPLICATION_JSON).content("{}")) |
| 99 | + .andExpect(status().isBadRequest()) |
| 100 | + .andExpect( |
| 101 | + content() |
| 102 | + .json( |
| 103 | + "{\"errors\":[\"Organization name is required\",\"User details are required\"]}")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + @WithMockUser |
| 108 | + void testCreateOrganization_WhenValid_ReturnsResponse() throws Exception { |
| 109 | + // Arrange |
| 110 | + var orgId = UUID.randomUUID(); |
| 111 | + var expectedOrg = Organization.builder().name("Test Organization").build(); |
| 112 | + var expectedUser = |
| 113 | + UserDetails.builder() |
| 114 | + .username("testUser") |
| 115 | + |
| 116 | + .firstName("John") |
| 117 | + .lastName("Doe") |
| 118 | + .build(); |
| 119 | + when(organizationSagaService.createOrganization(expectedOrg, expectedUser)) |
| 120 | + .then( |
| 121 | + (Answer<Organization>) |
| 122 | + invocation -> { |
| 123 | + expectedOrg.setId(orgId); |
| 124 | + return expectedOrg; |
| 125 | + }); |
| 126 | + |
| 127 | + // Act |
| 128 | + mockMvc |
| 129 | + .perform( |
| 130 | + post("/organizations") |
| 131 | + .contentType(MediaType.APPLICATION_JSON) |
| 132 | + .content( |
| 133 | + "{\"organizationName\":\"Test Organization\",\"userDetails\":{\"username\":\"testUser\"},\"userDetails\":{\"username\":\"testUser\",\"email\":\"[email protected]\",\"firstName\":\"John\",\"lastName\":\"Doe\"}}")) |
| 134 | + .andExpect(status().isOk()) |
| 135 | + .andExpect(jsonPath("$.organizationId").value(orgId.toString())); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + @WithMockUser |
| 140 | + void testGetAll_ReturnsOrganizations() throws Exception { |
| 141 | + // Arrange |
| 142 | + var expectedOrgs = new ArrayList<Organization>(); |
| 143 | + expectedOrgs.add( |
| 144 | + Organization.builder().id(UUID.randomUUID()).name("Test Organization 1").build()); |
| 145 | + expectedOrgs.add( |
| 146 | + Organization.builder().id(UUID.randomUUID()).name("Test Organization 2").build()); |
| 147 | + |
| 148 | + when(aclPermissionEvaluator.hasPermission(any(), any(), any())).thenReturn(true); |
| 149 | + when(organizationService.findAllOrganizations()).thenReturn(expectedOrgs); |
| 150 | + |
| 151 | + // Act |
| 152 | + mockMvc |
| 153 | + .perform(get("/organizations")) |
| 154 | + .andExpect(status().isOk()) |
| 155 | + .andExpect( |
| 156 | + jsonPath("$.organizations[0].organizationId") |
| 157 | + .value(expectedOrgs.get(0).getId().toString())) |
| 158 | + .andExpect( |
| 159 | + jsonPath("$.organizations[1].organizationId") |
| 160 | + .value(expectedOrgs.get(1).getId().toString())); |
| 161 | + } |
| 162 | +} |
0 commit comments