generated from alkemyTech/base-ong-server-java
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OT276-92] Tests: Endpoints /members (#80)
* Se realizaron los test unitarios y de integracion. Modificaciones en MemberResponse y configuraciones faltantes en SecurityConfig * Correccion en el tipeo de los imports * cambios solicitados * securityconfig Co-authored-by: Jonathan Manera <[email protected]>
- Loading branch information
1 parent
8911ee6
commit 04614b6
Showing
4 changed files
with
307 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/test/java/com/alkemy/ong/ports/input/rs/controller/MemberControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.alkemy.ong.ports.input.rs.controller; | ||
|
||
import com.alkemy.ong.H2Config; | ||
import com.alkemy.ong.config.util.JsonUtils; | ||
import com.alkemy.ong.core.model.Member; | ||
import com.alkemy.ong.core.usecase.MemberService; | ||
import com.alkemy.ong.ports.input.rs.api.ApiConstants; | ||
import com.alkemy.ong.ports.input.rs.request.MemberRequest; | ||
import com.alkemy.ong.ports.input.rs.response.MemberResponseList; | ||
import org.junit.jupiter.api.MethodOrderer; | ||
import org.junit.jupiter.api.Order; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestMethodOrder; | ||
import org.mockito.Mock; | ||
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.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithUserDetails; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@ActiveProfiles("test") | ||
@SpringJUnitConfig(classes = H2Config.class) | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
class MemberControllerIT { | ||
|
||
@Autowired | ||
MockMvc mockMvc; | ||
|
||
@Mock | ||
MemberService memberService; | ||
|
||
|
||
@Test | ||
@Order(1) | ||
@WithUserDetails("[email protected]") | ||
void createMember_shouldReturn201() throws Exception { | ||
|
||
MemberRequest request = MemberRequest.builder() | ||
.name("foo") | ||
.facebookUrl("foo.faceboock") | ||
.instagramUrl("foo.instagram") | ||
.linkedinUrl("foo.linkedin") | ||
.description("foo.description") | ||
.image("foo.image") | ||
.build(); | ||
given(memberService.createEntity(any(Member.class))).willReturn(99L); | ||
|
||
final String actualLocation = mockMvc.perform(post(ApiConstants.MEMBERS_URI) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(JsonUtils.objectToJson(request))) | ||
.andExpect(status().isCreated()) | ||
.andDo(print()) | ||
.andReturn() | ||
.getResponse() | ||
.getHeader(HttpHeaders.LOCATION); | ||
|
||
assertThat(actualLocation).isEqualTo("http://localhost/v1/members/1"); | ||
} | ||
@Test | ||
@Order(2) | ||
@WithUserDetails("[email protected]") | ||
void getMembers_shouldReturn200() throws Exception{ | ||
String content = mockMvc.perform(get(ApiConstants.MEMBERS_URI)) | ||
.andExpect(status().isOk()) | ||
.andDo(print()) | ||
.andReturn() | ||
.getResponse() | ||
.getContentAsString(); | ||
|
||
assertThat(content).isNotBlank(); | ||
|
||
MemberResponseList response = JsonUtils.jsonToObject(content, MemberResponseList.class); | ||
|
||
assertThat(response.getTotalElements()).isEqualTo(1); | ||
assertThat(response.getTotalPages()).isEqualTo(1); | ||
assertThat(response.getNextUri()).isEqualTo("http://localhost/v1/members?page=1"); | ||
assertThat(response.getPreviousUri()).isEqualTo("http://localhost/v1/members?page=0"); | ||
assertThat(response.getContent()).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
@Order(3) | ||
@WithUserDetails("[email protected]") | ||
void upDateMember_shouldReturn204() throws Exception { | ||
MemberRequest request = MemberRequest.builder() | ||
.name("faa") | ||
.facebookUrl("faa.faceboock") | ||
.instagramUrl("faa.instagram") | ||
.linkedinUrl("faa.linkedin") | ||
.description("faa.description") | ||
.image("faa.image") | ||
.build(); | ||
|
||
mockMvc.perform(put(ApiConstants.MEMBERS_URI + "/1") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(JsonUtils.objectToJson(request))) | ||
.andExpect(status().isNoContent()) | ||
.andDo(print()); | ||
} | ||
|
||
@Test | ||
@Order(4) | ||
@WithUserDetails("[email protected]") | ||
void deleteMember_shouldReturn204() throws Exception { | ||
mockMvc.perform(delete(ApiConstants.MEMBERS_URI + "/1")) | ||
.andExpect(status().isNoContent()) | ||
.andDo(print()); | ||
} | ||
|
||
|
||
@Test | ||
@Order(5) | ||
@WithUserDetails("[email protected]") | ||
void deleteMember_shouldReturn403() throws Exception { | ||
mockMvc.perform(delete(ApiConstants.MEMBERS_URI + "/1")) | ||
.andExpect(status().isForbidden()) | ||
.andDo(print()); | ||
} | ||
|
||
|
||
|
||
} |
145 changes: 145 additions & 0 deletions
145
src/test/java/com/alkemy/ong/ports/input/rs/controller/MemberControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package com.alkemy.ong.ports.input.rs.controller; | ||
|
||
import com.alkemy.ong.config.exception.handler.GlobalExceptionHandler; | ||
import com.alkemy.ong.config.util.JsonUtils; | ||
import com.alkemy.ong.core.model.Member; | ||
import com.alkemy.ong.core.model.MemberList; | ||
import com.alkemy.ong.core.usecase.MemberService; | ||
import com.alkemy.ong.ports.input.rs.api.ApiConstants; | ||
import com.alkemy.ong.ports.input.rs.mapper.MemberControllerMapper; | ||
import com.alkemy.ong.ports.input.rs.request.MemberRequest; | ||
import com.alkemy.ong.ports.input.rs.response.MemberResponseList; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mapstruct.factory.Mappers; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Spy; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import java.util.List; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.willDoNothing; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
|
||
@ExtendWith(MockitoExtension.class) | ||
class MemberControllerTest { | ||
|
||
|
||
private MockMvc mockMvc; | ||
|
||
@InjectMocks | ||
MemberController memberController; | ||
|
||
@Mock | ||
MemberService memberService; | ||
|
||
@Spy | ||
MemberControllerMapper mapper = Mappers.getMapper(MemberControllerMapper.class); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mockMvc = MockMvcBuilders.standaloneSetup(memberController) | ||
.setControllerAdvice(GlobalExceptionHandler.class) | ||
.build(); | ||
} | ||
|
||
@Test | ||
void createMember_shouldReturn201() throws Exception { | ||
|
||
MemberRequest request = MemberRequest.builder() | ||
.name("foo") | ||
.facebookUrl("foo.faceboock") | ||
.instagramUrl("foo.instagram") | ||
.linkedinUrl("foo.linkedin") | ||
.description("foo.description") | ||
.image("foo.image") | ||
.build(); | ||
|
||
given(memberService.createEntity(any(Member.class))).willReturn(99L); | ||
|
||
final String actualLocation = mockMvc.perform(post(ApiConstants.MEMBERS_URI) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(JsonUtils.objectToJson(request))) | ||
.andExpect(status().isCreated()) | ||
.andDo(print()) | ||
.andReturn() | ||
.getResponse() | ||
.getHeader(HttpHeaders.LOCATION); | ||
|
||
assertThat(actualLocation).isEqualTo("http://localhost/v1/members/99"); | ||
|
||
} | ||
|
||
@Test | ||
void getMembers_shouldReturn200() throws Exception { | ||
Member member = new Member(); | ||
member.setId(99L); | ||
member.setName("foo"); | ||
member.setImage("foo.image"); | ||
|
||
MemberList list = new MemberList(List.of(member), Pageable.ofSize(1), 1); | ||
|
||
given(memberService.getList(any(PageRequest.class))).willReturn(list); | ||
|
||
String content = mockMvc.perform(get(ApiConstants.MEMBERS_URI + "?page=0&size=1")) | ||
.andExpect(status().isOk()) | ||
.andDo(print()) | ||
.andReturn() | ||
.getResponse() | ||
.getContentAsString(); | ||
|
||
assertThat(content).isNotBlank(); | ||
|
||
MemberResponseList response = JsonUtils.jsonToObject(content, MemberResponseList.class); | ||
|
||
assertThat(response.getTotalElements()).isEqualTo(1); | ||
assertThat(response.getTotalPages()).isEqualTo(1); | ||
assertThat(response.getNextUri()).isEqualTo("http://localhost/v1/members?size=1&page=1"); | ||
assertThat(response.getPreviousUri()).isEqualTo("http://localhost/v1/members?size=1&page=0"); | ||
assertThat(response.getContent()).isNotEmpty(); | ||
} | ||
|
||
|
||
|
||
@Test | ||
void upDateMember_shouldReturn204() throws Exception{ | ||
|
||
MemberRequest request = MemberRequest.builder() | ||
.name("faa") | ||
.image("faa.image") | ||
.build(); | ||
|
||
willDoNothing().given(memberService).updateEntityIfExists(eq(99L), any(Member.class)); | ||
|
||
mockMvc.perform(put(ApiConstants.MEMBERS_URI + "/99") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(JsonUtils.objectToJson(request))) | ||
.andExpect(status().isNoContent()) | ||
.andDo(print()); | ||
} | ||
|
||
@Test | ||
void deleteMember_shouldReturn204() throws Exception{ | ||
mockMvc.perform(delete(ApiConstants.MEMBERS_URI + "/1")) | ||
.andExpect(status().isNoContent()) | ||
.andDo(print()); | ||
} | ||
|
||
|
||
} |