Skip to content

Commit

Permalink
Spontaneous payment improvements (#3)
Browse files Browse the repository at this point in the history
* rebase

* added cosmos config

Co-authored-by: aacitelli <[email protected]>
  • Loading branch information
alessio-acitelli and aacitelli authored Jun 17, 2022
1 parent 182e9f0 commit b9ecba9
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ target/**/*
.vscode
/.idea/
emulatorcert.crt
.env
*.env
**/*paDemandPaymentNoticeRequest.xml
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ The easiest way to develop locally is start only db container and run spring-boo
...
```

### Configure Cosmos emulator
### Configure Cosmos emulator
Launch the script in `utilities` folder:


Expand Down Expand Up @@ -126,4 +126,4 @@ Todo
Made with ❤️ from PagoPa S.p.A.

### Mainteiners
See `CODEOWNERS` file
See `CODEOWNERS` file
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${openfeign-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package it.gov.pagopa.spontaneouspayment;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.DependsOn;

import com.azure.spring.data.cosmos.core.mapping.EnableCosmosAuditing;
import com.azure.spring.data.cosmos.repository.config.EnableCosmosRepositories;

import it.gov.pagopa.spontaneouspayment.entity.CreditInstitution;
import it.gov.pagopa.spontaneouspayment.entity.Service;
import it.gov.pagopa.spontaneouspayment.entity.ServiceProperty;
import it.gov.pagopa.spontaneouspayment.entity.ServiceRef;
import it.gov.pagopa.spontaneouspayment.repository.CIRepository;
import it.gov.pagopa.spontaneouspayment.repository.ServiceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableCosmosRepositories("it.gov.pagopa.spontaneouspayment.repository")
@EnableCosmosAuditing
@DependsOn("expressionResolver")
public class SpontaneousPaymentApplication implements CommandLineRunner {

@Autowired
Expand All @@ -33,7 +37,7 @@ public static void main(String[] args) {

public void run(String... var1) {
CreditInstitution ci = new CreditInstitution();
ci.setOrganizationFiscalCode("organizationTest");
ci.setFiscalCode("organizationTest");
ci.setCompanyName("Comune di Roma");
ci.setStatus("ATTIVO");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package it.gov.pagopa.spontaneouspayment.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.DirectConnectionConfig;
Expand All @@ -10,10 +15,6 @@
import com.azure.spring.data.cosmos.core.ResponseDiagnosticsProcessor;
import com.azure.spring.data.cosmos.repository.config.EnableCosmosRepositories;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

@Configuration
@EnableCosmosRepositories
Expand All @@ -27,20 +28,15 @@ public class AppConfiguration extends AbstractCosmosConfiguration {
@Value("${azure.cosmos.key}")
private String key;

@Value("${azure.cosmos.secondary-key}")
private String secondaryKey;

@Value("${azure.cosmos.database}")
private String dbName;

@Value("${azure.cosmos.populate-query-metrics}")
private boolean queryMetricsEnabled;

private AzureKeyCredential azureKeyCredential;

@Bean
public CosmosClientBuilder getCosmosClientBuilder() {
this.azureKeyCredential = new AzureKeyCredential(key);
AzureKeyCredential azureKeyCredential = new AzureKeyCredential(key);
DirectConnectionConfig directConnectionConfig = new DirectConnectionConfig();
GatewayConnectionConfig gatewayConnectionConfig = new GatewayConnectionConfig();
return new CosmosClientBuilder()
Expand All @@ -57,10 +53,6 @@ public CosmosConfig cosmosConfig() {
.build();
}

public void switchToSecondaryKey() {
this.azureKeyCredential.update(secondaryKey);
}

@Override
protected String getDatabaseName() {
return dbName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class EntityValidator {
* @param joinPoint not used
* @param result the response to validate
*/
@AfterReturning(pointcut = "execution(* it.gov.pagopa.spontaneouspayment.repository.*.*(..))", returning = "result")
@AfterReturning(pointcut = "execution(* it.gov.pagopa.spontaneouspayment.repository.impl.*.*(..))", returning = "result")
public void validateResponse(JoinPoint joinPoint, Object result) {
if (result instanceof ResponseEntity) {
validateResponse((ResponseEntity<?>) result);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package it.gov.pagopa.spontaneouspayment.controller.impl;

import it.gov.pagopa.spontaneouspayment.controller.IPaymentsController;
import it.gov.pagopa.spontaneouspayment.model.SpontaneousPaymentModel;
import it.gov.pagopa.spontaneouspayment.model.response.PaymentPositionModel;
import it.gov.pagopa.spontaneouspayment.service.PaymentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;

import it.gov.pagopa.spontaneouspayment.controller.IPaymentsController;
import it.gov.pagopa.spontaneouspayment.model.SpontaneousPaymentModel;
import it.gov.pagopa.spontaneouspayment.model.response.PaymentPositionModel;
import it.gov.pagopa.spontaneouspayment.service.PaymentsService;

@Controller
@Slf4j
public class PaymentsController implements IPaymentsController {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package it.gov.pagopa.spontaneouspayment.entity;

import com.azure.spring.data.cosmos.core.mapping.Container;
import com.azure.spring.data.cosmos.core.mapping.PartitionKey;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -14,20 +16,17 @@
import java.time.LocalDateTime;
import java.util.List;

@Container(containerName = "credit_institution", ru = "400")
@Container(containerName = "${azure.cosmos.ec-container-name}", autoCreateContainer = false)
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class CreditInstitution {

//@Id
//@GeneratedValue
//private String id;

@Id
//@PartitionKey
private String organizationFiscalCode;
@PartitionKey
private String fiscalCode;

@NotBlank(message = "company name is required")
private String companyName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package it.gov.pagopa.spontaneouspayment.entity;

import com.azure.spring.data.cosmos.core.mapping.Container;
import com.azure.spring.data.cosmos.core.mapping.PartitionKey;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -13,7 +15,7 @@
import java.time.LocalDateTime;
import java.util.List;

@Container(containerName = "services", ru = "400")
@Container(containerName = "${azure.cosmos.service-container-name}", autoCreateContainer = false)
@Getter
@Setter
@NoArgsConstructor
Expand All @@ -28,6 +30,7 @@ public class Service {
private String description;

@NotBlank(message = "transfer category is required")
@PartitionKey
private String transferCategory; // tassonomia

@NotBlank(message = "remittance information is required")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
@Repository
public interface CIRepository extends CosmosRepository<CreditInstitution, String> {

@Query("select TOP 1 * from c where c.organizationFiscalCode = @organizationFiscalCode and EXISTS (SELECT VALUE t FROM t IN c.services WHERE t.id = @serviceId)")
@Query("select TOP 1 * from c where c.fiscalCode = @organizationFiscalCode and EXISTS (SELECT VALUE t FROM t IN c.services WHERE t.id = @serviceId)")
List<CreditInstitution> getCreditInstitutionByOrgFiscCodeAndServiceId(@Param("organizationFiscalCode") String organizationFiscalCode, @Param("serviceId") String serviceId);

Optional<CreditInstitution> findByOrganizationFiscalCode(String organizationFiscalCode);
Optional<CreditInstitution> findByFiscalCode(String organizationFiscalCode);

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private it.gov.pagopa.spontaneouspayment.entity.Service getServiceDetails(@NotNu
}

private CreditInstitution getCreditorInstitution(String organizationFiscalCode) {
return ciRepository.findByOrganizationFiscalCode(organizationFiscalCode)
return ciRepository.findByFiscalCode(organizationFiscalCode)
.orElseThrow(() -> new AppException(AppError.ORGANIZATION_SERVICE_NOT_FOUND, organizationFiscalCode));
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/application-local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ properties.environment=local
# Cosmos account config
azure.cosmos.uri=https://localhost:8081
azure.cosmos.key=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
azure.cosmos.database=gps
azure.cosmos.secondary-key=
azure.cosmos.database=db
azure.cosmos.populate-query-metrics=false

azure.cosmos.ec-container-name=creditor_institutions
azure.cosmos.service-container-name=services

service.gpd.host=http://localhost:8085

# timeout
Expand Down
15 changes: 13 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#cors
#spring_cors_origin=*
server.port=9090

# info
application.name=@project.artifactId@
application.version=@project.version@
Expand All @@ -8,9 +12,11 @@ properties.environment=azure
azure.cosmos.uri=${COSMOS_URI}
azure.cosmos.key=${COSMOS_KEY}
azure.cosmos.database=${COSMOS_DATABASE}
azure.cosmos.secondary-key=${COSMOS_SECONDARY_KEY}
azure.cosmos.populate-query-metrics=${COSMOS_QUERY_METRICS}

azure.cosmos.ec-container-name=${EC_CONTAINER_NAME}
azure.cosmos.service-container-name=${SERVICE_CONTAINER_NAME}

service.gpd.host=${GPD_HOST}

# timeout
Expand All @@ -26,4 +32,9 @@ logging.level.root=${LOGGING_LEVEL}
logging.level.it.gov.pagopa.spontaneouspayment=${LOGGING_LEVEL}



#jpa:
# spring.jpa.hibernate.ddl-auto=validate
# spring.jpa.show-sql=false
# spring.jpa.open-in-view=false
spring.devtools.add-properties=true
spring.profiles.active=@spring.profiles.active@
81 changes: 81 additions & 0 deletions utilities/cosmos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

# set -e

# force delete image and container of MS AZ Cosmos DB Emulator
docker container rm -f test-linux-emulator
# force delete image and container of MS AZ Cosmos DB Emulator
docker image rm -f mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest

PORT=$1
JVM_HOME=$2

# Main ...
if [ -z "$PORT" ]
then
PORT=8081
echo "CosmosDB starting on DEFAULT port $PORT"
else
echo "CosmosDB starting on specific port $PORT"
fi

if [ -z "$JVM_HOME" ]
then
JVM_HOME=$JAVA_HOME
echo "Using DEFAULT JAVA_HOME $JVM_HOME"
else
echo "Using custom JAVA_HOME $JVM_HOME"
fi


# Azure Cosmos DB Emulator
URL="https://localhost:$PORT/_explorer/index.html"

ipaddr=$(ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' | head -n 1)
echo "Using ${ipaddr} for CosmosDB configuration..."

docker pull mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator

docker run \
--detach \
--publish $PORT:8081 \
--publish 10251-10254:10251-10254 \
--memory 3g --cpus=2.0 \
--name=test-linux-emulator \
--env AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10 \
--env AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true \
--env AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=$ipaddr \
--interactive \
--tty \
mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator

echo -n "CosmosDB starting..."
cosmos_started="`docker logs test-linux-emulator | grep -wc Started`"
echo -n $cosmos_started
# check cosmos is UP
while [ "$cosmos_started" != "12" ]
do
sleep 3
echo -n "."
cosmos_started=`docker logs test-linux-emulator | grep -wc Started`
echo -n $cosmos_started
done

echo "!!! STARTED !!!"

echo "Setting certificate..."

curl -k "https://${ipaddr}:${PORT}/_explorer/emulator.pem" > emulatorcert.crt

# add keychain accesss
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain emulatorcert.crt

# add jvm trust-store
keystore_alias="cosmoskeystore"
echo "Remember, the keystore passowrd is: changeit"
sudo keytool -delete -alias $keystore_alias -keystore "${JAVA_HOME}/lib/security/cacerts"
sudo keytool -trustcacerts -keystore "${JAVA_HOME}/lib/security/cacerts" -storepass changeit -importcert -alias $keystore_alias -file emulatorcert.crt

echo "Setting certificate...done."

open $URL

0 comments on commit b9ecba9

Please sign in to comment.