diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/ColorRecipesExplorerApplicationTest.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/ColorRecipesExplorerApplicationTest.java new file mode 100644 index 0000000..ffa8bf7 --- /dev/null +++ b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/ColorRecipesExplorerApplicationTest.java @@ -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() { + } + +} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/ColorRecipesExplorerApplicationTests.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/ColorRecipesExplorerApplicationTests.java deleted file mode 100644 index 291e3a2..0000000 --- a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/ColorRecipesExplorerApplicationTests.java +++ /dev/null @@ -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() { - } - -} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/CompanyBuilder.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/CompanyBuilder.java new file mode 100644 index 0000000..47a1d97 --- /dev/null +++ b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/CompanyBuilder.java @@ -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); + } +} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/MaterialBuilder.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/MaterialBuilder.java new file mode 100644 index 0000000..28d7cc0 --- /dev/null +++ b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/MaterialBuilder.java @@ -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); + } +} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/MaterialTypeBuilder.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/MaterialTypeBuilder.java new file mode 100644 index 0000000..e9acad1 --- /dev/null +++ b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/MaterialTypeBuilder.java @@ -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); + } +} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/MixTypeBuilder.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/MixTypeBuilder.java new file mode 100644 index 0000000..fccab54 --- /dev/null +++ b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/builders/MixTypeBuilder.java @@ -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); + } +} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/CompanyServiceTests.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/CompanyServiceTests.java deleted file mode 100644 index b95af66..0000000 --- a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/CompanyServiceTests.java +++ /dev/null @@ -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 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 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 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 actual = companyService.getByID(testCompany.getCompanyID()); - - // Arrange - assertFalse(actual.isPresent()); - } - - @Test - public void getAllTest() { - // Arrange - List companies = new ArrayList<>(); - companies.add(testCompany); - - doReturn(companies).when(companyDao).findAll(); - - // Act - List actual = companyService.getAll(); - - // Assert - assertTrue(actual.contains(testCompany)); - } - - @Test - public void getAllTestShouldBeEmpty() { - // Arrange - doReturn(new ArrayList<>()).when(companyDao).findAll(); - - // Act - List 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 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 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 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 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 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 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); - } -} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MaterialServiceTests.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MaterialServiceTests.java deleted file mode 100644 index b86aa8f..0000000 --- a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MaterialServiceTests.java +++ /dev/null @@ -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 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 actual = materialService.getAllByMaterialType(testMaterialType); - - // Assert - assertTrue(actual.contains(testMaterial)); - } - - @Test - public void getAllByMaterialTypeTestShouldBeEmpty() { - // Arrange - doReturn(new ArrayList<>()).when(materialDao).findAllByMaterialType(testMaterialType); - - // Act - List actual = materialService.getAllByMaterialType(testMaterialType); - - // Assert - assertTrue(actual.isEmpty()); - } - - @Test - public void getAllByMaterialTypeTestNull() { - // Arrange - doReturn(testMaterialsList).when(materialDao).findAllByMaterialType(any()); - - // Act - List 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) 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) 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) 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) 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) 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 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 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 actual = materialService.getAllBySearchString(mixTypeMaterial.getName()); - - // Assert - assertFalse(actual.contains(mixTypeMaterial)); - } - -} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MaterialTypeServiceTests.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MaterialTypeServiceTests.java deleted file mode 100644 index cc94380..0000000 --- a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MaterialTypeServiceTests.java +++ /dev/null @@ -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 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 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 actual = materialTypeService.getByName(testMaterialType.getName()); - assertFalse(actual.isPresent()); - } - - @Test - public void getByNameTestNull() { - doReturn(Optional.empty()).when(materialTypeDao).findByMaterialTypeName(testMaterialType.getName()); - - Optional actual = materialTypeService.getByName(null); - assertFalse(actual.isPresent()); - } - - @Test - public void getDefaultMaterialTypeTest() { - doReturn(Optional.of(testMaterialType)).when(materialTypeService).getByName(MaterialType.DEFAULT_MATERIAL_TYPE_NAME); - - Optional actual = materialTypeService.getDefaultMaterialType(); - assertTrue(actual.isPresent()); - } - - // Test non-fonctionnel -// @Test -// public void getDefaultMaterialTypeGetByNameFail() { -// doReturn(Optional.empty()).when(materialTypeService).getByName(any()); -// -// Optional 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); - } - -} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MixQuantityServiceTests.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MixQuantityServiceTests.java deleted file mode 100644 index 48ba90c..0000000 --- a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MixQuantityServiceTests.java +++ /dev/null @@ -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); - } - -} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MixServiceTests.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MixServiceTests.java deleted file mode 100644 index 934b447..0000000 --- a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MixServiceTests.java +++ /dev/null @@ -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 materials = new ArrayList<>(); -// materials.add(0); -// List quantities = new ArrayList<>(); -// List 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 materials = new ArrayList<>(); -// materials.add(0); -// List quantities = new ArrayList<>(); -// List 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); -// } - -} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MixTypeServiceTests.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MixTypeServiceTests.java deleted file mode 100644 index c25317c..0000000 --- a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/MixTypeServiceTests.java +++ /dev/null @@ -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 actual = mixTypeService.getByName(testMixType.getName()); - assertTrue(actual.isPresent()); - } - - @Test - public void getByNameTestShouldBeEmpty() { - doReturn(Optional.empty()).when(mixTypeDao).findByTypeName(testMixType.getName()); - - Optional actual = mixTypeService.getByName(testMixType.getName()); - assertFalse(actual.isPresent()); - } - - @Test - public void getByNameTestNull() { - doReturn(Optional.empty()).when(mixTypeDao).findByTypeName(any()); - - Optional actual = mixTypeService.getByName(null); - assertFalse(actual.isPresent()); - } - - @Test - public void getByMaterialTest() { - doReturn(Optional.of(testMixType)).when(mixTypeDao).findByMaterial(testMaterial); - - Optional actual = mixTypeService.getByMaterial(testMaterial); - assertTrue(actual.isPresent()); - } - - @Test - public void getByMaterialTestShouldBeEmpty() { - doReturn(Optional.empty()).when(mixTypeDao).findByMaterial(testMaterial); - - Optional actual = mixTypeService.getByMaterial(testMaterial); - assertFalse(actual.isPresent()); - } - - @Test - public void getByMaterialTestNull() { - doReturn(Optional.empty()).when(mixTypeDao).findByMaterial(any()); - - Optional 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 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 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 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 actual = mixTypeService.createByName(null); - assertFalse(actual.isPresent()); - } - -} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/RecipeServiceTests.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/RecipeServiceTests.java deleted file mode 100644 index dacf4dc..0000000 --- a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/core/services/RecipeServiceTests.java +++ /dev/null @@ -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 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 testRecipesList = new ArrayList<>(); - testRecipesList.add(testRecipe); - - doReturn(testRecipesList).when(recipeDao).findAllByCompany(testCompany); - - List actual = recipeService.getByCompany(testCompany); - assertFalse(actual.isEmpty()); - } - - @Test - public void getByCompanyTestShouldBeEmpty() { - doReturn(new ArrayList<>()).when(recipeDao).findAllByCompany(testCompany); - - List actual = recipeService.getByCompany(testCompany); - assertTrue(actual.isEmpty()); - } - - @Test - public void getByCompanyTestNull() { - doReturn(new ArrayList<>()).when(recipeDao).findAllByCompany(any()); - - List 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 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 actual = recipeService.getSortedMixes(testRecipe); -// assertTrue(actual.isEmpty()); -// } -// -// @Test -// public void getSortedMixesTestNull() { -// doReturn(new ArrayList<>()).when(testRecipe).getMixes(); -// -// List actual = recipeService.getSortedMixes(null); -// assertTrue(actual.isEmpty()); -// } - - @Test - public void convertAndCreateStepsTest() { - MultiValueMap multiValueMap = new LinkedMultiValueMap<>(); - List 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 actual = recipeService.convertAndCreateSteps(testRecipe, multiValueMap); - assertTrue(actual.isPresent()); - } - - @Test - public void convertAndCreateStepsTestWithPreviousSteps() { - MultiValueMap multiValueMap = new LinkedMultiValueMap<>(); - List testSteps = new LinkedList<>(); - testSteps.add("step 1"); - testSteps.add("step 2"); - testSteps.add("step 3"); - - multiValueMap.add("step", testSteps); - - List 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 actual = recipeService.convertAndCreateSteps(testRecipe, multiValueMap); - assertTrue(actual.isPresent()); - } - - @Test - public void convertAndCreateStepsTestShouldBeEmpty() { - MultiValueMap multiValueMap = new LinkedMultiValueMap<>(); - List testSteps = new LinkedList<>(); - testSteps.add("step 1"); - testSteps.add("step 2"); - testSteps.add("step 3"); - - multiValueMap.add("step", testSteps); - - List 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 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 actual = recipeService.convertAndCreateSteps(null, null); - assertFalse(actual.isPresent()); - } - -} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/AbstractServiceTest.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/AbstractServiceTest.java new file mode 100644 index 0000000..e7063d8 --- /dev/null +++ b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/AbstractServiceTest.java @@ -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, R extends JpaRepository> { + + 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 entities = Arrays.asList(entity, anotherEntity); + + when(repository.findAll()).thenReturn(entities); + + // when + List 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 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 entities = Arrays.asList(entity, anotherEntity); + + doAnswer(i -> i.getArgument(0)).when(service).saveAll(any()); + + // when + Collection found = service.saveAll(entities); + + // then + assertFalse(found.isEmpty()); + assertTrue(found.containsAll(entities)); + } + + @Test + public void whenSaveAll_thenReturnEmptyCollection() { + // given + List entities = new ArrayList<>(); + + doAnswer(i -> i.getArgument(0)).when(service).saveAll(any()); + + // when + Collection 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 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(); + +} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/CompanyServiceTest.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/CompanyServiceTest.java new file mode 100644 index 0000000..051ee6c --- /dev/null +++ b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/CompanyServiceTest.java @@ -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 { + + @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(); + } +} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/MaterialServiceTest.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/MaterialServiceTest.java new file mode 100644 index 0000000..19f8324 --- /dev/null +++ b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/MaterialServiceTest.java @@ -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 { + + @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 materials = Arrays.asList(material, anotherMaterial); + + doReturn(materials).when(materialService).getAll(); + + // when + List 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(); + } +} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/MaterialTypeServiceTest.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/MaterialTypeServiceTest.java new file mode 100644 index 0000000..6551a2c --- /dev/null +++ b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/MaterialTypeServiceTest.java @@ -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 { + + @MockBean + private MaterialTypeDao materialTypeDao; + + @Autowired + @SpyBean + private MaterialTypeService materialTypeService; + + @MockBean + private MaterialService materialService; + + @Spy + private List 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.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 materialTypes = Arrays.asList(defaultMaterialType, materialType); + + doReturn(materialTypes).when(materialTypeService).getAll(); + + // when + List 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(); + } +} diff --git a/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/MixTypeServiceTest.java b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/MixTypeServiceTest.java new file mode 100644 index 0000000..0cd3b45 --- /dev/null +++ b/src/tests/java/dev/fyloz/trial/colorrecipesexplorer/services/MixTypeServiceTest.java @@ -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 { + + @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(); + } +}