Skip to content

Commit

Permalink
Migrate from Assertj to JUnit Jupiter (#922)
Browse files Browse the repository at this point in the history
* Migrate from assertj to JUnit Jupiter

* Fix checkstyle
  • Loading branch information
loicgreffier authored Jan 3, 2025
1 parent 1cbb304 commit f151fd0
Show file tree
Hide file tree
Showing 72 changed files with 2,185 additions and 1,892 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/michelin/suricate/model/entity/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ public class Project extends AbstractAuditingEntity<Long> {

@ToString.Exclude
@ManyToMany
@JoinTable(name = "user_project", joinColumns = {@JoinColumn(name = "project_id")}, inverseJoinColumns = {
@JoinColumn(name = "user_id")})
@JoinTable(
name = "user_project",
joinColumns = {@JoinColumn(name = "project_id")},
inverseJoinColumns = {@JoinColumn(name = "user_id")}
)
private Set<User> users = new LinkedHashSet<>();

/**
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/michelin/suricate/model/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ public class User extends AbstractEntity<Long> {

@ToString.Exclude
@ManyToMany
@JoinTable(name = "user_role", joinColumns = {@JoinColumn(name = "user_id")}, inverseJoinColumns = {
@JoinColumn(name = "role_id")})
@JoinTable(
name = "user_role",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "role_id")}
)
private Set<Role> roles = new LinkedHashSet<>();

@ToString.Exclude
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/michelin/suricate/model/entity/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ public class Widget extends AbstractAuditingEntity<Long> {

@ToString.Exclude
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "widget_library", joinColumns = {@JoinColumn(name = "widget_id")}, inverseJoinColumns = {
@JoinColumn(name = "library_id")})
@JoinTable(
name = "widget_library",
joinColumns = {@JoinColumn(name = "widget_id")},
inverseJoinColumns = {@JoinColumn(name = "library_id")}
)
private Set<Library> libraries = new LinkedHashSet<>();

@ToString.Exclude
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.michelin.suricate.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -45,7 +46,7 @@ void shouldGetAssetNotModified() {

ResponseEntity<byte[]> actual = assetController.getAsset(webRequest, "token");

assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
assertEquals(HttpStatus.NOT_MODIFIED, actual.getStatusCode());

verify(assetService)
.getAssetById("token");
Expand All @@ -69,11 +70,11 @@ void shouldGetAsset() {

ResponseEntity<byte[]> actual = assetController.getAsset(webRequest, "token");

assertThat(actual.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(actual.getHeaders().getContentLength()).isEqualTo(10L);
assertThat(actual.getHeaders().getLastModified()).isEqualTo(946688400000L);
assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(actual.getBody()).isEqualTo(new byte[10]);
assertEquals(MediaType.APPLICATION_JSON, actual.getHeaders().getContentType());
assertEquals(10L, actual.getHeaders().getContentLength());
assertEquals(946688400000L, actual.getHeaders().getLastModified());
assertEquals(HttpStatus.OK, actual.getStatusCode());
assertArrayEquals(new byte[10], actual.getBody());

verify(assetService)
.getAssetById("token");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.michelin.suricate.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -48,10 +49,10 @@ void shouldSignIn() {

ResponseEntity<JwtAuthenticationResponseDto> actual = authenticationController.signIn(signInRequestDto);

assertThat(actual.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(actual.getBody()).isNotNull();
assertThat(actual.getBody().getAccessToken()).isEqualTo("token");
assertEquals(MediaType.APPLICATION_JSON, actual.getHeaders().getContentType());
assertEquals(HttpStatus.OK, actual.getStatusCode());
assertNotNull(actual.getBody());
assertEquals("token", actual.getBody().getAccessToken());

verify(authenticationManager)
.authenticate(Mockito.<UsernamePasswordAuthenticationToken>argThat(auth ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.michelin.suricate.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -62,19 +64,22 @@ void shouldGetCategories() {

Page<CategoryResponseDto> actual = categoryController.getCategories("search", Pageable.unpaged());

assertThat(actual).isNotEmpty();
assertThat(actual.get()).hasSize(1);
assertThat(actual.get().toList().get(0)).isEqualTo(categoryResponseDto);
assertFalse(actual.isEmpty());
assertEquals(1, actual.get().count());
assertEquals(categoryResponseDto, actual.get().toList().getFirst());
}

@Test
void shouldGetWidgetByCategoryNoContent() {
when(widgetService.getWidgetsByCategory(any()))
.thenReturn(Optional.empty());

assertThatThrownBy(() -> categoryController.getWidgetByCategory(1L))
.isInstanceOf(NoContentException.class)
.hasMessage("No resource for the class 'Widget'");
NoContentException exception = assertThrows(
NoContentException.class,
() -> categoryController.getWidgetByCategory(1L)
);

assertEquals("No resource for the class 'Widget'", exception.getMessage());
}

@Test
Expand All @@ -92,10 +97,10 @@ void shouldGetWidgetByCategory() {

ResponseEntity<List<WidgetResponseDto>> actual = categoryController.getWidgetByCategory(1L);

assertThat(actual.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(actual.getBody()).isNotNull();
assertThat(actual.getBody()).hasSize(1);
assertThat(actual.getBody().get(0)).isEqualTo(widgetResponseDto);
assertEquals(MediaType.APPLICATION_JSON, actual.getHeaders().getContentType());
assertEquals(HttpStatus.OK, actual.getStatusCode());
assertNotNull(actual.getBody());
assertEquals(1, actual.getBody().size());
assertEquals(widgetResponseDto, actual.getBody().getFirst());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.michelin.suricate.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -55,19 +57,22 @@ void shouldGetAll() {

Page<CategoryParameterResponseDto> actual = categoryParametersController.getAll("search", Pageable.unpaged());

assertThat(actual).isNotEmpty();
assertThat(actual.get()).hasSize(1);
assertThat(actual.get().toList().get(0)).isEqualTo(categoryParameterResponseDto);
assertFalse(actual.isEmpty());
assertEquals(1, actual.get().count());
assertEquals(categoryParameterResponseDto, actual.get().toList().getFirst());
}

@Test
void shouldGetOneByKeyNotFound() {
when(categoryParametersService.getOneByKey(any()))
.thenReturn(Optional.empty());

assertThatThrownBy(() -> categoryParametersController.getOneByKey("key"))
.isInstanceOf(ObjectNotFoundException.class)
.hasMessage("CategoryParameter 'key' not found");
ObjectNotFoundException exception = assertThrows(
ObjectNotFoundException.class,
() -> categoryParametersController.getOneByKey("key")
);

assertEquals("CategoryParameter 'key' not found", exception.getMessage());
}

@Test
Expand All @@ -85,10 +90,10 @@ void shouldGetOneByKey() {

ResponseEntity<CategoryParameterResponseDto> actual = categoryParametersController.getOneByKey("key");

assertThat(actual.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(actual.getBody()).isNotNull();
assertThat(actual.getBody()).isEqualTo(categoryParameterResponseDto);
assertEquals(MediaType.APPLICATION_JSON, actual.getHeaders().getContentType());
assertEquals(HttpStatus.OK, actual.getStatusCode());
assertNotNull(actual.getBody());
assertEquals(categoryParameterResponseDto, actual.getBody());
}

@Test
Expand All @@ -99,9 +104,12 @@ void shouldUpdateOneByKeyNotFound() {
when(categoryParametersService.getOneByKey(any()))
.thenReturn(Optional.empty());

assertThatThrownBy(() -> categoryParametersController.updateOneByKey("key", widgetConfigurationRequestDto))
.isInstanceOf(ObjectNotFoundException.class)
.hasMessage("CategoryParameter 'key' not found");
ObjectNotFoundException exception = assertThrows(
ObjectNotFoundException.class,
() -> categoryParametersController.updateOneByKey("key", widgetConfigurationRequestDto)
);

assertEquals("CategoryParameter 'key' not found", exception.getMessage());
}

@Test
Expand All @@ -117,17 +125,20 @@ void shouldUpdateOneByKey() {

ResponseEntity<Void> actual = categoryParametersController.updateOneByKey("key", widgetConfigurationRequestDto);

assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
assertEquals(HttpStatus.NO_CONTENT, actual.getStatusCode());
}

@Test
void shouldDeleteOneByKeyNotFound() {
when(categoryParametersService.getOneByKey(any()))
.thenReturn(Optional.empty());

assertThatThrownBy(() -> categoryParametersController.deleteOneByKey("key"))
.isInstanceOf(ObjectNotFoundException.class)
.hasMessage("CategoryParameter 'key' not found");
ObjectNotFoundException exception = assertThrows(
ObjectNotFoundException.class,
() -> categoryParametersController.deleteOneByKey("key")
);

assertEquals("CategoryParameter 'key' not found", exception.getMessage());
}

@Test
Expand All @@ -140,6 +151,6 @@ void shouldDeleteOneByKey() {

ResponseEntity<Void> actual = categoryParametersController.deleteOneByKey("key");

assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
assertEquals(HttpStatus.NO_CONTENT, actual.getStatusCode());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.michelin.suricate.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

import com.michelin.suricate.model.enumeration.AuthenticationProvider;
Expand Down Expand Up @@ -34,13 +37,11 @@ void shouldGetAuthenticationProviders() {

ResponseEntity<List<AuthenticationProvider>> actual = configurationController.getAuthenticationProviders();

assertThat(actual.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(actual.getBody()).isNotNull();
assertThat(actual.getBody()).hasSize(2);
assertThat(actual.getBody())
.contains(AuthenticationProvider.LDAP)
.contains(AuthenticationProvider.GITHUB);
assertEquals(MediaType.APPLICATION_JSON, actual.getHeaders().getContentType());
assertEquals(HttpStatus.OK, actual.getStatusCode());
assertNotNull(actual.getBody());
assertEquals(2, actual.getBody().size());
assertIterableEquals(List.of(AuthenticationProvider.LDAP, AuthenticationProvider.GITHUB), actual.getBody());
}

@Test
Expand All @@ -51,9 +52,9 @@ void shouldGetAuthenticationProvidersEmpty() {

ResponseEntity<List<AuthenticationProvider>> actual = configurationController.getAuthenticationProviders();

assertThat(actual.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(actual.getBody()).isNotNull();
assertThat(actual.getBody()).isEmpty();
assertEquals(MediaType.APPLICATION_JSON, actual.getHeaders().getContentType());
assertEquals(HttpStatus.OK, actual.getStatusCode());
assertNotNull(actual.getBody());
assertTrue(actual.getBody().isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.michelin.suricate.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.argThat;
import static org.mockito.Mockito.eq;
Expand Down Expand Up @@ -79,11 +81,11 @@ void shouldExports() {

ResponseEntity<ImportExportDto> actual = importExportController.exports();

assertThat(actual.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(actual.getBody()).isNotNull();
assertThat(actual.getBody().getRepositories().get(0)).isEqualTo(importExportRepositoryDto);
assertThat(actual.getBody().getProjects().get(0)).isEqualTo(importExportProjectDto);
assertEquals(MediaType.APPLICATION_JSON, actual.getHeaders().getContentType());
assertEquals(HttpStatus.OK, actual.getStatusCode());
assertNotNull(actual.getBody());
assertEquals(importExportRepositoryDto, actual.getBody().getRepositories().getFirst());
assertEquals(importExportProjectDto, actual.getBody().getProjects().getFirst());
}

@Test
Expand Down Expand Up @@ -126,9 +128,9 @@ void shouldImports() throws GitAPIException, IOException {

ResponseEntity<Void> actual = importExportController.imports(localUser, importExportDto);

assertThat(actual.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
assertThat(actual.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(actual.getBody()).isNull();
assertEquals(MediaType.APPLICATION_JSON, actual.getHeaders().getContentType());
assertEquals(HttpStatus.OK, actual.getStatusCode());
assertNull(actual.getBody());

verify(repositoryMapper)
.toRepositoryEntity(importExportRepositoryDto);
Expand Down
Loading

0 comments on commit f151fd0

Please sign in to comment.