Skip to content

Commit

Permalink
RI-193: teste les fonctions du CompanyController (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaFlu-SDV committed Nov 28, 2021
1 parent 965d41a commit 3ef6b61
Showing 1 changed file with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
import java.util.Arrays;
import java.util.List;

import static com.dynonuggets.refonteimplicaction.utils.ApiUrls.COMPANIES_BASE_URI;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
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.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(controllers = CompanyController.class)
class CompanyControllerTest extends ControllerIntegrationTestBase {
Expand Down Expand Up @@ -106,4 +108,65 @@ void getAllWithoutJwtShouldBeForbidden() throws Exception {

verify(companyService, never()).getAll(any());
}

@Test
@WithMockUser
void should_create_company_when_authenticated() throws Exception {
// given
CompanyDto companyDto = CompanyDto.builder()
.id(123L)
.description("Description")
.name("Implicaction")
.url("url")
.build();

String json = gson.toJson(companyDto);

CompanyDto expectedDto = CompanyDto.builder()
.id(123L)
.description("Description")
.name("Implicaction")
.url("url")
.build();


given(companyService.saveOrUpdateCompany(any())).willReturn(expectedDto);

// when
final ResultActions resultActions = mvc.perform(
post(COMPANIES_BASE_URI).content(json).accept(APPLICATION_JSON).contentType(APPLICATION_JSON)
);

// then
resultActions.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType(APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.id", is(expectedDto.getId().intValue())))
.andExpect(jsonPath("$.description", is(expectedDto.getDescription())))
.andExpect(jsonPath("$.name", is(expectedDto.getName())))
.andExpect(jsonPath("$.url", is(expectedDto.getUrl())));

verify(companyService, times(1)).saveOrUpdateCompany(any());
}

@Test
void should_not_create_company_and_response_forbidden_when_not_authenticated() throws Exception {
// given
CompanyDto companyDto = CompanyDto.builder()
.description("Description")
.name("Implicaction")
.url("url")
.build();

String json = gson.toJson(companyDto);

// when
final ResultActions resultActions = mvc.perform(
post(COMPANIES_BASE_URI).content(json).accept(APPLICATION_JSON).contentType(APPLICATION_JSON)
);

// then
resultActions.andDo(print()).andExpect(status().isForbidden());
verify(companyService, never()).saveOrUpdateCompany(any());
}
}

0 comments on commit 3ef6b61

Please sign in to comment.