Skip to content

Commit

Permalink
Merge pull request #7 from pagopa/PAGOPA-996-sviluppo-modifiche-lista…
Browse files Browse the repository at this point in the history
…-ec-aderenti

[PAGOPA-996] Authorizer enrolled EC's API update
  • Loading branch information
andrea-deri authored Jun 20, 2023
2 parents 2809e27 + 6ff7173 commit c3108d1
Show file tree
Hide file tree
Showing 20 changed files with 688 additions and 49 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG JAVA_VERSION=11
# This image additionally contains function core tools – useful when using custom extensions
#FROM mcr.microsoft.com/azure-functions/java:3.0-java$JAVA_VERSION-core-tools AS installer-env
FROM mcr.microsoft.com/azure-functions/java:3.0-java$JAVA_VERSION-build AS installer-env
#FROM mcr.microsoft.com/azure-functions/java:4.0-java$JAVA_VERSION-core-tools AS installer-env
FROM mcr.microsoft.com/azure-functions/java:4.0-java$JAVA_VERSION-build AS installer-env

COPY . /src/java-function-app
RUN cd /src/java-function-app && \
Expand All @@ -12,9 +12,9 @@ RUN cd /src/java-function-app && \
cp -a . /home/site/wwwroot

# This image is ssh enabled
#FROM mcr.microsoft.com/azure-functions/java:3.0-java$JAVA_VERSION-appservice
#FROM mcr.microsoft.com/azure-functions/java:4.0-java$JAVA_VERSION-appservice
# This image isn't ssh enabled
FROM mcr.microsoft.com/azure-functions/java:3.0-java$JAVA_VERSION
FROM mcr.microsoft.com/azure-functions/java:4.0-java$JAVA_VERSION

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
Expand Down
7 changes: 3 additions & 4 deletions integration-test/src/features/main.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ Feature: All about authorizer workflow
Scenario: Enrolled EC - Get valued list by existing domain
When the client execute a call for the domain "gpd"
Then the client receives status code 200
Then the client receives a non-empty list
Then the client receives an object with enrolled creditor institutions

Scenario: Enrolled EC - Get an empty list for a non-existing domain
Scenario: Enrolled EC - Get an error for a non-existing domain
When the client execute a call for the domain "xxx"
Then the client receives status code 200
Then the client receives an empty list
Then the client receives status code 400
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,9 @@ async function assertStatusCodeNotEquals(response, statusCode) {
assert.ok(response.status !== statusCode);
}

async function assertECListIsNotEmpty(response) {
console.log(` - Then the client receives a non-empty list..`);
assert.strictEqual(1, response.data.length)
assert.equal(JSON.parse(response.data), "77777777777");
}

async function assertECListIsEmpty(response) {
console.log(` - Then the client receives an empty list..`);
assert.strictEqual(0, response.data.length);
async function assertResponseWithEnrolledCI(response) {
console.log(` - Then the client receives an object with enrolled creditor institutions..`);
assert.ok(response.data.creditor_institutions !== undefined);
}

module.exports = {
Expand All @@ -81,6 +75,5 @@ module.exports = {
executeHealthCheckForGPDPayments,
generateAuthorization,
executeGetEnrolledECInvocation,
assertECListIsNotEmpty,
assertECListIsEmpty
assertResponseWithEnrolledCI
}
6 changes: 2 additions & 4 deletions integration-test/src/step_definitions/support/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const {
executeHealthCheckForGPDPayments,
generateAuthorization,
executeGetEnrolledECInvocation,
assertECListIsNotEmpty,
assertECListIsEmpty
assertResponseWithEnrolledCI
} = require('./logic/common_logic');


Expand All @@ -30,8 +29,7 @@ When('the client execute a call for entity {string} with subscription key {strin
When('the client execute a call for the domain {string}', (domain) => executeGetEnrolledECInvocation(domain, bundle));
Then('the client receives status code {int}', (statusCode) => assertStatusCodeEquals(bundle.response, statusCode));
Then('the client receives status code different from {int}', (statusCode) => assertStatusCodeNotEquals(bundle.response, statusCode));
Then('the client receives a non-empty list', () => assertECListIsNotEmpty(bundle.response));
Then('the client receives an empty list', () => assertECListIsEmpty(bundle.response));
Then('the client receives an object with enrolled creditor institutions', () => assertResponseWithEnrolledCI(bundle.response));


Before(function(scenario) {
Expand Down
2 changes: 1 addition & 1 deletion openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition - Enrolled EC",
"version": "0.0.5-1"
"version": "0.0.5-3"
},
"servers": [
{
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>it.gov.pagopa.authorizer</groupId>
<artifactId>platform-authorizer</artifactId>
<version>0.0.5-1</version>
<version>0.0.5-3</version>
<packaging>jar</packaging>

<name>Azure Authorizer cache Fn</name>
Expand Down Expand Up @@ -120,7 +120,7 @@
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~2</value>
<value>~4</value>
</property>
</appSettings>
<resourceGroup>java-functions-group</resourceGroup>
Expand Down
61 changes: 45 additions & 16 deletions src/main/java/it/gov/pagopa/authorizer/EnrolledEC.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
package it.gov.pagopa.authorizer;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Calendar;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.functions.annotation.*;
import it.gov.pagopa.authorizer.model.EnrolledCreditorInstitutions;
import it.gov.pagopa.authorizer.model.ProblemJson;
import it.gov.pagopa.authorizer.service.EnrollingService;
import it.gov.pagopa.authorizer.util.Constants;
import org.springframework.http.MediaType;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.CosmosDBInput;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;


public class EnrolledEC {

private final String apiconfigPath = System.getenv(Constants.APICONFIG_SELFCARE_INTEGRATION_PATH_PARAMETER);

private final String apiconfigSubkey = System.getenv(Constants.APICONFIG_SELFCARE_INTEGRATION_SUBKEY_PARAMETER);

@FunctionName("EnrolledECFunction")
public HttpResponseMessage run (
@HttpTrigger(
Expand All @@ -38,20 +45,42 @@ public HttpResponseMessage run (
sqlQuery = "%EC_SQL_QUERY%",
connectionStringSetting = "COSMOS_CONN_STRING"
) String[] enrolledECsDomain,
final ExecutionContext context) {
@BindingName("domain") String domain,
final ExecutionContext context) throws InterruptedException {

Instant start = Instant.now();

Logger logger = context.getLogger();
logger.log(Level.INFO, () -> String.format("Called endpoint [%s]: found [%d] element(s) related to the requested domain.", request.getUri().getPath(), enrolledECsDomain.length));

List<String> distinctResult = Arrays.asList(enrolledECsDomain).stream().distinct().collect(Collectors.toList());

HttpResponseMessage response = request.createResponseBuilder(HttpStatus.OK)
.body(distinctResult)
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.build();
logger.log(Level.FINE, () -> String.format("The execution will end with an HTTP status code %d and duration time %d ms", HttpStatus.OK.value(), Duration.between(start, Instant.now()).toMillis()));
HttpResponseMessage response;
try {
EnrollingService enrollingService = getEnrollingService(logger);
EnrolledCreditorInstitutions result = enrollingService.getEnrolledCI(enrolledECsDomain, domain);
response = request.createResponseBuilder(HttpStatus.OK)
.body(new ObjectMapper().writeValueAsString(result))
.header(Constants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
logger.log(Level.FINE, () -> String.format("The execution will end with an HTTP status code %d and duration time %d ms", HttpStatus.OK.value(), Duration.between(start, Instant.now()).toMillis()));
} catch (URISyntaxException | IOException e) {
response = request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ProblemJson.builder().status(500).title("Communication error").detail("Error during communication with APIConfig for segregation codes retrieving.").build())
.header(Constants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
logger.log(Level.SEVERE, "An error occurred while trying to calling APIConfig \"get segregation codes\" API. ", e);
} catch (IllegalArgumentException e) {
response = request.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder().status(400).title("Invalid domain").detail(e.getMessage()).build())
.header(Constants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
logger.log(Level.SEVERE, "An error occurred while get the service URL mapping from passed domain. ", e);
}
return response;
}

public EnrollingService getEnrollingService(Logger logger) {
long start = Calendar.getInstance().getTimeInMillis();
HttpClient httpClient = HttpClient.newHttpClient();
logger.log(Level.INFO, () -> String.format("Generated a new stub for HTTP Client in [%d] ms", Calendar.getInstance().getTimeInMillis() - start));
return new EnrollingService(logger, httpClient, apiconfigPath, apiconfigSubkey);
}
}
24 changes: 24 additions & 0 deletions src/main/java/it/gov/pagopa/authorizer/model/CIAssociatedCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package it.gov.pagopa.authorizer.model;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

/** Code associated with Creditor Institution */
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class CIAssociatedCode {

@JsonProperty("code")
private String code;

@JsonProperty("name")
private String stationName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package it.gov.pagopa.authorizer.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

import java.util.List;

/** Codes associated with Creditor Institution */
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class CIAssociatedCodeList {

@JsonProperty("used")
private List<CIAssociatedCode> usedCodes;

@JsonProperty("unused")
private List<CIAssociatedCode> unusedCodes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package it.gov.pagopa.authorizer.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

import java.util.List;


@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class EnrolledCreditorInstitution {

@JsonProperty("organization_fiscal_code")
private String organizationFiscalCode;

@JsonProperty("segregation_codes")
private List<String> segregationCodes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package it.gov.pagopa.authorizer.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

import java.util.List;

@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class EnrolledCreditorInstitutions {
@JsonProperty("creditor_institutions")
private List<EnrolledCreditorInstitution> creditorInstitutions;
}
33 changes: 33 additions & 0 deletions src/main/java/it/gov/pagopa/authorizer/model/ProblemJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package it.gov.pagopa.authorizer.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
* Object returned as response in case of an error.
* <p> See {@link it.gov.pagopa.debtposition.exception.ErrorHandler}
*/
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProblemJson {

@JsonProperty("title")
private String title;

@JsonProperty("status")
private Integer status;

@JsonProperty("detail")
private String detail;
}
Loading

0 comments on commit c3108d1

Please sign in to comment.