diff --git a/pom.xml b/pom.xml index d867374..cb0b629 100644 --- a/pom.xml +++ b/pom.xml @@ -87,6 +87,26 @@ spring-web 6.1.0 + + + com.oracle.database.jdbc + ojdbc8 + 21.3.0.0 + + + com.oracle.database.xml + xdb + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-logging + + + diff --git a/src/main/java/fr/abes/kbart2kafka/Kbart2kafkaApplication.java b/src/main/java/fr/abes/kbart2kafka/Kbart2kafkaApplication.java index 46379f5..81ec156 100644 --- a/src/main/java/fr/abes/kbart2kafka/Kbart2kafkaApplication.java +++ b/src/main/java/fr/abes/kbart2kafka/Kbart2kafkaApplication.java @@ -1,9 +1,14 @@ package fr.abes.kbart2kafka; +import fr.abes.kbart2kafka.exception.IllegalDateException; import fr.abes.kbart2kafka.exception.IllegalFileFormatException; +import fr.abes.kbart2kafka.exception.IllegalPackageException; import fr.abes.kbart2kafka.exception.IllegalProviderException; +import fr.abes.kbart2kafka.repository.ProviderRepository; import fr.abes.kbart2kafka.service.FileService; +import fr.abes.kbart2kafka.service.ProviderPackageService; import fr.abes.kbart2kafka.utils.CheckFiles; +import fr.abes.kbart2kafka.utils.Utils; import lombok.extern.slf4j.Slf4j; import org.apache.logging.log4j.ThreadContext; import org.springframework.beans.factory.annotation.Value; @@ -21,8 +26,14 @@ public class Kbart2kafkaApplication implements CommandLineRunner { private String kbartHeader; private final FileService service; - public Kbart2kafkaApplication(FileService service) { + private final ProviderPackageService providerPackageService; + + private final ProviderRepository providerRepository; + + public Kbart2kafkaApplication(FileService service, ProviderPackageService providerPackageService, ProviderRepository providerRepository) { this.service = service; + this.providerPackageService = providerPackageService; + this.providerRepository = providerRepository; } public static void main(String[] args) { @@ -46,10 +57,10 @@ public void run(String... args) throws IOException { ThreadContext.put("package", args[0]); // Récupération du chemin d'accès au fichier File tsvFile = new File(args[0]); - // Appelle du service de vérification de fichier try { CheckFiles.verifyFile(tsvFile, kbartHeader); - } catch (IllegalFileFormatException | IllegalProviderException e) { + checkExistingPackage(tsvFile.getName()); + } catch (IllegalFileFormatException | IllegalProviderException | IllegalPackageException | IllegalDateException e) { log.error(e.getMessage()); throw new RuntimeException(e); } @@ -59,4 +70,9 @@ public void run(String... args) throws IOException { double executionTime = (double) (endTime - startTime) / 1000; log.info("Temps d'exécution : " + executionTime + " secondes"); } + + private void checkExistingPackage(String filename) throws IllegalProviderException, IllegalPackageException, IllegalDateException { + if (providerPackageService.hasMoreRecentPackageInBdd(Utils.extractProvider(filename), Utils.extractPackageName(filename), Utils.extractDate(filename))) + throw new IllegalPackageException("Un package plus récent est déjà présent dans la base"); + } } diff --git a/src/main/java/fr/abes/kbart2kafka/configuration/AbstractConfig.java b/src/main/java/fr/abes/kbart2kafka/configuration/AbstractConfig.java new file mode 100644 index 0000000..549a2dc --- /dev/null +++ b/src/main/java/fr/abes/kbart2kafka/configuration/AbstractConfig.java @@ -0,0 +1,25 @@ +package fr.abes.kbart2kafka.configuration; + +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; + +import java.util.HashMap; + +public abstract class AbstractConfig { + protected void configHibernate(LocalContainerEntityManagerFactoryBean em, String platform, boolean showsql, String dialect, String ddlAuto, boolean generateDdl, String initMode) { + HibernateJpaVendorAdapter vendorAdapter + = new HibernateJpaVendorAdapter(); + vendorAdapter.setGenerateDdl(generateDdl); + vendorAdapter.setShowSql(showsql); + vendorAdapter.setDatabasePlatform(platform); + em.setJpaVendorAdapter(vendorAdapter); + HashMap properties = new HashMap<>(); + properties.put("hibernate.format_sql", true); + properties.put("hibernate.hbm2ddl.auto", ddlAuto); + properties.put("hibernate.dialect", dialect); + properties.put("logging.level.org.hibernate", "DEBUG"); + properties.put("hibernate.type", "trace"); + properties.put("spring.sql.init.mode", initMode); + em.setJpaPropertyMap(properties); + } +} diff --git a/src/main/java/fr/abes/kbart2kafka/configuration/BaconDbConfig.java b/src/main/java/fr/abes/kbart2kafka/configuration/BaconDbConfig.java new file mode 100644 index 0000000..0877955 --- /dev/null +++ b/src/main/java/fr/abes/kbart2kafka/configuration/BaconDbConfig.java @@ -0,0 +1,56 @@ +package fr.abes.kbart2kafka.configuration; + +import lombok.NoArgsConstructor; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.transaction.PlatformTransactionManager; + +import javax.sql.DataSource; + +@Configuration +@EnableJpaRepositories(entityManagerFactoryRef = "baconEntityManager", transactionManagerRef = "baconTransactionManager", basePackages = "fr.abes.kbart2kafka.repository") +@NoArgsConstructor +public class BaconDbConfig extends AbstractConfig { + @Value("${spring.jpa.bacon.show-sql}") + protected boolean showsql; + @Value("${spring.jpa.bacon.properties.hibernate.dialect}") + protected String dialect; + @Value("${spring.jpa.bacon.hibernate.ddl-auto}") + protected String ddlAuto; + @Value("${spring.jpa.bacon.database-platform}") + protected String platform; + @Value("${spring.jpa.bacon.generate-ddl}") + protected boolean generateDdl; + @Value("${spring.sql.bacon.init.mode}") + protected String initMode; + + @Bean + @ConfigurationProperties(prefix = "spring.datasource.bacon") + public DataSource baconDataSource() { return DataSourceBuilder.create().build(); } + + @Bean + public LocalContainerEntityManagerFactoryBean baconEntityManager() { + LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + em.setDataSource(baconDataSource()); + em.setPackagesToScan("fr.abes.kbart2kafka.entity"); + configHibernate(em, platform, showsql, dialect, ddlAuto, generateDdl, initMode); + return em; + } + + @Primary + @Bean + public PlatformTransactionManager baconTransactionManager(@Qualifier("baconEntityManager") LocalContainerEntityManagerFactoryBean entityManagerFactory) { + final JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(entityManagerFactory.getObject()); + return transactionManager; + } + +} diff --git a/src/main/java/fr/abes/kbart2kafka/entity/Provider.java b/src/main/java/fr/abes/kbart2kafka/entity/Provider.java new file mode 100644 index 0000000..58e1d04 --- /dev/null +++ b/src/main/java/fr/abes/kbart2kafka/entity/Provider.java @@ -0,0 +1,42 @@ +package fr.abes.kbart2kafka.entity; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.io.Serializable; + +@Entity +@Table(name = "PROVIDER") +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class Provider implements Serializable { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq-provider") + @SequenceGenerator(name = "seq-provider", sequenceName = "PROVIDER_SEQ", allocationSize = 1) + @Column(name = "IDT_PROVIDER") + private Integer idtProvider; + @Column(name = "PROVIDER") + private String provider; + @Column(name = "NOM_CONTACT") + private String nomContact; + @Column(name = "PRENOM_CONTACT") + private String prenomContact; + @Column(name = "MAIL_CONTACT") + private String mailContact; + @Column(name = "DISPLAY_NAME") + private String displayName; + + + public Provider(String provider) { + this.provider = provider; + //on ne connait pas le display name à l'avance, on l'initialise au provider pour éviter une erreur not null dans la table + this.displayName = provider; + } + + +} diff --git a/src/main/java/fr/abes/kbart2kafka/entity/ProviderPackage.java b/src/main/java/fr/abes/kbart2kafka/entity/ProviderPackage.java new file mode 100644 index 0000000..411f15e --- /dev/null +++ b/src/main/java/fr/abes/kbart2kafka/entity/ProviderPackage.java @@ -0,0 +1,59 @@ +package fr.abes.kbart2kafka.entity; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.io.Serializable; +import java.util.Date; + +@Entity +@Table(name = "PROVIDER_PACKAGE") +@Getter @Setter +@AllArgsConstructor +@NoArgsConstructor +public class ProviderPackage implements Serializable, Comparable { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "ID_PROVIDER_PACKAGE") + private Integer idProviderPackage; + @Column(name = "PACKAGE") + private String packageName; + @Column(name = "DATE_P") + private Date dateP; + @Column(name = "PROVIDER_IDT_PROVIDER") + private Integer providerIdtProvider; + @Column(name = "LABEL_ABES") + private char labelAbes; + + @ManyToOne + @JoinColumn(referencedColumnName = "IDT_PROVIDER", insertable = false, updatable = false) + private Provider provider; + + public ProviderPackage(String packageName, Date dateP, Integer providerIdtProvider, char labelAbes) { + this.packageName = packageName; + this.dateP = dateP; + this.providerIdtProvider = providerIdtProvider; + this.labelAbes = labelAbes; + } + + public ProviderPackage(Integer idProviderPackage, String packageName, Date dateP, Integer providerIdtProvider, char labelAbes) { + this.idProviderPackage = idProviderPackage; + this.packageName = packageName; + this.dateP = dateP; + this.providerIdtProvider = providerIdtProvider; + this.labelAbes = labelAbes; + } + + @Override + public String toString() { + return "{ id:"+idProviderPackage + ", packageName:"+packageName+", providerIdt:"+providerIdtProvider+" dateP:"+dateP+" }"; + } + + @Override + public int compareTo(ProviderPackage o) { + return o.getDateP().compareTo(this.getDateP()); + } +} diff --git a/src/main/java/fr/abes/kbart2kafka/exception/IllegalDateException.java b/src/main/java/fr/abes/kbart2kafka/exception/IllegalDateException.java new file mode 100644 index 0000000..2458c8f --- /dev/null +++ b/src/main/java/fr/abes/kbart2kafka/exception/IllegalDateException.java @@ -0,0 +1,7 @@ +package fr.abes.kbart2kafka.exception; + +public class IllegalDateException extends Throwable { + public IllegalDateException(Exception e) { + super(e); + } +} diff --git a/src/main/java/fr/abes/kbart2kafka/exception/IllegalPackageException.java b/src/main/java/fr/abes/kbart2kafka/exception/IllegalPackageException.java new file mode 100644 index 0000000..394288d --- /dev/null +++ b/src/main/java/fr/abes/kbart2kafka/exception/IllegalPackageException.java @@ -0,0 +1,11 @@ +package fr.abes.kbart2kafka.exception; + +public class IllegalPackageException extends Throwable { + public IllegalPackageException(Exception e) { + super(e); + } + + public IllegalPackageException(String s) { + super(s); + } +} diff --git a/src/main/java/fr/abes/kbart2kafka/exception/IllegalProviderException.java b/src/main/java/fr/abes/kbart2kafka/exception/IllegalProviderException.java index c862c75..a4c5176 100644 --- a/src/main/java/fr/abes/kbart2kafka/exception/IllegalProviderException.java +++ b/src/main/java/fr/abes/kbart2kafka/exception/IllegalProviderException.java @@ -1,7 +1,9 @@ package fr.abes.kbart2kafka.exception; -public class IllegalProviderException extends Throwable { +public class IllegalProviderException extends Exception { public IllegalProviderException(String message) { super(message); } + + public IllegalProviderException(Exception e) { super(e); } } diff --git a/src/main/java/fr/abes/kbart2kafka/repository/ProviderPackageRepository.java b/src/main/java/fr/abes/kbart2kafka/repository/ProviderPackageRepository.java new file mode 100644 index 0000000..095df01 --- /dev/null +++ b/src/main/java/fr/abes/kbart2kafka/repository/ProviderPackageRepository.java @@ -0,0 +1,13 @@ +package fr.abes.kbart2kafka.repository; + +import fr.abes.kbart2kafka.entity.ProviderPackage; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ProviderPackageRepository extends JpaRepository { + List findByPackageNameAndProviderIdtProvider(String packageName, Integer providerIdtProvider); + +} diff --git a/src/main/java/fr/abes/kbart2kafka/repository/ProviderRepository.java b/src/main/java/fr/abes/kbart2kafka/repository/ProviderRepository.java new file mode 100644 index 0000000..fd60119 --- /dev/null +++ b/src/main/java/fr/abes/kbart2kafka/repository/ProviderRepository.java @@ -0,0 +1,13 @@ +package fr.abes.kbart2kafka.repository; + + +import fr.abes.kbart2kafka.entity.Provider; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface ProviderRepository extends JpaRepository { + Optional findByProvider(String provider); +} diff --git a/src/main/java/fr/abes/kbart2kafka/service/ProviderPackageService.java b/src/main/java/fr/abes/kbart2kafka/service/ProviderPackageService.java new file mode 100644 index 0000000..ca80f16 --- /dev/null +++ b/src/main/java/fr/abes/kbart2kafka/service/ProviderPackageService.java @@ -0,0 +1,34 @@ +package fr.abes.kbart2kafka.service; + +import fr.abes.kbart2kafka.entity.Provider; +import fr.abes.kbart2kafka.entity.ProviderPackage; +import fr.abes.kbart2kafka.repository.ProviderPackageRepository; +import fr.abes.kbart2kafka.repository.ProviderRepository; +import org.springframework.stereotype.Service; + +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Optional; + +@Service +public class ProviderPackageService { + private final ProviderPackageRepository repository; + + private final ProviderRepository providerRepository; + + public ProviderPackageService(ProviderPackageRepository repository, ProviderRepository providerRepository) { + this.repository = repository; + this.providerRepository = providerRepository; + } + + public boolean hasMoreRecentPackageInBdd(String provider, String packageName, Date datePackage) { + Optional providerBdd = providerRepository.findByProvider(provider); + if (providerBdd.isPresent()) { + List packageList = repository.findByPackageNameAndProviderIdtProvider(packageName, providerBdd.get().getIdtProvider()); + Collections.sort(packageList); + return packageList.get(0).getDateP().after(datePackage); + } + return false; + } +} diff --git a/src/main/java/fr/abes/kbart2kafka/utils/CheckFiles.java b/src/main/java/fr/abes/kbart2kafka/utils/CheckFiles.java index 9b5e0e0..46c1b66 100644 --- a/src/main/java/fr/abes/kbart2kafka/utils/CheckFiles.java +++ b/src/main/java/fr/abes/kbart2kafka/utils/CheckFiles.java @@ -23,7 +23,7 @@ public static Boolean detectFileName(File file) throws IllegalFileFormatExceptio public static void detectProvider(File file) throws IllegalProviderException { String filename = file.getName(); filename = filename.replace("\\", "/"); - if(!filename.contains("_") || filename.substring(0, filename.indexOf('_')).length() <= 0) { + if(!filename.contains("_") || filename.substring(0, filename.indexOf('_')).isEmpty()) { log.error("Message envoyé : {}", "Le nom du fichier ne contient pas de provider"); throw new IllegalProviderException("Le nom du fichier "+ filename +" ne contient pas de provider"); } diff --git a/src/main/java/fr/abes/kbart2kafka/utils/Utils.java b/src/main/java/fr/abes/kbart2kafka/utils/Utils.java index c382520..d919179 100644 --- a/src/main/java/fr/abes/kbart2kafka/utils/Utils.java +++ b/src/main/java/fr/abes/kbart2kafka/utils/Utils.java @@ -1,10 +1,15 @@ package fr.abes.kbart2kafka.utils; +import fr.abes.kbart2kafka.exception.IllegalDateException; +import fr.abes.kbart2kafka.exception.IllegalPackageException; +import fr.abes.kbart2kafka.exception.IllegalProviderException; + import java.net.URI; import java.net.URISyntaxException; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Utils { @@ -17,16 +22,42 @@ public static String extractDomainFromUrl(String url) throws URISyntaxException return host; } - public static > Map getMaxValuesFromMap(Map map) { - Map maxKeys = new HashMap<>(); - if (!map.isEmpty()) { - V maxValue = Collections.max(map.values()); - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue().equals(maxValue)) { - maxKeys.put(entry.getKey(), entry.getValue()); - } + public static String extractProvider(String filename) throws IllegalProviderException { + try { + return filename.substring(0, filename.indexOf('_')); + } catch (Exception e) { + throw new IllegalProviderException(e); + } + } + + public static String extractPackageName(String filename) throws IllegalPackageException { + try { + if (filename.contains("_FORCE")) { + String tempsStr = filename.substring(0, filename.indexOf("_FORCE")); + return tempsStr.substring(tempsStr.indexOf('_') + 1, tempsStr.lastIndexOf('_')); + } else if (filename.contains("_BYPASS")) { + String tempStr = filename.substring(0, filename.indexOf("_BYPASS")); + return tempStr.substring(tempStr.indexOf('_') + 1, tempStr.lastIndexOf('_')); + } else { + return filename.substring(filename.indexOf('_') + 1, filename.lastIndexOf('_')); + } + } catch (Exception e) { + throw new IllegalPackageException(e); + } + } + + public static Date extractDate(String filename) throws IllegalDateException { + Date date = new Date(); + try { + Matcher matcher = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})", Pattern.CASE_INSENSITIVE).matcher(filename); + if(matcher.find()){ + date = new SimpleDateFormat("yyyy-MM-dd").parse(matcher.group(1)); } + return date; + } catch (Exception e) { + throw new IllegalDateException(e); } - return maxKeys; } + + } diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 7a45c9c..f8f2d4d 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -4,3 +4,16 @@ spring.kafka.producer.nbthread= logging.level.root=INFO logging.level.fr.abes=DEBUG + +# Base Bacon +spring.datasource.bacon.driver-class-name=oracle.jdbc.OracleDriver +spring.datasource.bacon.jdbcurl= +spring.datasource.bacon.username= +spring.datasource.bacon.password= + +spring.jpa.bacon.database-platform=org.hibernate.dialect.OracleDialect +spring.jpa.bacon.properties.hibernate.dialect=org.hibernate.dialect.OracleDialect +spring.jpa.bacon.generate-ddl=false +spring.jpa.bacon.hibernate.ddl-auto=none +spring.jpa.bacon.show-sql=false +spring.sql.bacon.init.mode=never \ No newline at end of file diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 6a2195c..1c3aebe 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -3,4 +3,17 @@ spring.kafka.producer.bootstrap-servers= spring.kafka.producer.nbthread= logging.level.root=INFO -logging.level.fr.abes=ERROR \ No newline at end of file +logging.level.fr.abes=ERROR + +# Base Bacon +spring.datasource.bacon.driver-class-name=oracle.jdbc.OracleDriver +spring.datasource.bacon.jdbcurl= +spring.datasource.bacon.username= +spring.datasource.bacon.password= + +spring.jpa.bacon.database-platform=org.hibernate.dialect.OracleDialect +spring.jpa.bacon.properties.hibernate.dialect=org.hibernate.dialect.OracleDialect +spring.jpa.bacon.generate-ddl=false +spring.jpa.bacon.hibernate.ddl-auto=none +spring.jpa.bacon.show-sql=false +spring.sql.bacon.init.mode=never \ No newline at end of file diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties index 1863a7f..8701684 100644 --- a/src/main/resources/application-test.properties +++ b/src/main/resources/application-test.properties @@ -3,4 +3,17 @@ spring.kafka.producer.bootstrap-servers= spring.kafka.producer.nbthread= logging.level.root=INFO -logging.level.fr.abes=INFO \ No newline at end of file +logging.level.fr.abes=INFO + +# Base Bacon +spring.datasource.bacon.driver-class-name=oracle.jdbc.OracleDriver +spring.datasource.bacon.jdbcurl= +spring.datasource.bacon.username= +spring.datasource.bacon.password= + +spring.jpa.bacon.database-platform=org.hibernate.dialect.OracleDialect +spring.jpa.bacon.properties.hibernate.dialect=org.hibernate.dialect.OracleDialect +spring.jpa.bacon.generate-ddl=false +spring.jpa.bacon.hibernate.ddl-auto=none +spring.jpa.bacon.show-sql=false +spring.sql.bacon.init.mode=never \ No newline at end of file diff --git a/src/test/java/fr/abes/kbart2kafka/service/ProviderPackageServiceTest.java b/src/test/java/fr/abes/kbart2kafka/service/ProviderPackageServiceTest.java new file mode 100644 index 0000000..8305e68 --- /dev/null +++ b/src/test/java/fr/abes/kbart2kafka/service/ProviderPackageServiceTest.java @@ -0,0 +1,72 @@ +package fr.abes.kbart2kafka.service; + +import fr.abes.kbart2kafka.entity.Provider; +import fr.abes.kbart2kafka.entity.ProviderPackage; +import fr.abes.kbart2kafka.repository.ProviderPackageRepository; +import fr.abes.kbart2kafka.repository.ProviderRepository; +import org.assertj.core.util.Lists; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Optional; + +@SpringBootTest(classes = {ProviderPackageService.class}) +class ProviderPackageServiceTest { + @Autowired + ProviderPackageService service; + + @MockBean + ProviderPackageRepository providerPackageRepository; + + @MockBean + ProviderRepository providerRepository; + + @Test + @DisplayName("test isMoreRecentPackage : cas général") + void isMoreRecentPackageInBdd() { + Provider provider = new Provider(1, "provider", "nomContact", "prenomContact", "mail@contact.com", "displayName"); + Mockito.when(providerRepository.findByProvider("provider")).thenReturn(Optional.of(provider)); + Calendar calendar = Calendar.getInstance(); + calendar.set(2022, 10, 1); + Date date1 = calendar.getTime(); + ProviderPackage package1 = new ProviderPackage(1, "package", date1, 1, 'Y'); + calendar.set(2023, 8, 10); + Date date2 = calendar.getTime(); + ProviderPackage package2 = new ProviderPackage(2, "package", date2, 1, 'Y'); + calendar.set(2023, 7, 5); + Date date3 = calendar.getTime(); + ProviderPackage package3 = new ProviderPackage(3, "package", date3, 1, 'Y'); + + List packageList = Lists.newArrayList(package1, package2, package3); + Mockito.when(providerPackageRepository.findByPackageNameAndProviderIdtProvider("package", 1)).thenReturn(packageList); + + calendar.set(2023, 4, 20); + Date inputDate = calendar.getTime(); + Assertions.assertTrue(service.hasMoreRecentPackageInBdd("provider", "package", inputDate)); + + + calendar.set(2023, 11, 20); + inputDate = calendar.getTime(); + Assertions.assertFalse(service.hasMoreRecentPackageInBdd("provider", "package", inputDate)); + + //cas date égale + calendar.set(2023, 8, 10); + inputDate = calendar.getTime(); + Assertions.assertFalse(service.hasMoreRecentPackageInBdd("provider", "package", inputDate)); + } + + @Test + @DisplayName("test isMoreRecentPacakge : cas pas de provider trouvé") + void isMoreRecentPacakageInBddNoProvider() { + Mockito.when(providerRepository.findByProvider("provider")).thenReturn(Optional.empty()); + Assertions.assertFalse(service.hasMoreRecentPackageInBdd("provider", "package", Calendar.getInstance().getTime())); + } +} \ No newline at end of file diff --git a/src/test/java/fr/abes/kbart2kafka/utils/UtilsTest.java b/src/test/java/fr/abes/kbart2kafka/utils/UtilsTest.java index ae78fc0..1dee2e6 100644 --- a/src/test/java/fr/abes/kbart2kafka/utils/UtilsTest.java +++ b/src/test/java/fr/abes/kbart2kafka/utils/UtilsTest.java @@ -1,9 +1,17 @@ package fr.abes.kbart2kafka.utils; +import fr.abes.kbart2kafka.exception.IllegalDateException; +import fr.abes.kbart2kafka.exception.IllegalPackageException; +import fr.abes.kbart2kafka.exception.IllegalProviderException; +import jdk.jshell.execution.Util; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.net.URISyntaxException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; class UtilsTest { @Test @@ -23,4 +31,33 @@ void extractDomainFromUrlTest3() { String url = "teskljgfklj/test"; Assertions.assertThrows(URISyntaxException.class, () -> Utils.extractDomainFromUrl(url)); } + + @Test + void extractDate() throws IllegalDateException, ParseException { + String string = "2023-08-21"; + Date date = new SimpleDateFormat("yyyy-MM-dd").parse(string); + + Assertions.assertEquals(date, Utils.extractDate("SPRINGER_GLOBAL_ALLEBOOKS_2023-08-21.tsv")); + } + + @Test + @DisplayName("test récupération package dans nom de fichier") + void testextractPackageName() throws IllegalPackageException { + String filename = "SPRINGER_GLOBAL_ALLEBOOKS_2023-05-01_FORCE.tsv"; + Assertions.assertEquals("GLOBAL_ALLEBOOKS", Utils.extractPackageName(filename)); + + filename = "SPRINGER_GLOBAL_ALLEBOOKS_2023-05-01.tsv"; + Assertions.assertEquals("GLOBAL_ALLEBOOKS", Utils.extractPackageName(filename)); + + } + + @Test + @DisplayName("test récupération provider dans nom de fichier") + void testExtractProvider() throws IllegalProviderException { + String filename = "SPRINGER_GLOBAL_ALLEBOOKS_2023-05-01_FORCE.tsv"; + Assertions.assertEquals("SPRINGER", Utils.extractProvider(filename)); + + String filename2 = "test"; + Assertions.assertThrows(IllegalProviderException.class, () -> Utils.extractProvider(filename2)); + } }