Compare commits

...

9 Commits

9 changed files with 519 additions and 2 deletions
@@ -19,8 +19,26 @@ public class Commande {
private String pays; private String pays;
private String modePaiement; private String modePaiement;
private UUID commandeId ;
private double montantTotal ;
private Integer pointsFideliteGagnes ;
public void setRandomUUID() { public void setRandomUUID() {
this.clientId = UUID.randomUUID(); this.clientId = UUID.randomUUID();
} }
public void setRandomUUIDCommande() {
this.commandeId = UUID.randomUUID();
}
public int convertPointsFidelite(){
if (this.montantTotal==0|| this.montantTotal<=0){
return 0;
}
return (int) Math.ceil(this.getMontantTotal());
}
// conversion point de fidéliter
} }
@@ -0,0 +1,36 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.book.entity.Book;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public class ComandeRepository {
private final List<Commande> commande = new ArrayList<>();
public List<Commande> findAll(){return commande;}
public Commande save(Commande newCommande){
Optional<Commande> optionalCommandeWithSameIsbn = this.findById(newCommande.getCommandeId());
optionalCommandeWithSameIsbn.ifPresent(commande::remove);
this.commande.add(newCommande);
return newCommande;
}
public Optional<Commande> findById(UUID uuid){
return this.commande.stream()
.filter(customer -> customer.getCommandeId().equals(uuid))
.findFirst();
}
public boolean existsById(UUID uuid) {
return this.commande.stream()
.anyMatch(customer -> customer.getCommandeId().equals(uuid));
}
}
@@ -1,9 +1,11 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity; package fr.iut_fbleau.but3.dev62.mylibrary.commande.entity;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer; import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -25,6 +27,7 @@ public class CommandeTest {
String ville = "LKa Rochette" ; String ville = "LKa Rochette" ;
String codePostal = "7700" ; String codePostal = "7700" ;
String pays = "France" ; String pays = "France" ;
String modePayement = ModePaiement.CB.name() ;
Commande commande = Commande.builder() Commande commande = Commande.builder()
.clientId(clientId) .clientId(clientId)
@@ -33,6 +36,7 @@ public class CommandeTest {
.ville(ville) .ville(ville)
.codePostal(codePostal) .codePostal(codePostal)
.pays(pays) .pays(pays)
.modePaiement(modePayement)
.build(); .build();
assertEquals(clientId, commande.getClientId()); assertEquals(clientId, commande.getClientId());
@@ -41,6 +45,7 @@ public class CommandeTest {
assertEquals(ville, commande.getVille()); assertEquals(ville, commande.getVille());
assertEquals(codePostal, commande.getCodePostal()); assertEquals(codePostal, commande.getCodePostal());
assertEquals(pays, commande.getPays()); assertEquals(pays, commande.getPays());
assertEquals(modePayement, commande.getModePaiement());
} }
@@ -57,6 +62,40 @@ public class CommandeTest {
assertNotEquals(originalId, commande.getClientId()); assertNotEquals(originalId, commande.getClientId());
} }
@Test
@DisplayName("setRandomUUID should change the ID to a new random UUID")
void testSetRandomUUIDForCommande() {
Commande commande = Commande.builder().build();
UUID originalId = commande.getCommandeId();
commande.setRandomUUIDCommande();
assertNotNull(commande.getCommandeId());
assertNotEquals(originalId, commande.getCommandeId());
}
@Test
void testConvertPointsFideliteGagnes(){
Commande commande = Commande.builder()
.montantTotal(14.34)
.build();
Integer point = commande.convertPointsFidelite();
assertEquals(point,(int) Math.ceil(commande.getMontantTotal()));
}
@ParameterizedTest
@ValueSource(doubles = {-1.9,0.0})
void testConvertPointsFideliteGagnesWhenMontantTotalIsNotGood(){
Commande commande = Commande.builder()
.montantTotal(14.34)
.build();
Integer point = commande.convertPointsFidelite();
assertEquals(point,(int) Math.ceil(commande.getMontantTotal()));
}
@@ -0,0 +1,4 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception;
public class Test {
}
@@ -0,0 +1,67 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NotValidAdressExceptionTest {
@Test
@DisplayName("Exception should be created with the provided message")
void testExceptionCreation() {
String errorMessage = "Adresse is not valid";
NotValidAdressException exception = new NotValidAdressException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {
"rue is not valide",
"city is not valide",
"code postal is not valide",
"Country is not valide"
})
@DisplayName("Exception should handle different validation messages")
void testExceptionWithDifferentMessages(String errorMessage) {
NotValidAdressException exception = new NotValidAdressException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
String errorMessage = "Required field is missing";
Exception exception = assertThrows(NotValidAdressException.class, () -> {
throw new NotValidAdressException(errorMessage);
});
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be catchable as a general Exception")
void testExceptionInheritance() {
String errorMessage = "Invalid Adresse ";
try {
throw new NotValidAdressException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidAdressException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -0,0 +1,61 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NotValidCommandeExceptionTest {
@Test
@DisplayName("Exception should be created with the provided message")
void testExceptionCreation() {
String errorMessage = "Commande is not valid";
NotValidCommandeException exception = new NotValidCommandeException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@ParameterizedTest
@ValueSource(strings = {
"Mode Paiement is not valide",
"List ligne commande is not valide"
})
@DisplayName("Exception should handle different validation messages")
void testExceptionWithDifferentMessages(String errorMessage) {
NotValidCommandeException exception = new NotValidCommandeException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
String errorMessage = "Required field is missing";
Exception exception = assertThrows(NotValidCommandeException.class, () -> {
throw new NotValidCommandeException(errorMessage);
});
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be catchable as a general Exception")
void testExceptionInheritance() {
String errorMessage = "Invalid Commande ";
try {
throw new NotValidCommandeException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidCommandeException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -0,0 +1,47 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.valid;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NotValidLigneCommandeExceptionTest {
@Test
@DisplayName("Exception should handle different validation messages")
void testExceptionCreation() {
String errorMessage = "Ligne commande is not valide";
NotValidLigneCommandeException exception = new NotValidLigneCommandeException(errorMessage);
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be properly thrown and caught")
void testExceptionCanBeThrownAndCaught() {
String errorMessage = "Required field is missing";
Exception exception = assertThrows(NotValidLigneCommandeException.class, () -> {
throw new NotValidLigneCommandeException(errorMessage);
});
assertEquals(errorMessage, exception.getMessage());
}
@Test
@DisplayName("Exception should be catchable as a general Exception")
void testExceptionInheritance() {
String errorMessage = "Invalid Commande ";
try {
throw new NotValidLigneCommandeException(errorMessage);
} catch (Exception e) {
assertEquals(NotValidLigneCommandeException.class, e.getClass());
assertEquals(errorMessage, e.getMessage());
}
}
}
@@ -0,0 +1,246 @@
package fr.iut_fbleau.but3.dev62.mylibrary.commande.repository;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.Commande;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.entity.Customer;
import fr.iut_fbleau.but3.dev62.mylibrary.customer.repository.CustomerRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CommandeRepositoryTest {
private ComandeRepository repository;
private Commande commandeN1;
private Commande commandeN2;
private Commande commandeN3;
@BeforeEach
void setUp() {
repository = new ComandeRepository();
UUID clientId = UUID.randomUUID();
List<LigneCommandeInfo> lignesCommande = new ArrayList<>();
LigneCommandeInfo commande1 = new LigneCommandeInfo(12);
LigneCommandeInfo commande2 = new LigneCommandeInfo(13);
lignesCommande.add(commande1);
lignesCommande.add(commande2);
String rue = "rue du chien";
String ville = "LKa Rochette";
String codePostal = "7700";
String pays = "France";
String modePayement = ModePaiement.CB.name();
commandeN1 = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande)
.rue(rue)
.ville(ville)
.codePostal(codePostal)
.pays(pays)
.modePaiement(modePayement)
.build();
commandeN1.setRandomUUIDCommande();
List<LigneCommandeInfo> lignesCommande2 = new ArrayList<>();
LigneCommandeInfo commande3 = new LigneCommandeInfo(43);
LigneCommandeInfo commande4 = new LigneCommandeInfo(1);
lignesCommande2.add(commande3);
lignesCommande2.add(commande4);
commandeN2 = Commande.builder()
.clientId(clientId)
.lignesCommande(lignesCommande2)
.rue(rue)
.ville(ville)
.codePostal(codePostal)
.pays(pays)
.modePaiement(modePayement)
.build();
commandeN2.setRandomUUID();
commandeN2.setRandomUUIDCommande();
List<LigneCommandeInfo> lignesCommande3 = new ArrayList<>();
LigneCommandeInfo commande6 = new LigneCommandeInfo(12);
LigneCommandeInfo commande7 = new LigneCommandeInfo(13);
lignesCommande3.add(commande6);
lignesCommande3.add(commande7);
String rue2 = "rue du chevre chaud ";
String ville2 = "jsp";
String codePostal2 = "77";
String pays2 = "Lune ";
String modePayement2 = ModePaiement.PAYPAL.name();
commandeN3 = Commande.builder()
.lignesCommande(lignesCommande3)
.rue(rue2)
.ville(ville2)
.codePostal(codePostal2)
.pays(pays2)
.modePaiement(modePayement2)
.build();
commandeN3.setRandomUUID();
commandeN3.setRandomUUIDCommande();
}
@Test
@DisplayName("New repository should be empty")
void testNewRepositoryIsEmpty() {
List<Commande> commande = repository.findAll();
assertTrue(commande.isEmpty());
assertEquals(0, commande.size());
}
@Nested
@DisplayName("Save operations")
class SaveOperations {
@Test
@DisplayName("Save should add a new comande")
void testSaveNewCommande() {
Commande savedCommande = repository.save(commandeN1);
assertEquals(1, repository.findAll().size());
assertEquals(commandeN1.getCommandeId(), savedCommande.getCommandeId());
assertEquals(commandeN1.getClientId(), savedCommande.getClientId());
}
@Test
@DisplayName("Save should update existing commande with same ID")
void testSaveUpdatesExistingCommande() {
repository.save(commandeN1);
List<LigneCommandeInfo> lignesCommande2 = new ArrayList<>();
LigneCommandeInfo commande3 = new LigneCommandeInfo(43);
LigneCommandeInfo commande4 = new LigneCommandeInfo(1);
lignesCommande2.add(commande3);
lignesCommande2.add(commande4);
UUID id = commandeN1.getCommandeId();
Commande updatedCommande = Commande.builder()
.commandeId(id)
.lignesCommande(lignesCommande2)
.rue("rue")
.ville("moi")
.codePostal("1")
.pays("Russie")
.modePaiement(ModePaiement.POINTS_FIDELITE.name())
.build();
Commande savedCommande = repository.save(updatedCommande);
assertEquals(1, repository.findAll().size());
assertEquals(id, savedCommande.getCommandeId());
assertEquals("rue", savedCommande.getRue());
assertEquals("moi", savedCommande.getVille());
assertEquals("1", savedCommande.getCodePostal());
assertEquals("Russie", savedCommande.getPays());
assertEquals(ModePaiement.POINTS_FIDELITE.name(), savedCommande.getModePaiement());
}
@Test
@DisplayName("Save multiple customers should add all of them")
void testSaveMultipleCommande() {
repository.save(commandeN1);
repository.save(commandeN2);
List<Commande> commande = repository.findAll();
assertEquals(2, commande.size());
assertTrue(commande.contains(commandeN1));
assertTrue(commande.contains(commandeN2));
}
}
@Nested
@DisplayName("Find operations")
class FindOperations {
@BeforeEach
void setUpCommande() {
repository.save(commandeN1);
repository.save(commandeN2);
}
@Test
@DisplayName("FindAll should return all commande")
void testFindAll() {
List<Commande> commande = repository.findAll();
assertEquals(2, commande.size());
assertTrue(commande.contains(commandeN1));
assertTrue(commande.contains(commandeN2));
}
@Test
@DisplayName("FindById should return commande with matching ID")
void testFindById() {
Optional<Commande> foundCommande = repository.findById(commandeN1.getCommandeId());
assertTrue(foundCommande.isPresent());
assertEquals(commandeN1.getCommandeId(), foundCommande.get().getCommandeId());
assertEquals(commandeN1.getClientId(), foundCommande.get().getClientId());
}
@Test
void testFindByIdNotFound(){
UUID nonExistentId = UUID.randomUUID();
Optional<Commande> foundCommande = repository.findById(nonExistentId);
assertTrue(foundCommande.isEmpty());
}
@Test
void testExistsByIdExists(){
boolean exists = repository.existsById(commandeN1.getCommandeId());
assertTrue(exists);
}
@Test
void testExistsByIdNotExists(){
UUID nonExistentId = UUID.randomUUID();
boolean exists = repository.existsById(nonExistentId);
assertTrue(exists);
}
}
@Nested
@DisplayName("Delete operations")
class DeleteOperations {
}
}
@@ -4,7 +4,6 @@ import fr.iut_fbleau.but3.dev62.mylibrary.commande.AdresseInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ComandeInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.ComandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo; import fr.iut_fbleau.but3.dev62.mylibrary.commande.LigneCommandeInfo;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement; import fr.iut_fbleau.but3.dev62.mylibrary.commande.ModePaiement;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.entity.LigneCommande;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException; import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidAdressException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException; import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidCommandeException;
import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException; import fr.iut_fbleau.but3.dev62.mylibrary.commande.exception.NotValidLigneCommandeException;