Début de la création de tests unitaires

This commit is contained in:
FyloZ 2020-03-08 22:40:18 -04:00
parent a47f82462d
commit cf7956637b
18 changed files with 1293 additions and 1650 deletions

View File

@ -0,0 +1,16 @@
package dev.fyloz.trial.colorrecipesexplorer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ColorRecipesExplorerApplicationTest {
@Test
public void contextLoads() {
}
}

View File

@ -1,16 +0,0 @@
package dev.fyloz.trial.colorrecipesexplorer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ColorRecipesExplorerApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@ -0,0 +1,23 @@
package dev.fyloz.trial.colorrecipesexplorer.builders;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Company;
public class CompanyBuilder {
private Long id = 0L;
private String name = "Trial";
public CompanyBuilder id(Long val) {
id = val;
return this;
}
public CompanyBuilder name(String val) {
name = val;
return this;
}
public Company build() {
return new Company(id, name);
}
}

View File

@ -0,0 +1,50 @@
package dev.fyloz.trial.colorrecipesexplorer.builders;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Material;
import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType;
public class MaterialBuilder {
private Long id = 0L;
private String name = "DEDEL";
private Float inventoryQuantity = 50000.00f;
private boolean isMixType = false;
private MaterialType materialType = new MaterialTypeBuilder().build();
public MaterialBuilder() {
}
public MaterialBuilder id(Long id) {
this.id = id;
return this;
}
public MaterialBuilder name(String name) {
this.name = name;
return this;
}
public MaterialBuilder inventoryQuantity(Float inventoryQuantity) {
this.inventoryQuantity = inventoryQuantity;
return this;
}
public MaterialBuilder mixType(boolean mixType) {
isMixType = mixType;
return this;
}
public MaterialBuilder materialType(MaterialType materialType) {
this.materialType = materialType;
return this;
}
public Material build() {
return new Material(id, name, inventoryQuantity, isMixType, materialType);
}
}

View File

@ -0,0 +1,39 @@
package dev.fyloz.trial.colorrecipesexplorer.builders;
import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType;
public class MaterialTypeBuilder {
private Long id = 0L;
private String name = "Base";
private String prefix = "BAS";
private boolean usePercentages = false;
public MaterialTypeBuilder id(Long id) {
this.id = id;
return this;
}
public MaterialTypeBuilder name(String name) {
this.name = name;
return this;
}
public MaterialTypeBuilder prefix(String prefix) {
this.prefix = prefix;
return this;
}
public MaterialTypeBuilder usePercentages(boolean usePercentages) {
this.usePercentages = usePercentages;
return this;
}
public MaterialType build() {
return new MaterialType(id, name, prefix, usePercentages);
}
}

View File

@ -0,0 +1,35 @@
package dev.fyloz.trial.colorrecipesexplorer.builders;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Material;
import dev.fyloz.trial.colorrecipesexplorer.core.model.MixType;
import lombok.NonNull;
import javax.validation.constraints.NotNull;
public class MixTypeBuilder {
private Long id = 0L;
private String name = "Lacque Coloré";
private Material material = new MaterialBuilder().build();
public MixTypeBuilder() {
}
public MixTypeBuilder id(Long val) {
id = val;
return this;
}
public MixTypeBuilder name(@NonNull @NotNull String val) {
name = val;
return this;
}
public MixTypeBuilder material(@NonNull @NotNull Material val) {
material = val;
return this;
}
public MixType build() {
return new MixType(id, name, material);
}
}

View File

@ -1,546 +0,0 @@
package dev.fyloz.trial.colorrecipesexplorer.core.services;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Company;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Recipe;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.CompanyService;
import dev.fyloz.trial.colorrecipesexplorer.dao.CompanyDao;
import dev.fyloz.trial.colorrecipesexplorer.dao.RecipeDao;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class CompanyServiceTests {
@Mock
private CompanyDao companyDao;
@Mock
private RecipeDao recipeDao;
@Spy
@InjectMocks
private CompanyService companyService;
private Company testCompany;
private Company testCompany2;
private List<Company> testCompaniesList;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
testCompany = new Company("Test Company");
testCompany.setCompanyId(0);
testCompany2 = new Company("Test Company 2");
testCompany.setCompanyID(1);
testCompaniesList = new ArrayList<>();
testCompaniesList.add(testCompany);
testCompaniesList.add(testCompany2);
}
@Test
public void isValidForCreationTestShouldBeTrue() {
// Arrange
when(companyService.existsByName(testCompany.getName())).thenReturn(false);
when(companyService.exists(testCompany)).thenReturn(false);
// Act
boolean actual = companyService.isValidForCreation(testCompany);
// Assert
assertTrue(actual);
}
@Test
public void isValidForCreationTestShouldBeFalse() {
// Arrange
when(companyService.existsByName(testCompany.getName())).thenReturn(true);
when(companyService.exists(testCompany)).thenReturn(true);
// Act
boolean actual = companyService.isValidForCreation(testCompany);
// Assert
assertFalse(actual);
}
@Test
public void isValidForCreationTestExists() {
// Arrange
when(companyService.existsByName(testCompany.getName())).thenReturn(false);
when(companyService.exists(testCompany)).thenReturn(true);
// Act
boolean actual = companyService.isValidForCreation(testCompany);
// Assert
assertFalse(actual);
}
@Test
public void isValidForCreationTestNameExists() {
// Arrange
when(companyService.existsByName(testCompany.getName())).thenReturn(true);
when(companyService.exists(testCompany)).thenReturn(false);
// Act
boolean actual = companyService.isValidForCreation(testCompany);
// Assert
assertFalse(actual);
}
@Test
public void isValidForCreationTestNull() {
// Act
boolean actual = companyService.isValidForCreation(null);
// Assert
assertFalse(actual);
}
@Test
public void existsByNameTestShouldBeTrue() {
// Arrange
when(companyDao.existsByCompanyName(testCompany.getName())).thenReturn(true);
// Act
boolean actual = companyService.existsByName(testCompany.getName());
// Assert
assertTrue(actual);
}
@Test
public void existsByNameTestShouldBeFalse() {
// Arrange
when(companyDao.existsByCompanyName(testCompany.getName())).thenReturn(false);
// Act
boolean actual = companyService.existsByName(testCompany.getName());
// Assert
assertFalse(actual);
}
@Test
public void existsByNameTestNull() {
// Act
boolean actual = companyService.existsByName(null);
// Assert
assertFalse(actual);
}
@Test
public void isLinkedToRecipesTestShouldBeTrue() {
// Arrange
List<Recipe> recipesList = new ArrayList<>();
recipesList.add(new Recipe());
when(recipeDao.findAllByCompany(testCompany)).thenReturn(recipesList);
// Act
boolean actual = companyService.isLinkedToRecipes(testCompany);
// Assert
assertTrue(actual);
}
@Test
public void isLinkedToRecipesTestShouldBeFalse() {
// Arrange
when(recipeDao.findAllByCompany(testCompany)).thenReturn(new ArrayList<>());
// Act
boolean actual = companyService.isLinkedToRecipes(testCompany);
// Assert
assertFalse(actual);
}
@Test
public void isLinkedToRecipesTestNull() {
// Act
boolean actual = companyService.isLinkedToRecipes(null);
// Assert
assertFalse(actual);
}
@Test
public void deleteIfNotLinkedTestShouldBeTrue() {
// Arrange
doReturn(true).when(companyService).delete(testCompany);
doReturn(false).when(companyService).isLinkedToRecipes(testCompany);
// Act
boolean actual = companyService.deleteIfNotLinked(testCompany);
// Assert
assertTrue(actual);
}
@Test
public void deleteIfNotLinkedTestShouldBeFalse() {
// Arrange
doReturn(true).when(companyService).delete(testCompany);
doReturn(true).when(companyService).isLinkedToRecipes(testCompany);
// Act
boolean actual = companyService.deleteIfNotLinked(testCompany);
// Assert
assertFalse(actual);
}
@Test
public void deleteIfNotLinkedTestNull() {
// Arrange
doReturn(true).when(companyService).delete(testCompany);
// Act
boolean actual = companyService.deleteIfNotLinked(null);
// Assert
assertFalse(actual);
}
// Test les méthodes génériques
@Test
public void getByIDTest() {
// Arrange
doReturn(Optional.of(testCompany)).when(companyDao).findById(testCompany.getCompanyID());
// Act
Optional<Company> actual = companyService.getByID(testCompany.getCompanyID());
// Assert
assertTrue(actual.isPresent());
assertEquals(testCompany, actual.get());
}
@Test
public void getByIDTestShouldBeNull() {
// Arrange
doReturn(Optional.empty()).when(companyDao).findById(testCompany.getCompanyID());
// Act
Optional<Company> actual = companyService.getByID(testCompany.getCompanyID());
// Arrange
assertFalse(actual.isPresent());
}
@Test
public void getAllTest() {
// Arrange
List<Company> companies = new ArrayList<>();
companies.add(testCompany);
doReturn(companies).when(companyDao).findAll();
// Act
List<Company> actual = companyService.getAll();
// Assert
assertTrue(actual.contains(testCompany));
}
@Test
public void getAllTestShouldBeEmpty() {
// Arrange
doReturn(new ArrayList<>()).when(companyDao).findAll();
// Act
List<Company> actual = companyService.getAll();
// Assert
assertTrue(actual.isEmpty());
}
@Test
public void saveTest() {
// Arrange
doReturn(true).when(companyService).isValidForCreation(testCompany);
doReturn(testCompany).when(companyDao).save(testCompany);
// Act
Optional<Company> actual = companyService.save(testCompany);
// Assert
assertTrue(actual.isPresent());
assertEquals(testCompany, actual.get());
}
@Test
public void saveTestInvalidCompany() {
// Arrange
doReturn(false).when(companyService).isValidForCreation(testCompany);
doReturn(testCompany).when(companyDao).save(testCompany);
// Act
Optional<Company> actual = companyService.save(testCompany);
// Assert
assertFalse(actual.isPresent());
}
@Test
public void saveTestNull() {
// Arrange
doReturn(false).when(companyService).isValidForCreation(testCompany);
doReturn(testCompany).when(companyDao).save(testCompany);
// Act
Optional<Company> actual = companyService.save(null);
// Assert
assertFalse(actual.isPresent());
}
@Test
public void saveAllTestShouldBeTrue() {
// Arrange
doAnswer(i -> Optional.of(i.getArguments()[0])).when(companyService).save(any(Company.class));
// Act
boolean actual = companyService.saveAll(testCompaniesList);
// Assert
assertTrue(actual);
}
@Test
public void saveAllTestShouldBeFalse() {
// Arrange
doAnswer(i -> Optional.empty()).when(companyService).save(any(Company.class));
// Act
boolean actual = companyService.saveAll(testCompaniesList);
// Assert
assertFalse(actual);
}
@Test
public void saveAllTestOneSaveFalse() {
// Arrange
doReturn(Optional.of(testCompany)).when(companyService).save(testCompany);
doReturn(Optional.empty()).when(companyService).save(testCompany2);
// Act
boolean actual = companyService.saveAll(testCompaniesList);
// Assert
assertFalse(actual);
}
@Test
public void saveAllTestNull() {
// Arrange
doAnswer(i -> Optional.empty()).when(companyService).save(any(Company.class));
// Act
boolean actual = companyService.saveAll(null);
// Assert
assertFalse(actual);
}
@Test
public void updateTest() {
// Arrange
doReturn(true).when(companyService).isValidForUpdate(testCompany);
doReturn(testCompany).when(companyDao).save(testCompany);
// Act
Optional<Company> actual = companyService.update(testCompany);
// Assert
assertTrue(actual.isPresent());
assertEquals(testCompany, actual.get());
}
@Test
public void updateTestInvalidCompany() {
// Arrange
doReturn(false).when(companyService).isValidForUpdate(testCompany);
doReturn(testCompany).when(companyDao).save(testCompany);
// Act
Optional<Company> actual = companyService.update(testCompany);
// Assert
assertFalse(actual.isPresent());
}
@Test
public void updateTestNull() {
// Arrange
doReturn(false).when(companyService).isValidForUpdate(testCompany);
doReturn(testCompany).when(companyDao).save(testCompany);
// Act
Optional<Company> actual = companyService.update(null);
// Assert
assertFalse(actual.isPresent());
}
@Test
public void deleteTestShouldBeTrue() {
// Arrange
doReturn(true).when(companyService).exists(testCompany);
// Act
boolean actual = companyService.delete(testCompany);
// Assert
assertTrue(actual);
}
@Test
public void deleteTestShouldBeFalse() {
// Arrange
doReturn(false).when(companyService).exists(testCompany);
// Act
boolean actual = companyService.delete(testCompany);
// Assert
assertFalse(actual);
}
@Test
public void deleteTestNull() {
// Arrange
doReturn(true).when(companyService).exists(testCompany);
// Act
boolean actual = companyService.delete(null);
// Assert
assertFalse(actual);
}
@Test
public void deleteAllTestShouldBeTrue() {
// Arrange
doReturn(true).when(companyService).exists(any());
// Act
boolean actual = companyService.deleteAll(testCompaniesList);
// Assert
assertTrue(actual);
}
@Test
public void deleteAllTestShouldBeFalse() {
// Arrange
doReturn(false).when(companyService).exists(any());
// Act
boolean actual = companyService.deleteAll(testCompaniesList);
// Assert
assertFalse(actual);
}
@Test
public void deleteAllTestNull() {
// Arrange
doReturn(false).when(companyService).exists(any());
// Act
boolean actual = companyService.deleteAll(null);
// Assert
assertFalse(actual);
}
@Test
public void existsTestShouldBeTrue() {
// Arrange
doReturn(true).when(companyService).existsById(testCompany.getCompanyID());
// Act
boolean actual = companyService.exists(testCompany);
// Assert
assertTrue(actual);
}
@Test
public void existsTestShouldBeFalse() {
// Arrange
doReturn(false).when(companyService).existsById(testCompany.getCompanyID());
// Act
boolean actual = companyService.exists(testCompany);
// Assert
assertFalse(actual);
}
@Test
public void existsTestNull() {
// Arrange
doReturn(true).when(companyService).existsById(testCompany.getCompanyID());
// Act
boolean actual = companyService.exists(null);
// Assert
assertFalse(actual);
}
@Test
public void existsByIdTestShouldBeTrue() {
// Arrange
doReturn(true).when(companyDao).existsById(testCompany.getCompanyID());
// Act
boolean actual = companyService.exists(testCompany);
// Assert
assertTrue(actual);
}
@Test
public void existsByIdTestShouldBeFalse() {
// Arrange
doReturn(false).when(companyDao).existsById(testCompany.getCompanyID());
// Act
boolean actual = companyService.exists(testCompany);
// Assert
assertFalse(actual);
}
@Test
public void existsByIdTestNull() {
// Arrange
doReturn(true).when(companyDao).existsById(testCompany.getCompanyID());
// Act
boolean actual = companyService.exists(null);
// Assert
assertFalse(actual);
}
}

View File

@ -1,339 +0,0 @@
package dev.fyloz.trial.colorrecipesexplorer.core.services;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Material;
import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixQuantityService;
import dev.fyloz.trial.colorrecipesexplorer.dao.MaterialDao;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
public class MaterialServiceTests {
@Mock
private MaterialDao materialDao;
@Mock
private MixQuantityService mixQuantityService;
@Spy
@InjectMocks
private MaterialService materialService;
private Material testMaterial;
private MaterialType testMaterialType;
private List<Material> testMaterialsList;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
testMaterial = new Material();
testMaterial.setMaterialID(0);
testMaterial.setName("Test Material");
testMaterialType = new MaterialType();
testMaterialType.setMaterialTypeID(0);
testMaterialType.setMaterialTypeName("Test Type");
testMaterialsList = new ArrayList<>();
testMaterialsList.add(testMaterial);
}
@Test
public void getAllByMaterialTypeTest() {
// Arrange
doReturn(testMaterialsList).when(materialDao).findAllByMaterialType(testMaterialType);
// Act
List<Material> actual = materialService.getAllByMaterialType(testMaterialType);
// Assert
assertTrue(actual.contains(testMaterial));
}
@Test
public void getAllByMaterialTypeTestShouldBeEmpty() {
// Arrange
doReturn(new ArrayList<>()).when(materialDao).findAllByMaterialType(testMaterialType);
// Act
List<Material> actual = materialService.getAllByMaterialType(testMaterialType);
// Assert
assertTrue(actual.isEmpty());
}
@Test
public void getAllByMaterialTypeTestNull() {
// Arrange
doReturn(testMaterialsList).when(materialDao).findAllByMaterialType(any());
// Act
List<Material> actual = materialService.getAllByMaterialType(null);
// Assert
assertTrue(actual.isEmpty());
}
@Test
public void isLinkedToMixesTestShouldBeTrue() {
// Arrange
doReturn(true).when(mixQuantityService).existsByMaterial(testMaterial);
// Act
boolean actual = materialService.isLinkedToMixes(testMaterial);
// Arrange
assertTrue(actual);
}
@Test
public void isLinkedToMixesTestShouldBeFalse() {
// Arrange
doReturn(false).when(mixQuantityService).existsByMaterial(testMaterial);
// Act
boolean actual = materialService.isLinkedToMixes(testMaterial);
// Arrange
assertFalse(actual);
}
@Test
public void isLinkedToMixesTestNull() {
// Arrange
doReturn(false).when(mixQuantityService).existsByMaterial(any());
// Act
boolean actual = materialService.isLinkedToMixes(null);
// Arrange
assertFalse(actual);
}
@Test
public void deleteIfNotLinkedTestShouldBeTrue() {
// Arrange
doReturn(false).when(materialService).isLinkedToMixes(testMaterial);
doReturn(true).when(materialService).delete(testMaterial);
// Act
boolean actual = materialService.deleteIfNotLinked(testMaterial);
// Assert
assertTrue(actual);
}
@Test
public void deleteIfNotLinkedTestShouldBeFalse() {
// Arrange
doReturn(true).when(materialService).isLinkedToMixes(testMaterial);
doReturn(false).when(materialService).delete(testMaterial);
// Act
boolean actual = materialService.deleteIfNotLinked(testMaterial);
// Assert
assertFalse(actual);
}
@Test
public void deleteIfNotLinkedTestShouldBeFalseNotLinked() {
// Arrange
doReturn(false).when(materialService).isLinkedToMixes(testMaterial);
doReturn(false).when(materialService).delete(testMaterial);
// Act
boolean actual = materialService.deleteIfNotLinked(testMaterial);
// Assert
assertFalse(actual);
}
@Test
public void deleteIfNotLinkedTestNull() {
// Arrange
doReturn(false).when(materialService).isLinkedToMixes(testMaterial);
doReturn(true).when(materialService).delete(testMaterial);
// Act
boolean actual = materialService.deleteIfNotLinked(null);
// Assert
assertFalse(actual);
}
@Test
public void existsTestShouldBeTrue() {
// Arrange
doReturn(true).when((GenericService<Material, MaterialDao>) materialService).exists(testMaterial);
doReturn(true).when(materialDao).existsByMaterialCode(testMaterial.getName());
// Act
boolean actual = materialService.exists(testMaterial);
// Assert
assertTrue(actual);
}
@Test
public void existsTestShouldBeFalse() {
// Arrange
doReturn(false).when((GenericService<Material, MaterialDao>) materialService).exists(testMaterial);
doReturn(false).when(materialDao).existsByMaterialCode(testMaterial.getName());
// Act
boolean actual = materialService.exists(testMaterial);
// Assert
assertFalse(actual);
}
@Test
public void existsTestShouldBeTrueOneConditionMet() {
// Arrange
doReturn(true).when(materialDao).existsByMaterialCode(testMaterial.getName());
// Act
boolean actual = materialService.exists(testMaterial);
// Assert
assertTrue(actual);
}
@Test
public void existsTestNull() {
// Arrange
doReturn(true).when(materialDao).existsByMaterialCode(any());
// Act
boolean actual = materialService.exists(null);
// Assert
assertFalse(actual);
}
@Test
public void isValidForUpdateTestShouldBeTrue() {
// Arrange
doReturn(null).when(materialDao).findByMaterialCode(testMaterial.getName());
doReturn(true).when((GenericService<Material, MaterialDao>) materialService).isValidForUpdate(testMaterial);
// Act
boolean actual = materialService.isValidForUpdate(testMaterial);
// Assert
assertTrue(actual);
}
@Test
public void isValidForUpdateTestShouldBeTrueMaterialCodeExistsWithSameId() {
// Arrange
doReturn(testMaterial).when(materialDao).findByMaterialCode(testMaterial.getName());
doReturn(true).when((GenericService<Material, MaterialDao>) materialService).isValidForUpdate(testMaterial);
// Act
boolean actual = materialService.isValidForUpdate(testMaterial);
// Assert
assertTrue(actual);
}
@Test
public void isValidForUpdateTestShouldBeFalse() {
// Arrange
doReturn(testMaterial).when(materialDao).findByMaterialCode(testMaterial.getName());
doReturn(false).when((GenericService<Material, MaterialDao>) materialService).isValidForUpdate(testMaterial);
// Act
boolean actual = materialService.isValidForUpdate(testMaterial);
// Assert
assertFalse(actual);
}
@Test
public void isValidForUpdateTestShouldBeFalseMaterialCodeExists() {
// Arrange
Material otherMaterial = new Material();
otherMaterial.setName(testMaterial.getName());
otherMaterial.setMaterialID(1);
doReturn(otherMaterial).when(materialDao).findByMaterialCode(testMaterial.getName());
// Act
boolean actual = materialService.isValidForUpdate(testMaterial);
// Assert
assertFalse(actual);
}
@Test
public void isValidForUpdateShouldNull() {
// Arrange
doReturn(testMaterial).when(materialDao).findByMaterialCode(testMaterial.getName());
// Act
boolean actual = materialService.isValidForUpdate(null);
// Assert
assertFalse(actual);
}
@Test
public void getAllBySearchStringTest() {
// Arrange
doReturn(testMaterialsList).when(materialDao).findAllByMaterialCodeContainingIgnoreCase(testMaterial.getName());
// Act
List<Material> actual = materialService.getAllBySearchString(testMaterial.getName());
// Assert
assertTrue(actual.contains(testMaterial));
}
@Test
public void getAllBySearchStringTestShouldBeEmpty() {
// Arrange
doReturn(new ArrayList<>()).when(materialDao).findAllByMaterialCodeContainingIgnoreCase(testMaterial.getName());
// Act
List<Material> actual = materialService.getAllBySearchString(testMaterial.getName());
// Assert
assertTrue(actual.isEmpty());
}
@Test
public void getAllBySearchStringTestMixType() {
// Arrange
Material mixTypeMaterial = new Material();
mixTypeMaterial.setMaterialID(2);
mixTypeMaterial.setName("Mix Type");
mixTypeMaterial.setMixType(true);
testMaterialsList.add(mixTypeMaterial);
doReturn(testMaterialsList).when(materialDao).findAllByMaterialCodeContainingIgnoreCase(mixTypeMaterial.getName());
// Act
List<Material> actual = materialService.getAllBySearchString(mixTypeMaterial.getName());
// Assert
assertFalse(actual.contains(mixTypeMaterial));
}
}

View File

@ -1,238 +0,0 @@
package dev.fyloz.trial.colorrecipesexplorer.core.services;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Material;
import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialTypeService;
import dev.fyloz.trial.colorrecipesexplorer.dao.MaterialTypeDao;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
public class MaterialTypeServiceTests {
@Mock
private MaterialService materialService;
@Mock
private MaterialTypeDao materialTypeDao;
@Spy
@InjectMocks
private MaterialTypeService materialTypeService;
private MaterialType testMaterialType;
private Material testMaterial;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
testMaterialType = new MaterialType();
testMaterialType.setMaterialTypeID(0);
testMaterialType.setMaterialTypeName("Test Type");
testMaterial = new Material();
testMaterial.setMaterialID(0);
testMaterial.setName("Test Material");
}
@Test
public void isLinkedToMaterialsTestShouldBeTrue() {
// Arrange
List<Material> testMaterialList = new ArrayList<>();
testMaterialList.add(testMaterial);
doReturn(testMaterialList).when(materialService).getAllByMaterialType(testMaterialType);
// Act
boolean actual = materialTypeService.isLinkedToMaterials(testMaterialType);
// Assert
assertTrue(actual);
}
@Test
public void isLinkedToMaterialsTestShouldBeFalse() {
doReturn(new ArrayList<>()).when(materialService).getAllByMaterialType(testMaterialType);
boolean actual = materialTypeService.isLinkedToMaterials(testMaterialType);
assertFalse(actual);
}
@Test
public void isLinkedToMaterialsTestNull() {
doReturn(new ArrayList<>()).when(materialService).getAllByMaterialType(any());
boolean actual = materialTypeService.isLinkedToMaterials(testMaterialType);
assertFalse(actual);
}
@Test
public void deleteIfNotLinkedTestShouldBeTrue() {
doReturn(false).when(materialTypeService).isLinkedToMaterials(testMaterialType);
doReturn(true).when(materialTypeService).delete(testMaterialType);
boolean actual = materialTypeService.deleteIfNotLinked(testMaterialType);
assertTrue(actual);
}
@Test
public void deleteIfNotLinkedTestShouldBeFalse() {
doReturn(true).when(materialTypeService).isLinkedToMaterials(testMaterialType);
doReturn(false).when(materialTypeService).delete(testMaterialType);
boolean actual = materialTypeService.deleteIfNotLinked(testMaterialType);
assertFalse(actual);
}
@Test
public void deleteIfNotLinkedTestIsLinked() {
doReturn(true).when(materialTypeService).isLinkedToMaterials(testMaterialType);
doReturn(true).when(materialTypeService).delete(testMaterialType);
boolean actual = materialTypeService.deleteIfNotLinked(testMaterialType);
assertFalse(actual);
}
@Test
public void deleteIfNotLinkedTestNull() {
doReturn(true).when(materialTypeService).isLinkedToMaterials(testMaterialType);
doReturn(false).when(materialTypeService).delete(testMaterialType);
boolean actual = materialTypeService.deleteIfNotLinked(null);
assertFalse(actual);
}
@Test
public void isValidForCreationTestShouldBeTrue() {
doReturn(false).when(materialTypeService).existsByName(testMaterialType.getName());
boolean actual = materialTypeService.isValidForCreation(testMaterialType);
assertTrue(actual);
}
@Test
public void isValidForCreationTestShouldBeFalse() {
doReturn(true).when(materialTypeService).existsByName(testMaterialType.getName());
boolean actual = materialTypeService.isValidForCreation(testMaterialType);
assertFalse(actual);
}
@Test
public void isValidForCreationTestNull() {
doReturn(false).when(materialTypeService).existsByName(testMaterialType.getName());
boolean actual = materialTypeService.isValidForCreation(null);
assertFalse(actual);
}
@Test
public void isValidForUpdateTestShouldBeTrue() {
doReturn(true).when(materialTypeService).exists(testMaterialType);
doReturn(true).when(materialTypeService).isValidForUpdateName(testMaterialType);
doReturn(true).when(materialTypeService).isValidForUpdatePrefix(testMaterialType);
boolean actual = materialTypeService.isValidForUpdate(testMaterialType);
assertTrue(actual);
}
@Test
public void isValidForUpdateTestShouldBeFalse() {
doReturn(false).when(materialTypeService).exists(testMaterialType);
doReturn(false).when(materialTypeService).isValidForUpdateName(testMaterialType);
doReturn(false).when(materialTypeService).isValidForUpdatePrefix(testMaterialType);
boolean actual = materialTypeService.isValidForUpdate(testMaterialType);
assertFalse(actual);
}
@Test
public void isValidForUpdateTestNull() {
doReturn(false).when(materialTypeService).exists(any());
doReturn(false).when(materialTypeService).isValidForUpdateName(any());
doReturn(false).when(materialTypeService).isValidForUpdatePrefix(any());
boolean actual = materialTypeService.isValidForUpdate(null);
assertFalse(actual);
}
@Test
public void getByNameTest() {
doReturn(Optional.of(testMaterialType)).when(materialTypeDao).findByMaterialTypeName(testMaterialType.getName());
Optional<MaterialType> actual = materialTypeService.getByName(testMaterialType.getName());
assertTrue(actual.isPresent());
assertEquals(testMaterialType.getName(), actual.get().getName());
}
@Test
public void getByNameTestShouldBeEmpty() {
doReturn(Optional.empty()).when(materialTypeDao).findByMaterialTypeName(testMaterialType.getName());
Optional<MaterialType> actual = materialTypeService.getByName(testMaterialType.getName());
assertFalse(actual.isPresent());
}
@Test
public void getByNameTestNull() {
doReturn(Optional.empty()).when(materialTypeDao).findByMaterialTypeName(testMaterialType.getName());
Optional<MaterialType> actual = materialTypeService.getByName(null);
assertFalse(actual.isPresent());
}
@Test
public void getDefaultMaterialTypeTest() {
doReturn(Optional.of(testMaterialType)).when(materialTypeService).getByName(MaterialType.DEFAULT_MATERIAL_TYPE_NAME);
Optional<MaterialType> actual = materialTypeService.getDefaultMaterialType();
assertTrue(actual.isPresent());
}
// Test non-fonctionnel
// @Test
// public void getDefaultMaterialTypeGetByNameFail() {
// doReturn(Optional.empty()).when(materialTypeService).getByName(any());
//
// Optional<MaterialType> actual = materialTypeService.getDefaultMaterialType();
// assertTrue(actual.isPresent());
// }
@Test
public void existsByNameTestShouldBeTrue() {
doReturn(Optional.of(testMaterialType)).when(materialTypeService).getByName(testMaterialType.getName());
boolean actual = materialTypeService.existsByName(testMaterialType.getName());
assertTrue(actual);
}
@Test
public void existsByNameTestShouldBeFalse() {
doReturn(Optional.empty()).when(materialTypeService).getByName(testMaterialType.getName());
boolean actual = materialTypeService.existsByName(testMaterialType.getName());
assertFalse(actual);
}
@Test
public void existsByNameTestNull() {
doReturn(Optional.empty()).when(materialTypeService).getByName(any());
boolean actual = materialTypeService.existsByName(null);
assertFalse(actual);
}
}

View File

@ -1,61 +0,0 @@
package dev.fyloz.trial.colorrecipesexplorer.core.services;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixQuantityService;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Material;
import dev.fyloz.trial.colorrecipesexplorer.dao.MixQuantityDao;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
public class MixQuantityServiceTests {
@Mock
private MixQuantityDao mixQuantityDao;
@Spy
@InjectMocks
private MixQuantityService mixQuantityService;
private Material testMaterial;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
testMaterial = new Material();
testMaterial.setMaterialID(0);
testMaterial.setName("Test Material");
}
@Test
public void existsByMaterialTestShouldBeTrue() {
doReturn(true).when(mixQuantityDao).existsByMaterial(testMaterial);
boolean actual = mixQuantityService.existsByMaterial(testMaterial);
assertTrue(actual);
}
@Test
public void existsByMaterialTestShouldBeFalse() {
doReturn(false).when(mixQuantityDao).existsByMaterial(testMaterial);
boolean actual = mixQuantityService.existsByMaterial(testMaterial);
assertFalse(actual);
}
@Test
public void existsByMaterialTestNull() {
doReturn(false).when(mixQuantityDao).existsByMaterial(testMaterial);
boolean actual = mixQuantityService.existsByMaterial(null);
assertFalse(actual);
}
}

View File

@ -1,81 +0,0 @@
package dev.fyloz.trial.colorrecipesexplorer.core.services;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Mix;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Recipe;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixQuantityService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixService;
import org.junit.Before;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import static org.mockito.ArgumentMatchers.any;
public class MixServiceTests {
@Mock
private MaterialService materialService;
@Mock
private MixQuantityService mixQuantityService;
@Spy
@InjectMocks
private MixService mixService;
private Mix testMix;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
Recipe recipe = new Recipe();
recipe.setRecipeId(0);
recipe.setName("Test Recipe");
testMix = new Mix();
testMix.setMixId(0);
testMix.setRecipe(recipe);
}
// @Test
// public void createTest() {
// List<Integer> materials = new ArrayList<>();
// materials.add(0);
// List<Float> quantities = new ArrayList<>();
// List<MixQuantity> mquantities = new ArrayList<>();
// mquantities.add(new MixQuantity());
// quantities.add(1000f);
// MixType type = new MixType();
// type.setName("Test Type");
//
// doReturn(Optional.of(testMix)).when(mixService).save(any());
// doReturn(mquantities).when(mixService).createMixQuantities(any(), any(), any());
// ModelResponseBuilder fakeResponseBuilder = mock(ModelResponseBuilder.class);
// verify(fakeResponseBuilder, times(0)).addResponseCode(any());
//
// mixService.create(new ModelResponseBuilder(""), materials, quantities, testMix.getRecipe(), type);
// }
// @Test
// public void createTestMaterialNotFound() {
// List<Integer> materials = new ArrayList<>();
// materials.add(0);
// List<Float> quantities = new ArrayList<>();
// List<MixQuantity> mquantities = new ArrayList<>();
// mquantities.add(new MixQuantity());
// quantities.add(1000f);
// MixType type = new MixType();
// type.setName("Test Type");
//
// doReturn(Optional.of(testMix)).when(mixService).save(any());
// doReturn(mquantities).when(mixService).createMixQuantities(any(), any(), any());
// ModelResponseBuilder fakeResponseBuilder = mock(ModelResponseBuilder.class);
// verify(fakeResponseBuilder, times(0)).addResponseCode(any());
//
// mixService.create(new ModelResponseBuilder(""), materials, quantities, testMix.getRecipe(), type);
// }
}

View File

@ -1,146 +0,0 @@
package dev.fyloz.trial.colorrecipesexplorer.core.services;
import dev.fyloz.trial.colorrecipesexplorer.core.model.MixType;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Material;
import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialTypeService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixTypeService;
import dev.fyloz.trial.colorrecipesexplorer.dao.MixTypeDao;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import java.util.Optional;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
public class MixTypeServiceTests {
@Mock
private MaterialTypeService materialTypeService;
@Mock
private MixTypeDao mixTypeDao;
@Spy
@InjectMocks
private MixTypeService mixTypeService;
private MixType testMixType;
private MaterialType testMaterialType;
private Material testMaterial;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
testMaterial = new Material();
testMaterial.setMaterialID(0);
testMaterial.setName("Test Material");
testMixType = new MixType();
testMixType.setTypeID(0);
testMixType.setName("Test Type");
testMixType.setMaterial(testMaterial);
testMaterialType = new MaterialType();
testMaterialType.setMaterialTypeID(0);
testMaterialType.setMaterialTypeName("Test Type");
}
@Test
public void getByNameTest() {
doReturn(Optional.of(testMixType)).when(mixTypeDao).findByTypeName(testMixType.getName());
Optional<MixType> actual = mixTypeService.getByName(testMixType.getName());
assertTrue(actual.isPresent());
}
@Test
public void getByNameTestShouldBeEmpty() {
doReturn(Optional.empty()).when(mixTypeDao).findByTypeName(testMixType.getName());
Optional<MixType> actual = mixTypeService.getByName(testMixType.getName());
assertFalse(actual.isPresent());
}
@Test
public void getByNameTestNull() {
doReturn(Optional.empty()).when(mixTypeDao).findByTypeName(any());
Optional<MixType> actual = mixTypeService.getByName(null);
assertFalse(actual.isPresent());
}
@Test
public void getByMaterialTest() {
doReturn(Optional.of(testMixType)).when(mixTypeDao).findByMaterial(testMaterial);
Optional<MixType> actual = mixTypeService.getByMaterial(testMaterial);
assertTrue(actual.isPresent());
}
@Test
public void getByMaterialTestShouldBeEmpty() {
doReturn(Optional.empty()).when(mixTypeDao).findByMaterial(testMaterial);
Optional<MixType> actual = mixTypeService.getByMaterial(testMaterial);
assertFalse(actual.isPresent());
}
@Test
public void getByMaterialTestNull() {
doReturn(Optional.empty()).when(mixTypeDao).findByMaterial(any());
Optional<MixType> actual = mixTypeService.getByMaterial(null);
assertFalse(actual.isPresent());
}
@Test
public void createByNameTest() {
doReturn(Optional.of(testMaterialType)).when(materialTypeService).getDefaultMaterialType();
doReturn(Optional.of(testMixType)).when(mixTypeService).save(any());
doReturn(Optional.empty()).when(mixTypeService).getByName(testMixType.getName());
Optional<MixType> actual = mixTypeService.createByName(testMixType.getName());
assertTrue(actual.isPresent());
}
@Test
public void createByNameTestExistsByName() {
doReturn(Optional.of(testMixType)).when(materialTypeService).getDefaultMaterialType();
doReturn(Optional.of(testMixType)).when(mixTypeService).save(any());
doReturn(Optional.of(testMixType)).when(mixTypeService).getByName(testMixType.getName());
Optional<MixType> actual = mixTypeService.createByName(testMixType.getName());
verify(mixTypeService, times(0)).save(any());
assertTrue(actual.isPresent());
}
@Test
public void createByNameTestShouldBeEmpty() {
doReturn(Optional.empty()).when(materialTypeService).getDefaultMaterialType();
doReturn(Optional.empty()).when(mixTypeService).save(any());
doReturn(Optional.empty()).when(mixTypeService).getByName(testMixType.getName());
Optional<MixType> actual = mixTypeService.createByName(testMixType.getName());
assertFalse(actual.isPresent());
}
@Test
public void createByNameTestNull() {
doReturn(Optional.empty()).when(materialTypeService).getDefaultMaterialType();
doReturn(Optional.empty()).when(mixTypeService).save(any());
doReturn(Optional.empty()).when(mixTypeService).getByName(any());
Optional<MixType> actual = mixTypeService.createByName(null);
assertFalse(actual.isPresent());
}
}

View File

@ -1,223 +0,0 @@
package dev.fyloz.trial.colorrecipesexplorer.core.services;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Mix;
import dev.fyloz.trial.colorrecipesexplorer.core.model.RecipeStep;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Company;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Recipe;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.CompanyService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.RecipeService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.StepService;
import dev.fyloz.trial.colorrecipesexplorer.dao.RecipeDao;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.*;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
public class RecipeServiceTests {
@Mock
private CompanyService companyService;
@Mock
private StepService stepService;
@Mock
private MixService mixService;
@Mock
private RecipeDao recipeDao;
@Spy
@InjectMocks
private RecipeService recipeService;
@Mock
private Recipe testRecipe;
private Company testCompany;
private List<Mix> testMixesList;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
testCompany = new Company();
testCompany.setCompanyID(0);
testCompany.setCompanyName("Test Company");
testRecipe = new Recipe();
testRecipe.setRecipeID(Integer.MAX_VALUE);
testRecipe.setName("Test Recipe");
testRecipe.setCompany(testCompany);
Mix mix1 = new Mix();
mix1.setMixID(5);
Mix mix2 = new Mix();
mix1.setMixID(1);
testMixesList = new ArrayList<>();
testMixesList.add(mix1);
testMixesList.add(mix2);
}
@Test
public void getByCompanyTest() {
List<Recipe> testRecipesList = new ArrayList<>();
testRecipesList.add(testRecipe);
doReturn(testRecipesList).when(recipeDao).findAllByCompany(testCompany);
List<Recipe> actual = recipeService.getByCompany(testCompany);
assertFalse(actual.isEmpty());
}
@Test
public void getByCompanyTestShouldBeEmpty() {
doReturn(new ArrayList<>()).when(recipeDao).findAllByCompany(testCompany);
List<Recipe> actual = recipeService.getByCompany(testCompany);
assertTrue(actual.isEmpty());
}
@Test
public void getByCompanyTestNull() {
doReturn(new ArrayList<>()).when(recipeDao).findAllByCompany(any());
List<Recipe> actual = recipeService.getByCompany(null);
assertTrue(actual.isEmpty());
}
@Test
public void deleteRecipeTestShouldBeTrue() {
doReturn(true).when(recipeService).delete(testRecipe);
doReturn(new ArrayList<>()).when(recipeService).getImageFiles(testRecipe);
boolean actual = recipeService.deleteRecipe(testRecipe);
assertTrue(actual);
}
@Test
public void deleteRecipeTestShouldBeFalse() {
doReturn(false).when(recipeService).delete(testRecipe);
boolean actual = recipeService.deleteRecipe(testRecipe);
assertFalse(actual);
}
@Test
public void deleteRecipeTestNull() {
doReturn(false).when(recipeService).delete(testRecipe);
boolean actual = recipeService.deleteRecipe(null);
assertFalse(actual);
}
// @Test
// public void getSortedMixesTest() {
//// doReturn(testMixesList).when(testRecipe).getMixes();
// testRecipe.setRecipeMixes(testMixesList);
//
// List<Mix> actual = recipeService.getSortedMixes(testRecipe);
// assertFalse(actual.isEmpty());
// assertEquals(testMixesList.get(1).getId(), actual.get(0).getId());
// assertEquals(testMixesList.get(0).getId(), actual.get(1).getId());
// }
//
// @Test
// public void getSortedMixesTestShouldBeEmpty() {
// testRecipe.setRecipeMixes(testMixesList);
//
// List<Mix> actual = recipeService.getSortedMixes(testRecipe);
// assertTrue(actual.isEmpty());
// }
//
// @Test
// public void getSortedMixesTestNull() {
// doReturn(new ArrayList<>()).when(testRecipe).getMixes();
//
// List<Mix> actual = recipeService.getSortedMixes(null);
// assertTrue(actual.isEmpty());
// }
@Test
public void convertAndCreateStepsTest() {
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
List<String> testSteps = new LinkedList<>();
testSteps.add("step 1");
testSteps.add("step 2");
testSteps.add("step 3");
multiValueMap.add("step", testSteps);
doReturn(true).when(stepService).deleteAll(any());
doReturn(Optional.of(testRecipe)).when(recipeService).save(any());
Optional<Recipe> actual = recipeService.convertAndCreateSteps(testRecipe, multiValueMap);
assertTrue(actual.isPresent());
}
@Test
public void convertAndCreateStepsTestWithPreviousSteps() {
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
List<String> testSteps = new LinkedList<>();
testSteps.add("step 1");
testSteps.add("step 2");
testSteps.add("step 3");
multiValueMap.add("step", testSteps);
List<RecipeStep> previousSteps = new ArrayList<>();
previousSteps.add(new RecipeStep(testRecipe, "previous 1"));
previousSteps.add(new RecipeStep(testRecipe, "previous 2"));
testRecipe.setRecipeSteps(previousSteps);
doReturn(true).when(stepService).deleteAll(any());
doReturn(Optional.of(testRecipe)).when(recipeService).save(any());
Optional<Recipe> actual = recipeService.convertAndCreateSteps(testRecipe, multiValueMap);
assertTrue(actual.isPresent());
}
@Test
public void convertAndCreateStepsTestShouldBeEmpty() {
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
List<String> testSteps = new LinkedList<>();
testSteps.add("step 1");
testSteps.add("step 2");
testSteps.add("step 3");
multiValueMap.add("step", testSteps);
List<RecipeStep> previousSteps = new ArrayList<>();
previousSteps.add(new RecipeStep(testRecipe, "previous 1"));
previousSteps.add(new RecipeStep(testRecipe, "previous 2"));
testRecipe.setRecipeSteps(previousSteps);
doReturn(false).when(stepService).deleteAll(any());
doReturn(Optional.empty()).when(recipeService).save(any());
Optional<Recipe> actual = recipeService.convertAndCreateSteps(testRecipe, multiValueMap);
assertFalse(actual.isPresent());
}
@Test
public void convertAndCreateStepsTestNull() {
doReturn(false).when(stepService).deleteAll(any());
doReturn(Optional.empty()).when(recipeService).save(any());
Optional<Recipe> actual = recipeService.convertAndCreateSteps(null, null);
assertFalse(actual.isPresent());
}
}

View File

@ -0,0 +1,248 @@
package dev.fyloz.trial.colorrecipesexplorer.services;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.NullIdentifierException;
import dev.fyloz.trial.colorrecipesexplorer.core.model.IModel;
import dev.fyloz.trial.colorrecipesexplorer.core.services.IGenericService;
import org.junit.jupiter.api.Test;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;
public abstract class AbstractServiceTest<E extends IModel, S extends IGenericService<E>, R extends JpaRepository<E, Long>> {
private S service;
private R repository;
public void init(S service, R repository) {
this.service = service;
this.repository = repository;
E entity = getEntity();
when(repository.existsById(anyLong())).thenReturn(false);
when(repository.existsById(entity.getId())).thenReturn(true);
when(repository.findById(anyLong())).thenReturn(Optional.empty());
when(repository.findById(entity.getId())).thenReturn(Optional.of(entity));
when(repository.save(any())).thenAnswer(i -> i.getArgument(0));
}
@Test
public void whenExistsById_thenReturnTrue() {
// given
E entity = getEntity();
// when
boolean found = service.existsById(entity.getId());
// then
assertTrue(found);
}
@Test
public void whenExistsById_thenReturnFalse() {
// given
E entity = getEntityDifferentId();
// when
boolean found = service.existsById(entity.getId());
// then
assertFalse(found);
}
@Test
public void whenGetById_thenReturnEntity() {
// given
E entity = getEntity();
// when
E found = service.getById(entity.getId());
// then
assertEquals(entity, found);
}
@Test
public void whenGetById_thenThrowEntityNotFoundException() {
// given
E entity = getEntityDifferentId();
// when
EntityNotFoundException thrown = assertThrows(EntityNotFoundException.class, () -> service.getById(entity.getId()));
assertEquals(thrown.getIdentifierType(), ModelException.IdentifierType.ID);
}
@Test
public void whenGetAll_thenReturnEntityList() {
// given
E entity = getEntity();
E anotherEntity = getEntityDifferentId();
List<E> entities = Arrays.asList(entity, anotherEntity);
when(repository.findAll()).thenReturn(entities);
// when
List<E> found = service.getAll();
// then
assertFalse(found.isEmpty());
assertTrue(found.containsAll(entities));
}
@Test
public void whenGetAll_thenReturnEmptyList() {
// given
when(repository.findAll()).thenReturn(new ArrayList<>());
// when
List<E> found = service.getAll();
// then
assertTrue(found.isEmpty());
}
@Test
public void whenSave_thenReturnEntity() {
// given
E entity = getEntityDifferentId();
// when
E found = service.save(entity);
// then
assertEquals(entity, found);
}
@Test
public void whenSave_thenThrowEntityAlreadyExistsException() {
// given
E entity = getEntity();
// then
EntityAlreadyExistsException thrown = assertThrows(EntityAlreadyExistsException.class, () -> service.save(entity));
assertEquals(thrown.getIdentifierType(), ModelException.IdentifierType.ID);
}
@Test
public void whenSaveAll_thenReturnEntityCollection() {
// given
E entity = getEntity();
E anotherEntity = getEntityDifferentId();
List<E> entities = Arrays.asList(entity, anotherEntity);
doAnswer(i -> i.getArgument(0)).when(service).saveAll(any());
// when
Collection<E> found = service.saveAll(entities);
// then
assertFalse(found.isEmpty());
assertTrue(found.containsAll(entities));
}
@Test
public void whenSaveAll_thenReturnEmptyCollection() {
// given
List<E> entities = new ArrayList<>();
doAnswer(i -> i.getArgument(0)).when(service).saveAll(any());
// when
Collection<E> found = service.saveAll(entities);
// then
assertTrue(found.isEmpty());
}
@Test
public void whenUpdate_thenReturnEntity() {
// given
E entity = getEntity();
// when
E found = service.update(entity);
// then
assertEquals(entity, found);
}
@Test
public void whenUpdate_thenThrowNullIdentifierException() {
// given
E entity = getEntityNullId();
// then
assertThrows(NullIdentifierException.class, () -> service.update(entity));
}
@Test
public void whenUpdate_thenThrowEntityNotFoundException() {
// given
E entity = getEntityDifferentId();
// when
EntityNotFoundException thrown = assertThrows(EntityNotFoundException.class, () -> service.update(entity));
// then
assertEquals(thrown.getIdentifierType(), ModelException.IdentifierType.ID);
}
@Test
public void whenDelete_thenDaoIsCalled() {
// given
E entity = getEntity();
// when
service.delete(entity);
// then
verify(repository).delete(entity);
}
@Test
public void whenDeleteById_thenDeleteIsCalled() {
// given
E entity = getEntity();
// when
service.deleteById(entity.getId());
// then
verify(service).delete(entity);
}
@Test
public void whenDeleteAll_thenDaoIsCalled() {
// given
E entity = getEntity();
E anotherEntity = getEntityDifferentId();
Collection<E> entities = Arrays.asList(entity, anotherEntity);
// when
service.deleteAll(entities);
// then
verify(repository).deleteAll(entities);
}
protected abstract E getEntity();
protected abstract E getEntityDifferentId();
protected abstract E getEntityNullId();
}

View File

@ -0,0 +1,165 @@
package dev.fyloz.trial.colorrecipesexplorer.services;
import dev.fyloz.trial.colorrecipesexplorer.builders.CompanyBuilder;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityLinkedException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Company;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.CompanyService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.RecipeService;
import dev.fyloz.trial.colorrecipesexplorer.dao.CompanyDao;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class CompanyServiceTest extends AbstractServiceTest<Company, CompanyService, CompanyDao> {
@MockBean
private CompanyDao companyDao;
@Autowired
@SpyBean
private CompanyService companyService;
@MockBean
private RecipeService recipeService;
@BeforeEach
public void setUp() {
companyService.setCompanyDao(companyDao);
companyService.setRecipeService(recipeService);
super.init(companyService, companyDao);
Company company = getEntityDifferentName();
when(companyDao.existsByName(anyString())).thenReturn(false);
when(companyDao.existsByName(company.getName())).thenReturn(true);
when(recipeService.existsByCompany(any())).thenReturn(false);
when(recipeService.existsByCompany(company)).thenReturn(true);
}
@Test
public void whenExistsByName_thenReturnTrue() {
// given
Company company = getEntityDifferentName();
// when
boolean found = companyService.existsByName(company.getName());
// then
assertTrue(found);
}
@Test
public void whenExistsByName_thenReturnFalse() {
// given
Company company = getEntity();
// when
boolean found = companyService.existsByName(company.getName());
// then
assertFalse(found);
}
@Test
public void whenIsLinkedToRecipes_thenReturnTrue() {
// given
Company company = getEntityDifferentName();
// when
boolean found = companyService.isLinkedToRecipes(company);
// then
assertTrue(found);
}
@Test
public void whenIsLinkedToRecipes_thenReturnFalse() {
// given
Company company = getEntity();
// when
boolean found = companyService.isLinkedToRecipes(company);
// then
assertFalse(found);
}
@Test
@Override
public void whenSave_thenReturnEntity() {
// given
Company company = getEntityDifferentId();
// when
Company found = companyService.save(company);
// then
assertEquals(company, found);
}
@Test
@Override
public void whenSave_thenThrowEntityAlreadyExistsException() {
// given
Company company = getEntityDifferentName();
// when
EntityAlreadyExistsException thrown = assertThrows(EntityAlreadyExistsException.class, () -> companyService.save(company));
// then
assertEquals(thrown.getIdentifierType(), ModelException.IdentifierType.NAME);
super.whenSave_thenThrowEntityAlreadyExistsException();
}
@Test
public void whenDelete_thenThrowEntityLinkedException() {
// given
Company company = getEntityDifferentName();
// then
assertThrows(EntityLinkedException.class, () -> companyService.delete(company));
}
@Override
protected Company getEntity() {
return new CompanyBuilder().build();
}
@Override
protected Company getEntityDifferentId() {
return new CompanyBuilder()
.id(1L)
.build();
}
@Override
protected Company getEntityNullId() {
return new CompanyBuilder()
.id(null)
.build();
}
private Company getEntityDifferentName() {
return new CompanyBuilder()
.name("Design")
.build();
}
}

View File

@ -0,0 +1,296 @@
package dev.fyloz.trial.colorrecipesexplorer.services;
import dev.fyloz.trial.colorrecipesexplorer.builders.MaterialBuilder;
import dev.fyloz.trial.colorrecipesexplorer.builders.MaterialTypeBuilder;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.core.model.Material;
import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType;
import dev.fyloz.trial.colorrecipesexplorer.core.services.files.SimdutService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixQuantityService;
import dev.fyloz.trial.colorrecipesexplorer.dao.MaterialDao;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class MaterialServiceTest extends AbstractServiceTest<Material, MaterialService, MaterialDao> {
@MockBean
private MaterialDao materialDao;
@Autowired
@SpyBean
private MaterialService materialService;
@MockBean
private MixQuantityService mixQuantityService;
@MockBean
private SimdutService simdutService;
@BeforeEach
void setUp() {
materialService.setMaterialDao(materialDao);
materialService.setMixQuantityService(mixQuantityService);
materialService.setSimdutService(simdutService);
super.init(materialService, materialDao);
Material material = getEntityDifferentName();
MaterialType materialType = getMaterialType();
when(materialDao.existsByName(anyString())).thenReturn(false);
when(materialDao.existsByName(material.getName())).thenReturn(true);
when(materialDao.findByName(anyString())).thenReturn(Optional.empty());
when(materialDao.findByName(material.getName())).thenReturn(Optional.of(material));
when(materialDao.existsByMaterialType(any())).thenReturn(false);
when(materialDao.existsByMaterialType(materialType)).thenReturn(true);
when(mixQuantityService.existsByMaterial(any())).thenReturn(false);
when(mixQuantityService.existsByMaterial(material)).thenReturn(true);
}
@Test
public void whenExistsByName_thenReturnTrue() {
// given
Material material = getEntityDifferentName();
// when
boolean found = materialService.existsByName(material.getName());
// then
assertTrue(found);
}
@Test
public void whenExistsByName_thenReturnFalse() {
// given
Material material = getEntity();
// when
boolean found = materialService.existsByName(material.getName());
// then
assertFalse(found);
}
@Test
public void whenExistsByMaterialType_thenReturnTrue() {
// given
MaterialType materialType = getMaterialType();
// when
boolean found = materialService.existsByMaterialType(materialType);
// then
assertTrue(found);
}
@Test
public void whenExistsByMaterialType_thenReturnFalse() {
// given
MaterialType materialType = new MaterialTypeBuilder().name("Teinture").build();
// when
boolean found = materialService.existsByMaterialType(materialType);
// then
assertFalse(found);
}
@Test
public void whenIsLinkedToMixes_thenReturnTrue() {
// given
Material material = getEntityDifferentName();
// when
boolean found = materialService.isLinkedToMixes(material);
// then
assertTrue(found);
}
@Test
public void whenIsLinkedToMixes_thenReturnFalse() {
// given
Material material = getEntity();
// when
boolean found = materialService.isLinkedToMixes(material);
// then
assertFalse(found);
}
@Test
public void whenGetAllNotMixType_thenReturnMaterialList() {
// given
Material material = getEntity();
Material anotherMaterial = getEntityMixType();
List<Material> materials = Arrays.asList(material, anotherMaterial);
doReturn(materials).when(materialService).getAll();
// when
List<Material> found = materialService.getAllNotMixType();
// then
assertFalse(found.isEmpty());
assertTrue(found.contains(material));
assertFalse(found.contains(anotherMaterial));
}
@Test
public void whenGetByName_thenReturnMaterial() {
// given
Material material = getEntityDifferentName();
// when
Material found = materialService.getByName(material.getName());
// then
assertEquals(material, found);
}
@Test
public void whenGetByName_thenThrowEntityNotFoundExceptionName() {
// given
Material material = getEntity();
// then
EntityNotFoundException thrown = assertThrows(EntityNotFoundException.class, () -> materialService.getByName(material.getName()));
assertEquals(thrown.getIdentifierType(), ModelException.IdentifierType.NAME);
}
@Override
@Test
public void whenSave_thenReturnEntity() {
// given
Material material = getEntityDifferentId();
MockMultipartFile simdut = new MockMultipartFile("simdut", "simdut.pdf", MediaType.APPLICATION_PDF_VALUE, "pdf content".getBytes());
// when
Material found = materialService.save(material, simdut);
//then
verify(simdutService).write(material, simdut);
assertEquals(material, found);
}
@Test
public void whenUpdate_thenReturnMaterial() {
// given
Material material = getEntityDifferentName();
// when
Material found = materialService.update(material);
// then
assertEquals(material, found);
}
@Test
public void whenUpdate_thenThrowEntityAlreadyExistsExceptionName() {
// given
Material material = getEntityDifferentName();
Material anotherMaterial = getEntityDifferentNameAndId();
when(materialDao.findByName(anotherMaterial.getName())).thenReturn(Optional.of(anotherMaterial));
// then
EntityAlreadyExistsException thrown = assertThrows(EntityAlreadyExistsException.class, () -> materialService.update(material));
assertEquals(thrown.getIdentifierType(), ModelException.IdentifierType.NAME);
}
@Test
public void whenUpdate_thenSimdutServiceIsCalled() {
// given
Material material = getEntityDifferentName();
MockMultipartFile simdut = new MockMultipartFile("simdut", "simdut.pdf", MediaType.APPLICATION_PDF_VALUE, "pdf content".getBytes());
// when
Material found = materialService.update(material, simdut);
// then
assertEquals(material, found);
verify(simdutService).update(simdut, found);
}
@Test
public void whenDelete_thenSimdutServiceIsCalled() {
// given
Material material = getEntityDifferentName();
// when
materialService.delete(material);
// then
verify(simdutService).delete(material);
}
@Override
protected Material getEntity() {
return new MaterialBuilder().build();
}
@Override
protected Material getEntityDifferentId() {
return new MaterialBuilder()
.id(1L)
.build();
}
@Override
protected Material getEntityNullId() {
return new MaterialBuilder()
.id(null)
.build();
}
private Material getEntityDifferentName() {
return new MaterialBuilder()
.name("Dirt")
.build();
}
private Material getEntityDifferentNameAndId() {
return new MaterialBuilder()
.id(1L)
.name("Dirt")
.build();
}
private Material getEntityMixType() {
return new MaterialBuilder()
.mixType(true)
.build();
}
private MaterialType getMaterialType() {
return new MaterialTypeBuilder().build();
}
}

View File

@ -0,0 +1,355 @@
package dev.fyloz.trial.colorrecipesexplorer.services;
import dev.fyloz.trial.colorrecipesexplorer.builders.MaterialTypeBuilder;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.CannotDeleteDefaultMaterialTypeException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.CannotEditDefaultMaterialTypeException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType;
import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MaterialTypeEditorDto;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialTypeService;
import dev.fyloz.trial.colorrecipesexplorer.dao.MaterialTypeDao;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class MaterialTypeServiceTest extends AbstractServiceTest<MaterialType, MaterialTypeService, MaterialTypeDao> {
@MockBean
private MaterialTypeDao materialTypeDao;
@Autowired
@SpyBean
private MaterialTypeService materialTypeService;
@MockBean
private MaterialService materialService;
@Spy
private List<MaterialType> defaultMaterialTypes = new LinkedList<>();
@BeforeEach
void setUp() {
materialTypeService.setMaterialTypeDao(materialTypeDao);
materialTypeService.setMaterialService(materialService);
materialTypeService.setDefaultMaterialTypes(defaultMaterialTypes);
super.init(materialTypeService, materialTypeDao);
MaterialType materialType = getEntity();
MaterialType nameMaterialType = getEntityDifferentName();
MaterialType prefixMaterialType = getEntityDifferentPrefix();
doReturn(false).when(defaultMaterialTypes).contains(Mockito.<MaterialType>any());
doReturn(true).when(defaultMaterialTypes).contains(materialType);
when(materialTypeDao.existsByName(anyString())).thenReturn(false);
when(materialTypeDao.existsByName(nameMaterialType.getName())).thenReturn(true);
when(materialTypeDao.existsByPrefix(anyString())).thenReturn(false);
when(materialTypeDao.existsByPrefix(prefixMaterialType.getPrefix())).thenReturn(true);
when(materialService.existsByMaterialType(any())).thenReturn(false);
when(materialService.existsByMaterialType(materialType)).thenReturn(true);
when(materialTypeDao.findByName(anyString())).thenReturn(Optional.empty());
when(materialTypeDao.findByName(nameMaterialType.getName())).thenReturn(Optional.of(nameMaterialType));
}
@Test
public void whenAddDefault_thenListIsCalled() {
// given
MaterialType materialType = getEntity();
// when
materialTypeService.addDefault(materialType);
// then
verify(defaultMaterialTypes).add(materialType);
}
@Test
public void whenIsDefault_thenReturnTrue() {
// given
MaterialType materialType = getEntity();
// when
boolean found = materialTypeService.isDefault(materialType);
// then
assertTrue(found);
}
@Test
public void whenIsDefault_thenReturnFalse() {
// given
MaterialType materialType = getEntityDifferentName();
// when
boolean found = materialTypeService.isDefault(materialType);
// then
assertFalse(found);
}
@Test
public void whenExistsByName_thenReturnTrue() {
// given
MaterialType materialType = getEntityDifferentName();
// when
boolean found = materialTypeService.existsByName(materialType.getName());
// then
assertTrue(found);
}
@Test
public void whenExistsByName_thenReturnFalse() {
// given
MaterialType materialType = getEntity();
// when
boolean found = materialTypeService.existsByName(materialType.getName());
// then
assertFalse(found);
}
@Test
public void whenExistsByPrefix_thenReturnTrue() {
// given
MaterialType materialType = getEntityDifferentPrefix();
// when
boolean found = materialTypeService.existsByPrefix(materialType.getPrefix());
// then
assertTrue(found);
}
@Test
public void whenExistsByPrefix_thenReturnFalse() {
// given
MaterialType materialType = getEntity();
// when
boolean found = materialTypeService.existsByPrefix(materialType.getPrefix());
// then
assertFalse(found);
}
@Test
public void whenIsLinkedToMaterials_thenReturnTrue() {
// given
MaterialType materialType = getEntity();
// when
boolean found = materialTypeService.isLinkedToMaterials(materialType);
// then
assertTrue(found);
}
@Test
public void whenIsLinkedToMaterials_thenReturnFalse() {
// given
MaterialType materialType = getEntityDifferentName();
// when
boolean found = materialTypeService.isLinkedToMaterials(materialType);
// then
assertFalse(found);
}
@Test
public void whenGetAllNotDefault_thenReturnMaterialTypeList() {
// given
MaterialType defaultMaterialType = getEntity();
MaterialType materialType = getEntityDifferentName();
List<MaterialType> materialTypes = Arrays.asList(defaultMaterialType, materialType);
doReturn(materialTypes).when(materialTypeService).getAll();
// when
List<MaterialType> found = materialTypeService.getAllNotDefault();
// then
assertFalse(found.isEmpty());
assertTrue(found.contains(materialType));
assertFalse(found.contains(defaultMaterialType));
}
@Test
public void whenGetByName_thenReturnTrue() {
// given
MaterialType materialType = getEntityDifferentName();
// when
MaterialType found = materialTypeService.getByName(materialType.getName());
// then
assertEquals(materialType, found);
}
@Test
public void whenGetByName_thenThrowEntityNotFoundExceptionName() {
// given
MaterialType materialType = getEntity();
// then
EntityNotFoundException thrown = assertThrows(EntityNotFoundException.class, () -> materialTypeService.getByName(materialType.getName()));
assertEquals(thrown.getIdentifierType(), ModelException.IdentifierType.NAME);
}
@Test
public void whenUpdate_thenReturnMaterialType() {
// given
MaterialType materialType = getEntityDifferentName();
MaterialTypeEditorDto dto = new MaterialTypeEditorDto(materialType);
doReturn(false).when(materialTypeService).isDefault(materialType);
// when
MaterialType found = materialTypeService.update(dto);
// then
assertEquals(materialType, found);
}
@Test
public void whenUpdate_thenThrowCannotEditDefaultMaterialTypeException() {
// given
MaterialType materialType = getEntity();
// then
assertThrows(CannotEditDefaultMaterialTypeException.class, () -> materialTypeService.update(new MaterialTypeEditorDto(materialType)));
}
@Test
public void whenUpdate_thenThrowEntityNotFoundExceptionOldName() {
// given
MaterialType materialType = getEntityDifferentPrefix();
// then
EntityNotFoundException thrown = assertThrows(EntityNotFoundException.class, () -> materialTypeService.update(new MaterialTypeEditorDto(materialType)));
assertEquals(thrown.getIdentifierType(), ModelException.IdentifierType.NAME);
}
@Test
public void whenUpdate_thenThrowEntityAlreadyExistsExceptionName() {
// given
MaterialType materialType = getEntityDifferentName();
MaterialTypeEditorDto dto = new MaterialTypeEditorDto(materialType);
dto.setOldName("Base");
doReturn(false).when(materialTypeService).isDefault(dto.getOldMaterialType());
doReturn(true).when(materialTypeService).existsByName(dto.getOldName());
// then
EntityAlreadyExistsException thrown = assertThrows(EntityAlreadyExistsException.class, () -> materialTypeService.update(dto));
assertEquals(thrown.getIdentifierType(), ModelException.IdentifierType.NAME);
}
@Test
public void whenUpdate_thenThrowEntityAlreadyExistsExceptionPrefix() {
// given
MaterialType materialType = getEntityDifferentPrefix();
MaterialTypeEditorDto dto = new MaterialTypeEditorDto(materialType);
dto.setOldPrefix("BAS");
dto.setName("Catalyseur");
dto.setOldName("Catalyseur");
// then
EntityAlreadyExistsException thrown = assertThrows(EntityAlreadyExistsException.class, () -> materialTypeService.update(dto));
assertEquals(thrown.getIdentifierType(), ModelException.IdentifierType.OTHER);
assertEquals(thrown.getIdentifierName(), MaterialType.IDENTIFIER_PREFIX_NAME);
}
@Override
public void whenDelete_thenDaoIsCalled() {
// given
MaterialType materialType = getEntityDifferentName();
// when
materialTypeService.delete(materialType);
// then
verify(materialTypeDao).delete(materialType);
}
@Test
public void whenDelete_thenThrowCannotDeleteDefaultMaterialTypeException() {
// given
MaterialType materialType = getEntity();
// then
assertThrows(CannotDeleteDefaultMaterialTypeException.class, () -> materialTypeService.delete(materialType));
}
@Override
public void whenDeleteById_thenDeleteIsCalled() {
// given
MaterialType materialType = getEntityDifferentId();
// when
materialTypeService.deleteById(materialType.getId());
// then
verify(materialTypeService).delete(materialType);
}
@Override
protected MaterialType getEntity() {
return new MaterialTypeBuilder().build();
}
@Override
protected MaterialType getEntityDifferentId() {
return new MaterialTypeBuilder()
.id(1L)
.build();
}
@Override
protected MaterialType getEntityNullId() {
return new MaterialTypeBuilder()
.id(null)
.build();
}
private MaterialType getEntityDifferentName() {
return new MaterialTypeBuilder()
.name("Catalyseur")
.build();
}
private MaterialType getEntityDifferentPrefix() {
return new MaterialTypeBuilder()
.prefix("CAT")
.build();
}
}

View File

@ -0,0 +1,66 @@
package dev.fyloz.trial.colorrecipesexplorer.services;
import dev.fyloz.trial.colorrecipesexplorer.builders.MixTypeBuilder;
import dev.fyloz.trial.colorrecipesexplorer.core.model.MixType;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService;
import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixTypeService;
import dev.fyloz.trial.colorrecipesexplorer.dao.MixTypeDao;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
public class MixTypeServiceTest extends AbstractServiceTest<MixType, MixTypeService, MixTypeDao> {
@MockBean
private MixTypeDao mixTypeDao;
@Autowired
@SpyBean
private MixTypeService mixTypeService;
@MockBean
private MaterialService materialService;
@BeforeEach
void setUp() {
mixTypeService.setMixTypeDao(mixTypeDao);
mixTypeService.setMaterialService(materialService);
super.init(mixTypeService, mixTypeDao);
}
@Test
public void whenExistsByName_thenReturnTrue() {
// given
// when
// then
}
@Override
protected MixType getEntity() {
return new MixTypeBuilder().build();
}
@Override
protected MixType getEntityDifferentId() {
return new MixTypeBuilder()
.id(1L)
.build();
}
@Override
protected MixType getEntityNullId() {
return new MixTypeBuilder()
.id(null)
.build();
}
}