Skip to content

Commit

Permalink
[SELC-6157] feat: Added new endpoint into TokenController
Browse files Browse the repository at this point in the history
  • Loading branch information
giampieroferrara authored Dec 13, 2024
1 parent 5593bc6 commit c58e8c4
Showing 7 changed files with 295 additions and 55 deletions.
45 changes: 45 additions & 0 deletions apps/onboarding-ms/src/main/docs/openapi.json
Original file line number Diff line number Diff line change
@@ -1605,6 +1605,51 @@
} ]
}
},
"/v1/tokens/contract-signed" : {
"put" : {
"tags" : [ "internal-v1" ],
"summary" : "Find an attachment for a given onboarding id and update the contract signed path",
"description" : "Find an attachment for a given onboarding id and update the contract signed path",
"operationId" : "updateContractSigned",
"parameters" : [ {
"name" : "contractSigned",
"in" : "query",
"required" : true,
"schema" : {
"type" : "string"
}
}, {
"name" : "onboardingId",
"in" : "query",
"required" : true,
"schema" : {
"type" : "string"
}
} ],
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"application/json" : {
"schema" : {
"format" : "int64",
"type" : "integer"
}
}
}
},
"401" : {
"description" : "Not Authorized"
},
"403" : {
"description" : "Not Allowed"
}
},
"security" : [ {
"SecurityScheme" : [ ]
} ]
}
},
"/v1/tokens/{onboardingId}/attachment" : {
"get" : {
"tags" : [ "Token Controller" ],
34 changes: 34 additions & 0 deletions apps/onboarding-ms/src/main/docs/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1179,6 +1179,40 @@ paths:
description: Not Allowed
security:
- SecurityScheme: []
/v1/tokens/contract-signed:
put:
tags:
- internal-v1
summary: Find an attachment for a given onboarding id and update the contract
signed path
description: Find an attachment for a given onboarding id and update the contract
signed path
operationId: updateContractSigned
parameters:
- name: contractSigned
in: query
required: true
schema:
type: string
- name: onboardingId
in: query
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
format: int64
type: integer
"401":
description: Not Authorized
"403":
description: Not Allowed
security:
- SecurityScheme: []
/v1/tokens/{onboardingId}/attachment:
get:
tags:
Original file line number Diff line number Diff line change
@@ -6,15 +6,20 @@
import it.pagopa.selfcare.onboarding.mapper.TokenMapper;
import it.pagopa.selfcare.onboarding.service.TokenService;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.*;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import java.io.File;
import java.util.List;
import lombok.AllArgsConstructor;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.jboss.resteasy.reactive.RestResponse;

import java.io.File;
import java.util.List;

@Authenticated
@Path("/v1/tokens")
@AllArgsConstructor
@@ -67,4 +72,18 @@ public Uni<RestResponse<File>> getAttachment(@PathParam(value = "onboardingId")
@NotNull @QueryParam(value = "name") String attachmentName){
return tokenService.retrieveAttachment(onboardingId, attachmentName);
}

@Operation(
summary = "Find an attachment for a given onboarding id and update the contract signed path",
description = "Find an attachment for a given onboarding id and update the contract signed path"
)
@PUT
@Tag(name = "internal-v1")
@Produces(MediaType.APPLICATION_JSON)
@Path("/contract-signed")
public Uni<Long> updateContractSigned(@NotNull @QueryParam(value = "onboardingId") String onboardingId,
@NotNull @QueryParam(value = "contractSigned") String contractSigned) {
return tokenService.updateContractSigned(onboardingId, contractSigned);
}

}
Original file line number Diff line number Diff line change
@@ -2,10 +2,9 @@

import io.smallrye.mutiny.Uni;
import it.pagopa.selfcare.onboarding.entity.Token;
import org.jboss.resteasy.reactive.RestResponse;

import java.io.File;
import java.util.List;
import org.jboss.resteasy.reactive.RestResponse;

public interface TokenService {

@@ -15,5 +14,7 @@ public interface TokenService {

Uni<RestResponse<File>> retrieveAttachment(String onboardingId, String attachmentName);

Uni<Long> updateContractSigned(String onboardingId, String documentSignedPath);

Uni<List<String>> getAttachments(String onboardingId);
}
Original file line number Diff line number Diff line change
@@ -7,18 +7,26 @@
import it.pagopa.selfcare.azurestorage.AzureBlobClient;
import it.pagopa.selfcare.onboarding.conf.OnboardingMsConfig;
import it.pagopa.selfcare.onboarding.entity.Token;
import it.pagopa.selfcare.onboarding.exception.InvalidRequestException;
import it.pagopa.selfcare.onboarding.util.QueryUtils;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.core.MediaType;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import org.bson.Document;
import org.jboss.resteasy.reactive.RestResponse;

@ApplicationScoped
public class TokenServiceDefault implements TokenService {
private final AzureBlobClient azureBlobClient;
private final OnboardingMsConfig onboardingMsConfig;

private static final String ONBOARDING_NOT_FOUND_OR_ALREADY_DELETED =
"Token with id %s not found or already deleted";

public TokenServiceDefault(AzureBlobClient azureBlobClient, OnboardingMsConfig onboardingMsConfig) {
this.azureBlobClient = azureBlobClient;
this.onboardingMsConfig = onboardingMsConfig;
@@ -60,14 +68,36 @@ public Uni<RestResponse<File>> retrieveAttachment(String onboardingId, String at
}

@Override
public Uni<Long> updateContractSigned(String onboardingId, String documentSignedPath) {

Map<String, Object> queryParameter = new HashMap<>();
queryParameter.put("contractSigned", documentSignedPath);
Document query = QueryUtils.buildUpdateDocument(queryParameter);

return Token.update(query)
.where("_id", onboardingId)
.onItem()
.transformToUni(
updateItemCount -> {
if (updateItemCount == 0) {
return Uni.createFrom()
.failure(
new InvalidRequestException(
String.format(ONBOARDING_NOT_FOUND_OR_ALREADY_DELETED, onboardingId)));
}
return Uni.createFrom().item(updateItemCount);
});
}

public Uni<List<String>> getAttachments(String onboardingId) {
return Token.find("onboardingId = ?1 and type = ?2", onboardingId, ATTACHMENT.name())
.stream().onItem().transform(Token.class::cast)
.map(Token::getName)
.collect().asList();
.stream().onItem().transform(Token.class::cast)
.map(Token::getName)
.collect().asList();
}

private String getAttachmentByOnboarding(String onboardingId, String filename) {
return String.format("%s%s%s%s",onboardingMsConfig.getContractPath(), onboardingId, "/attachments", "/" + filename);
return String.format("%s%s%s%s", onboardingMsConfig.getContractPath(), onboardingId, "/attachments", "/" + filename);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package it.pagopa.selfcare.onboarding.controller;

import static io.restassured.RestAssured.given;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;

import io.quarkus.test.InjectMock;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.common.http.TestHTTPEndpoint;
@@ -9,17 +14,15 @@
import io.restassured.http.ContentType;
import io.smallrye.mutiny.Uni;
import it.pagopa.selfcare.onboarding.entity.Token;
import it.pagopa.selfcare.onboarding.exception.InvalidRequestException;
import it.pagopa.selfcare.onboarding.service.TokenService;
import jakarta.ws.rs.core.MediaType;
import org.jboss.resteasy.reactive.RestResponse;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.List;
import java.util.UUID;

import static io.restassured.RestAssured.given;
import static org.mockito.Mockito.*;
import org.jboss.resteasy.reactive.RestResponse;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

@QuarkusTest
@TestHTTPEndpoint(TokenController.class)
@@ -37,15 +40,15 @@ void getToken() {
Token token = new Token();
token.setId(UUID.randomUUID().toString());
when(tokenService.getToken(onboardingId))
.thenReturn(Uni.createFrom().item(List.of(token)));
.thenReturn(Uni.createFrom().item(List.of(token)));

given()
.when()
.contentType(ContentType.JSON)
.queryParams("onboardingId", onboardingId)
.get()
.then()
.statusCode(200);
.when()
.contentType(ContentType.JSON)
.queryParams("onboardingId", onboardingId)
.get()
.then()
.statusCode(200);
}

@Test
@@ -54,14 +57,14 @@ void getContract() {
final String onboardingId = "onboardingId";
RestResponse.ResponseBuilder<File> response = RestResponse.ResponseBuilder.ok();
when(tokenService.retrieveContractNotSigned(onboardingId))
.thenReturn(Uni.createFrom().item(response.build()));
.thenReturn(Uni.createFrom().item(response.build()));

given()
.when()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.get("/{onboardingId}/contract", onboardingId)
.then()
.statusCode(200);
.when()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.get("/{onboardingId}/contract", onboardingId)
.then()
.statusCode(200);
}

@Test
@@ -71,26 +74,79 @@ void getAttachment() {
final String filename = "filename";
RestResponse.ResponseBuilder<File> response = RestResponse.ResponseBuilder.ok();
when(tokenService.retrieveAttachment(onboardingId, filename))
.thenReturn(Uni.createFrom().item(response.build()));
.thenReturn(Uni.createFrom().item(response.build()));

given()
.when()
.queryParam("name", filename)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.get("/{onboardingId}/attachment", onboardingId)
.then()
.statusCode(200);
.when()
.queryParam("name", filename)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.get("/{onboardingId}/attachment", onboardingId)
.then()
.statusCode(200);
}

@Test
@TestSecurity(user = "userJwt")
void getAttachmentBadRequest() {
final String onboardingId = "onboardingId";
given()
.when()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.get("/{onboardingId}/attachment", onboardingId)
.then()
.statusCode(400);
.when()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.get("/{onboardingId}/attachment", onboardingId)
.then()
.statusCode(400);
}

@Test
@TestSecurity(user = "userJwt")
void updateContractSignedTest_OK() {
// given
final String onboardingId = "onboardingId";
final String contractSigned = "contractSigned";

Uni<Long> response = Uni.createFrom().item(Long.valueOf(1));
when(tokenService.updateContractSigned(onboardingId, contractSigned)).thenReturn(response);

// when
given()
.when()
.queryParam("onboardingId", onboardingId)
.queryParam("contractSigned", contractSigned)
.contentType(MediaType.APPLICATION_JSON)
.put("/contract-signed")
.then()
.statusCode(200);

// then
Mockito.verify(tokenService, times(1)).updateContractSigned(anyString(), anyString());
}

@Test
@TestSecurity(user = "userJwt")
void updateContractSignedTest_KO() {
// given
final String onboardingId = "onboardingId";
final String contractSigned = "contractSigned";

Uni<Long> response = Uni.createFrom()
.failure(
new InvalidRequestException(
String.format("Error %S", onboardingId)));

when(tokenService.updateContractSigned(onboardingId, contractSigned)).thenReturn(response);

// when
given()
.when()
.queryParam("onboardingId", onboardingId)
.queryParam("contractSigned", contractSigned)
.contentType(MediaType.APPLICATION_JSON)
.put("/contract-signed")
.then()
.statusCode(400);

// then
Mockito.verify(tokenService, times(1)).updateContractSigned(anyString(), anyString());
}

}
Loading

0 comments on commit c58e8c4

Please sign in to comment.