Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RI-193: teste les fonctions du CompanyController #204

Merged
merged 2 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -37,7 +38,7 @@ public ResponseEntity<Page<CompanyDto>> getAllByCriteria(
@RequestMapping(method = {RequestMethod.POST, RequestMethod.PUT})
public ResponseEntity<CompanyDto> createOrUpdate(@RequestBody CompanyDto companyDto) throws ImplicactionException {
CompanyDto companyCreated = companyService.saveOrUpdateCompany(companyDto);
return ResponseEntity.ok(companyCreated);
return ResponseEntity.status(HttpStatus.CREATED).body(companyCreated);
PaFlu-SDV marked this conversation as resolved.
Show resolved Hide resolved
}
}

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().isCreated())
.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());
}
}