diff --git a/src/main/java/com/mastercard/app/petstore/PetstoreApplication.java b/src/main/java/com/mastercard/app/petstore/PetstoreApplication.java index ff94509..a54e36c 100644 --- a/src/main/java/com/mastercard/app/petstore/PetstoreApplication.java +++ b/src/main/java/com/mastercard/app/petstore/PetstoreApplication.java @@ -19,24 +19,6 @@ public class PetstoreApplication { @Autowired EmployeeFlowExample employeeFlowExample; - @Bean - PetFlowExample petFlowExample() { - petFlowExample = new PetFlowExample(); - return petFlowExample; - } - - @Bean - AdoptionFlowExample adoptionFlowExample() { - adoptionFlowExample = new AdoptionFlowExample(); - return adoptionFlowExample; - } - - @Bean - EmployeeFlowExample employeeFlowExample() { - employeeFlowExample = new EmployeeFlowExample(); - return employeeFlowExample; - } - @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { diff --git a/src/main/java/com/mastercard/app/petstore/examples/AdoptionFlowExample.java b/src/main/java/com/mastercard/app/petstore/examples/AdoptionFlowExample.java index 11012eb..2c46fde 100644 --- a/src/main/java/com/mastercard/app/petstore/examples/AdoptionFlowExample.java +++ b/src/main/java/com/mastercard/app/petstore/examples/AdoptionFlowExample.java @@ -16,15 +16,27 @@ * The type Adoption flow example. */ -@ComponentScan(basePackages = {"com.mastercard.app.petstore.utils"}) @Component("AdoptionFLowExample") public class AdoptionFlowExample { - @Autowired private AdoptionsService adoptionsService; - @Autowired private CatService catService; + /** + * Constructs an instance of {@code AdoptionFlowExample} with required dependencies. + *
+ * This constructor is annotated with {@link org.springframework.beans.factory.annotation.Autowired} + * so that Spring can automatically inject the required beans at runtime. + *
+ * + * @param adoptionsService the service responsible for handling adoption operations + * @param catService the service responsible for managing cat-related operations + */ + @Autowired + public AdoptionFlowExample(AdoptionsService adoptionsService, CatService catService){ + this.adoptionsService = adoptionsService; + this.catService = catService; + } /** * Adoption use case. Shows a typical adding an adoption, adopting pet, updating status and then removing it * diff --git a/src/main/java/com/mastercard/app/petstore/examples/EmployeeFlowExample.java b/src/main/java/com/mastercard/app/petstore/examples/EmployeeFlowExample.java index e21d5c4..98769e1 100644 --- a/src/main/java/com/mastercard/app/petstore/examples/EmployeeFlowExample.java +++ b/src/main/java/com/mastercard/app/petstore/examples/EmployeeFlowExample.java @@ -11,14 +11,26 @@ /** * The type Employee flow example. */ - -@ComponentScan(basePackages = {"com.mastercard.app.petstore.utils"}) @Component("EmployeeFlowExample") public class EmployeeFlowExample { - @Autowired private EmployeeService employeeService; + /** + * Constructs a new {@code EmployeeFlowExample} with the required service dependency. + *+ * This constructor is annotated with {@link org.springframework.beans.factory.annotation.Autowired}, + * allowing Spring to automatically inject the {@link EmployeeService} bean at runtime. + * The service is then used to perform operations related to employees within the flow example. + *
+ * + * @param employeeService the service responsible for managing employee-related operations + */ + @Autowired + public EmployeeFlowExample(EmployeeService employeeService){ + this.employeeService = employeeService; + } + /** * Employee use case. Show add a new employee, searching for their information, then removing them. * diff --git a/src/main/java/com/mastercard/app/petstore/examples/PetFlowExample.java b/src/main/java/com/mastercard/app/petstore/examples/PetFlowExample.java index 72f3f3f..cc1b711 100644 --- a/src/main/java/com/mastercard/app/petstore/examples/PetFlowExample.java +++ b/src/main/java/com/mastercard/app/petstore/examples/PetFlowExample.java @@ -11,15 +11,28 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Component; -@ComponentScan(basePackages = {"com.mastercard.app.petstore.utils"}) @Component("PetFlowExample") public class PetFlowExample { - @Autowired private CatService catService; - @Autowired private PetService petService; - + /** + * Constructs a new {@code PetFlowExample} with the required service dependencies. + *+ * This constructor is annotated with {@link org.springframework.beans.factory.annotation.Autowired}, + * allowing Spring to automatically inject the {@link CatService} and {@link PetService} beans + * at runtime. These services are then used to perform operations related to cats and pets + * within the flow example. + *
+ * + * @param catService the service responsible for managing cat-related operations + * @param petService the service responsible for managing general pet-related operations + */ + @Autowired + public PetFlowExample(CatService catService, PetService petService){ + this.catService = catService; + this.petService=petService; + } /** * Pet use case flow. Shows creating a cat, updating their status and then removing them * diff --git a/src/main/java/com/mastercard/app/petstore/utils/JweEncryptionUtils.java b/src/main/java/com/mastercard/app/petstore/utils/JweEncryptionUtils.java index 96a3b41..0cf5727 100644 --- a/src/main/java/com/mastercard/app/petstore/utils/JweEncryptionUtils.java +++ b/src/main/java/com/mastercard/app/petstore/utils/JweEncryptionUtils.java @@ -4,6 +4,7 @@ import com.mastercard.developer.encryption.EncryptionException; import com.mastercard.developer.encryption.JweConfigBuilder; import com.mastercard.developer.utils.EncryptionUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; @@ -15,15 +16,39 @@ @org.springframework.context.annotation.Configuration public class JweEncryptionUtils { - @Value("${mastercard.encryption.encryptionCert}") - private String encryptionCertificateFilePath; - @Value("${mastercard.encryption.decryptionKeys}") - private String decryptionKeyFilePath; - @Value("${mastercard.encryption.decryptionKeyAlias}") - private String decryptionKeyAlias; - @Value("${mastercard.encryption.decryptionKeyPassword}") - private String decryptionKeyPassword; + private final String encryptionCertificateFilePath; + private final String decryptionKeyFilePath; + + private final String decryptionKeyAlias; + + private final String decryptionKeyPassword; + + @Autowired + public JweEncryptionUtils( + @Value("${mastercard.encryption.encryptionCert}") String encryptionCertificateFilePath, + @Value("${mastercard.encryption.decryptionKeys") String decryptionKeyFilePath, + @Value("${mastercard.encryption.decryptionKeyAlias}") String decryptionKeyAlias, + @Value("${mastercard.encryption.decryptionKeyPassword}") String decryptionKeyPassword + ) + { + if (encryptionCertificateFilePath.isEmpty()){ + throw new IllegalArgumentException("encryptionCert in application.properties is empty"); + } + this.encryptionCertificateFilePath = encryptionCertificateFilePath; + if (decryptionKeyFilePath.isEmpty()){ + throw new IllegalArgumentException("decryptionKeys in application.properties is empty"); + } + this.decryptionKeyFilePath = decryptionKeyFilePath; + if (decryptionKeyAlias.isEmpty()){ + throw new IllegalArgumentException("decryptionKeyAlias in application.properties is empty"); + } + this.decryptionKeyAlias = decryptionKeyAlias; + if (decryptionKeyPassword.isEmpty()){ + throw new IllegalArgumentException("decryptionKeyPassword in application.properties is empty"); + } + this.decryptionKeyPassword = decryptionKeyPassword; + } /** * Sets field level encryption config for adoptions. * @@ -35,7 +60,6 @@ public class JweEncryptionUtils { */ @Bean public EncryptionConfig fieldLevelEncryptionConfigForAdoptions() throws EncryptionException, GeneralSecurityException, IOException { - checkProperties(); Certificate encryptionCertificate = EncryptionUtils.loadEncryptionCertificate(encryptionCertificateFilePath); PrivateKey decryptionKey = EncryptionUtils.loadDecryptionKey(decryptionKeyFilePath, decryptionKeyAlias, decryptionKeyPassword); @@ -59,7 +83,6 @@ public EncryptionConfig fieldLevelEncryptionConfigForAdoptions() throws Encrypti */ @Bean public EncryptionConfig fullBodyEncryptionConfig() throws EncryptionException, GeneralSecurityException, IOException { - checkProperties(); Certificate encryptionCertificate = EncryptionUtils.loadEncryptionCertificate(encryptionCertificateFilePath); PrivateKey decryptionKey = EncryptionUtils.loadDecryptionKey(decryptionKeyFilePath, decryptionKeyAlias, decryptionKeyPassword); @@ -70,19 +93,4 @@ public EncryptionConfig fullBodyEncryptionConfig() throws EncryptionException, G .withDecryptionKey(decryptionKey) .build(); } - - private void checkProperties() { - if (encryptionCertificateFilePath.isEmpty()){ - throw new IllegalArgumentException("encryptionCert in application.properties is empty"); - } - if (decryptionKeyFilePath.isEmpty()){ - throw new IllegalArgumentException("decryptionKeys in application.properties is empty"); - } - if (decryptionKeyAlias.isEmpty()){ - throw new IllegalArgumentException("decryptionKeyAlias in application.properties is empty"); - } - if (decryptionKeyPassword.isEmpty()){ - throw new IllegalArgumentException("decryptionKeyPassword in application.properties is empty"); - } - } }