Skip to content

Commit

Permalink
bump(spring-boot): fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrouillet committed Aug 1, 2024
1 parent 5944e5f commit 4a2f778
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
<version>3.0.6</version>
<version>3.3.2</version>
</dependency>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
Expand Down Expand Up @@ -196,7 +197,7 @@ public void getAllAccreditationRequestToAnswerAdmin() {
accreditationRequestList.add(accreditationRequest2);
PageRequest pageRequest = PageRequest.of(0, 10);

when(accreditationRequestRepository.findAll(any(Predicate.class), eq(pageRequest))).thenReturn(new PageImpl<>(accreditationRequestList));
when(accreditationRequestRepository.findAll(any(Predicate.class), eq(pageRequest))).thenReturn(new PageImpl<>(accreditationRequestList, Pageable.ofSize(accreditationRequestList.size()), accreditationRequestList.size()));

Page<AccreditationRequest> result = accreditationRequestService.getAllAccreditationRequestToAnswer(pageRequest);

Expand All @@ -221,7 +222,7 @@ public void getAllAccreditationRequestToAnswer() {
application2.setId(2L);
applications.add(application2);

PageImpl<Application> applicationPage = new PageImpl<>(applications);
PageImpl<Application> applicationPage = new PageImpl<>(applications, Pageable.ofSize(applications.size()), applications.size());
when(applicationRepository.findAllManagedByUser(any(), any())).thenReturn(applicationPage);


Expand All @@ -248,7 +249,7 @@ public void getAllAccreditationRequestToAnswer() {
accreditationRequestList.add(accreditationRequest2);
PageRequest pageRequest = PageRequest.of(0, 10);

when(accreditationRequestRepository.findAll(any(Predicate.class), eq(pageRequest))).thenReturn(new PageImpl<>(accreditationRequestList));
when(accreditationRequestRepository.findAll(any(Predicate.class), eq(pageRequest))).thenReturn(new PageImpl<>(accreditationRequestList, Pageable.ofSize(accreditationRequestList.size()), accreditationRequestList.size()));

Page<AccreditationRequest> result = accreditationRequestService.getAllAccreditationRequestToAnswer(pageRequest);

Expand Down Expand Up @@ -286,7 +287,7 @@ public void getMyAccreditationRequest() {
accreditationRequestList.add(accreditationRequest2);
PageRequest pageRequest = PageRequest.of(0, 10);

when(accreditationRequestRepository.findAll(any(Predicate.class), eq(pageRequest))).thenReturn(new PageImpl<>(accreditationRequestList));
when(accreditationRequestRepository.findAll(any(Predicate.class), eq(pageRequest))).thenReturn(new PageImpl<>(accreditationRequestList, Pageable.ofSize(accreditationRequestList.size()), accreditationRequestList.size()));

Page<AccreditationRequest> result = accreditationRequestService.getMyAccreditationRequest(pageRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void findTokenByUser() {
ApiToken apiToken2 = ApiToken.builder().id(2L).build();
apiTokens.add(apiToken1);
apiTokens.add(apiToken2);
Page<ApiToken> apiTokenPage = new PageImpl<>(apiTokens);
Page<ApiToken> apiTokenPage = new PageImpl<>(apiTokens, Pageable.ofSize(apiTokens.size()), apiTokens.size());
when(apiTokenRepository.findAllByUserLogin(eq("myLogin"), eq(pageable))).thenReturn(apiTokenPage);

Page<ApiToken> results = apiTokenService.findTokenByUser("myLogin", pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import fr.icdc.ebad.service.AccreditationRequestService;
import fr.icdc.ebad.web.rest.dto.CreationAccreditationRequestDto;
import fr.icdc.ebad.web.rest.dto.ResponseAccreditationRequestDto;
import fr.icdc.ebad.web.rest.errors.ExceptionTranslator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -18,6 +19,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
Expand All @@ -38,6 +40,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
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;

Expand All @@ -53,6 +56,9 @@ public class AccreditationRequestResourceTest {
@Autowired
private AccreditationRequestResource accreditationRequestResource;

@Autowired
private ExceptionTranslator exceptionTranslator;

@Autowired
private WebApplicationContext context;

Expand All @@ -64,6 +70,7 @@ public void setup() {
this.restMvc = MockMvcBuilders
.standaloneSetup(accreditationRequestResource)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.setControllerAdvice(exceptionTranslator)
.build();
objectMapper.registerModule(new JavaTimeModule());
}
Expand Down Expand Up @@ -93,12 +100,13 @@ public void findAll() throws Exception {
accreditationRequestList.add(accreditationRequest1);
accreditationRequestList.add(accreditationRequest2);

Page<AccreditationRequest> accreditationRequestPage = new PageImpl<>(accreditationRequestList);
Page<AccreditationRequest> accreditationRequestPage = new PageImpl<>(accreditationRequestList, Pageable.ofSize(2), 2);


when(accreditationRequestService.getAllAccreditationRequestToAnswer(any())).thenReturn(accreditationRequestPage);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/accreditation-requests/need-answer");
restMvc.perform(builder)
restMvc.perform(builder).andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content", hasSize(2)))
Expand Down Expand Up @@ -139,7 +147,7 @@ public void findAllMyRequest() throws Exception {
accreditationRequestList.add(accreditationRequest1);
accreditationRequestList.add(accreditationRequest2);

Page<AccreditationRequest> accreditationRequestPage = new PageImpl<>(accreditationRequestList);
Page<AccreditationRequest> accreditationRequestPage = new PageImpl<>(accreditationRequestList, Pageable.ofSize(accreditationRequestList.size()), accreditationRequestList.size());

when(accreditationRequestService.getMyAccreditationRequest(any())).thenReturn(accreditationRequestPage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void findToken() throws Exception {
ApiToken apiToken2 = ApiToken.builder().id(2L).build();
apiTokens.add(apiToken1);
apiTokens.add(apiToken2);
Page<ApiToken> apiTokenPage = new PageImpl<>(apiTokens);
Page<ApiToken> apiTokenPage = new PageImpl<>(apiTokens, Pageable.ofSize(apiTokens.size()), apiTokens.size());
when(apiTokenService.findTokenByUser(eq("user"), ArgumentMatchers.any(Pageable.class))).thenReturn(apiTokenPage);

restMvc.perform(builder)
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/fr/icdc/ebad/web/rest/ApplicationResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.querydsl.QuerydslPredicateArgumentResolver;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -103,7 +104,7 @@ public void findApplication() throws Exception {
Application application2 = new Application();
application2.setId(2L);
applications.add(application2);
PageImpl<Application> applicationPage = new PageImpl<>(applications);
PageImpl<Application> applicationPage = new PageImpl<>(applications, Pageable.ofSize(applications.size()), applications.size());

when(applicationService.findApplication(any(), any())).thenReturn(applicationPage);

Expand Down Expand Up @@ -131,7 +132,7 @@ public void getAll() throws Exception {
Application application2 = new Application();
application2.setId(2L);
applications.add(application2);
PageImpl<Application> applicationPage = new PageImpl<>(applications);
PageImpl<Application> applicationPage = new PageImpl<>(applications, Pageable.ofSize(applications.size()), applications.size());

when(applicationService.getAllApplicationsUsed(any(), eq("user"))).thenReturn(applicationPage);
when(userRepository.findUserFromApplication(anyLong(), anyString())).thenReturn(new User());
Expand Down Expand Up @@ -160,7 +161,8 @@ public void getAllWrite() throws Exception {
application2.setId(2L);
applications.add(application2);

PageImpl<Application> applicationPage = new PageImpl<>(applications);
PageImpl<Application> applicationPage = new PageImpl<>(applications, Pageable.ofSize(applications.size()), applications.size());


when(applicationService.getAllApplicationsManaged(any(), eq("dtrouillet"))).thenReturn(applicationPage);
when(userRepository.findManagerFromApplication(eq(1L), eq("dtrouillet"))).thenReturn(new User());
Expand Down Expand Up @@ -188,7 +190,7 @@ public void getAllManage() throws Exception {
application2.setId(2L);
applications.add(application2);

PageImpl<Application> applicationPage = new PageImpl<>(applications);
PageImpl<Application> applicationPage = new PageImpl<>(applications, Pageable.ofSize(applications.size()), applications.size());

when(applicationService.getAllApplications(any(), any())).thenReturn(applicationPage);
restMvc.perform(builder)
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/fr/icdc/ebad/web/rest/ChaineResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.querydsl.QuerydslPredicateArgumentResolver;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -96,7 +97,7 @@ public void getAllFromEnv() throws Exception {
chaines.add(chaine1);
chaines.add(chaine2);

Page<Chaine> chainePage = new PageImpl<>(chaines);
Page<Chaine> chainePage = new PageImpl<>(chaines, Pageable.ofSize(chaines.size()), chaines.size());

when(chaineService.getAllChaineFromEnvironmentWithPageable(any(), any(), argThat((environnement -> environnement.getId().equals(1L))))).thenReturn(chainePage);
when(permissionEnvironnement.canRead(eq(1L), any())).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.querydsl.QuerydslPredicateArgumentResolver;
import org.springframework.security.core.userdetails.UserDetails;
Expand Down Expand Up @@ -100,7 +101,7 @@ public void getAllFromEnv() throws Exception {
Directory directory2 = Directory.builder().id(3L).name("directory2").build();
directoryList.add(directory1);
directoryList.add(directory2);
Page<Directory> directoryPage = new PageImpl<>(directoryList);
Page<Directory> directoryPage = new PageImpl<>(directoryList, Pageable.ofSize(directoryList.size()), directoryList.size());

when(directoryService.findDirectoryFromEnvironnement(any(), any(), eq(1L))).thenReturn(directoryPage);
when(permissionEnvironnement.canRead(eq(1L), any())).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.querydsl.QuerydslPredicateArgumentResolver;
import org.springframework.security.test.context.support.WithMockUser;
Expand Down Expand Up @@ -77,7 +78,7 @@ public void getEnvironmentsFromApp() throws Exception {
List<Environnement> environnementList = new ArrayList<>();
environnementList.add(environnement1);
environnementList.add(environnement2);
Page<Environnement> environnementPage = new PageImpl<>(environnementList);
Page<Environnement> environnementPage = new PageImpl<>(environnementList, Pageable.ofSize(environnementList.size()), environnementList.size());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/environments?applicationId=1");
when(environnementService.getEnvironmentFromApp(eq(1L), any(Predicate.class), any())).thenReturn(environnementPage);
when(permissionApplication.canRead(eq(1L), any())).thenReturn(true);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/fr/icdc/ebad/web/rest/IdentityResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void getAllIdentities() throws Exception {
identities.add(identity2);
identities.add(identity3);

Page<Identity> identityPage = new PageImpl<>(identities);
Page<Identity> identityPage = new PageImpl<>(identities, Pageable.ofSize(identities.size()), identities.size());
when(identityService.findWithoutApp(any(Predicate.class), any(Pageable.class))).thenReturn(identityPage);

restMvc.perform(
Expand Down Expand Up @@ -162,7 +162,7 @@ public void getAllIdentitiesWithApplication() throws Exception {
identities.add(identity2);
identities.add(identity3);

Page<Identity> identityPage = new PageImpl<>(identities);
Page<Identity> identityPage = new PageImpl<>(identities, Pageable.ofSize(identities.size()), identities.size());
when(identityService.findAllByApplication(eq(1L), any(Predicate.class), any(Pageable.class))).thenReturn(identityPage);

restMvc.perform(
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/fr/icdc/ebad/web/rest/LogsResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.querydsl.QuerydslPredicateArgumentResolver;
import org.springframework.security.test.context.support.WithMockUser;
Expand Down Expand Up @@ -86,7 +87,7 @@ public void getAllLog() throws Exception {
logBatches.add(logBatch1);
logBatches.add(logBatch2);

PageImpl<LogBatch> logBatchPage = new PageImpl<>(logBatches);
PageImpl<LogBatch> logBatchPage = new PageImpl<>(logBatches, Pageable.ofSize(logBatches.size()), logBatches.size());

when(logBatchService.getAllLogBatchWithPageable(any(), any())).thenReturn(logBatchPage);

Expand Down Expand Up @@ -114,7 +115,7 @@ public void getAllLogFromEnv() throws Exception {
logBatches.add(logBatch1);
logBatches.add(logBatch2);

PageImpl<LogBatch> logBatchPage = new PageImpl<>(logBatches);
PageImpl<LogBatch> logBatchPage = new PageImpl<>(logBatches, Pageable.ofSize(logBatches.size()), logBatches.size());

when(logBatchService.getAllLogBatchWithPageable(any(), any())).thenReturn(logBatchPage);
when(permissionEnvironnement.canRead(eq(1L), any())).thenReturn(true);
Expand Down Expand Up @@ -142,7 +143,7 @@ public void getAllLogFromEnvBatch() throws Exception {
logBatches.add(logBatch1);
logBatches.add(logBatch2);

PageImpl<LogBatch> logBatchPage = new PageImpl<>(logBatches);
PageImpl<LogBatch> logBatchPage = new PageImpl<>(logBatches, Pageable.ofSize(logBatches.size()), logBatches.size());

when(logBatchService.getAllLogBatchWithPageable(any(), any())).thenReturn(logBatchPage);
when(permissionEnvironnement.canRead(eq(1L), any())).thenReturn(true);
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/fr/icdc/ebad/web/rest/NewResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void getAll() throws Exception {
Actualite actualite2 = new Actualite();
actualite2.setId(2L);
actualites.add(actualite2);
Page<Actualite> actualitePage = new PageImpl<>(actualites);
Page<Actualite> actualitePage = new PageImpl<>(actualites, Pageable.ofSize(actualites.size()), actualites.size());
when(newService.getAllActualites(ArgumentMatchers.any())).thenReturn(actualitePage);

restMvc.perform(builder)
Expand All @@ -100,7 +101,7 @@ public void getAllNonDraft() throws Exception {
Actualite actualite2 = new Actualite();
actualite2.setId(2L);
actualites.add(actualite2);
Page<Actualite> actualitePage = new PageImpl<>(actualites);
Page<Actualite> actualitePage = new PageImpl<>(actualites, Pageable.ofSize(actualites.size()), actualites.size());

when(newService.getAllActualitesPubliees(ArgumentMatchers.any())).thenReturn(actualitePage);

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/fr/icdc/ebad/web/rest/NormResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void getAll() throws Exception {

normeList.add(norme1);
normeList.add(norme2);
Page<Norme> normePage = new PageImpl<>(normeList);
Page<Norme> normePage = new PageImpl<>(normeList, Pageable.ofSize(normeList.size()), normeList.size());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/norms");

when(normeService.getAllNormes(any(Predicate.class), any(Pageable.class))).thenReturn(normePage);
Expand Down Expand Up @@ -105,7 +105,7 @@ public void getAllList() throws Exception {

normeList.add(norme1);
normeList.add(norme2);
Page<Norme> normePage = new PageImpl<>(normeList);
Page<Norme> normePage = new PageImpl<>(normeList, Pageable.ofSize(normeList.size()), normeList.size());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/norms/name");

when(normeService.getAllNormes(any(Predicate.class), any(Pageable.class))).thenReturn(normePage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
Expand Down Expand Up @@ -140,7 +141,7 @@ public void listByEnvironment() throws Exception {
schedulings.add(scheduling1);
schedulings.add(scheduling2);

PageImpl<Scheduling> page = new PageImpl<>(schedulings);
PageImpl<Scheduling> page = new PageImpl<>(schedulings, Pageable.ofSize(schedulings.size()), schedulings.size());

when(permissionEnvironnement.canRead(eq(2L), any())).thenReturn(true);
when(schedulingService.listByEnvironment(eq(2L), any())).thenReturn(page);
Expand Down Expand Up @@ -191,7 +192,7 @@ public void listAll() throws Exception {
schedulings.add(scheduling1);
schedulings.add(scheduling2);

PageImpl<Scheduling> page = new PageImpl<>(schedulings);
PageImpl<Scheduling> page = new PageImpl<>(schedulings, Pageable.ofSize(schedulings.size()), schedulings.size());

when(schedulingService.listAll(any())).thenReturn(page);
restMvc.perform(builder)
Expand Down
Loading

0 comments on commit 4a2f778

Please sign in to comment.