All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.unit.nva.testutils.HandlerRequestBuilder Maven / Gradle / Ivy

The newest version!
package no.unit.nva.testutils;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import nva.commons.apigateway.AccessRight;
import nva.commons.apigateway.AccessRightEntry;
import nva.commons.core.JacocoGenerated;

@JacocoGenerated
@SuppressWarnings("PMD.GodClass")
public class HandlerRequestBuilder {

    public static final String DELIMITER = System.lineSeparator();
    public static final String AUTHORIZER_NODE = "authorizer";
    public static final String CLAIMS_NODE = "claims";
    public static final String USER_NAME_CLAIM = "custom:nvaUsername";
    public static final String GROUPS_CLAIM = "cognito:groups";
    public static final String APPLICATION_ROLES_CLAIM = "custom:applicationRoles";
    public static final String PERSON_CRISTIN_ID = "custom:cristinId";
    public static final String CUSTOMER_ID = "custom:customerId";
    public static final String FEIDE_ID_CLAIM = "custom:feideId";
    public static final String ENTRIES_DELIMITER = ",";
    public static final String SCOPE_CLAIM = "scope";
    public static final String ISS_CLAIM = "iss";
    public static final String CLIENT_ID_CLAIM = "client_id";
    public static final String PERSON_NIN_CLAIM = "custom:nin";
    private static final String TOP_LEVEL_ORG_CRISTIN_ID_CLAIM = "custom:topOrgCristinId";
    private final ObjectMapper objectMapper;
    @JsonProperty("body")
    private String body;
    @JsonProperty("headers")
    private Map headers;
    @JsonProperty("queryStringParameters")
    private Map queryParameters;
    @JsonProperty("multiValueQueryStringParameters")
    private Map> multiValueQueryParameters;
    @JsonProperty("pathParameters")
    private Map pathParameters;
    @JsonProperty("requestContext")
    private ObjectNode requestContext;
    @JsonProperty("httpMethod")
    private String httpMethod;
    @JsonAnySetter
    private Map otherProperties;

    public HandlerRequestBuilder(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
        this.otherProperties = new LinkedHashMap<>();
    }

    public static String toString(InputStream inputStream) {
        return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
                   .lines()
                   .collect(Collectors.joining(DELIMITER));
    }

    public HandlerRequestBuilder withBody(T body) throws JsonProcessingException {
        if (body instanceof String) {
            this.body = (String) body;
        } else {
            this.body = objectMapper.writeValueAsString(body);
        }
        return this;
    }

    public HandlerRequestBuilder withHeaders(Map headers) {
        this.headers = headers;
        return this;
    }

    public HandlerRequestBuilder withQueryParameters(Map queryParameters) {
        this.queryParameters = queryParameters;
        return this;
    }

    public HandlerRequestBuilder withMultiValueQueryParameters(Map> multiValueQueryParameters) {
        this.multiValueQueryParameters = multiValueQueryParameters;
        return this;
    }

    public HandlerRequestBuilder withPathParameters(Map pathParameters) {
        this.pathParameters = pathParameters;
        return this;
    }

    public HandlerRequestBuilder withRequestContext(ObjectNode requestContext) {
        this.requestContext = requestContext;
        return this;
    }

    public HandlerRequestBuilder withHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
        return this;
    }

    /**
     * @param requestContext the requestContext object.
     * @return the builder.
     * @deprecated Use the method that accepts an {@link ObjectNode} as a parameter.
     */
    @Deprecated(since = "1.25.0")
    public HandlerRequestBuilder withRequestContext(Map requestContext) {
        this.requestContext = objectMapper.convertValue(requestContext, ObjectNode.class);
        return this;
    }

    public HandlerRequestBuilder withOtherProperties(Map otherProperties) {
        this.otherProperties.putAll(otherProperties);
        return this;
    }

    public InputStream build() throws JsonProcessingException {
        return new ByteArrayInputStream(objectMapper.writeValueAsBytes(this));
    }

    public APIGatewayProxyRequestEvent buildRequestEvent() throws JsonProcessingException {
        var json = objectMapper.writeValueAsString(this);
        return objectMapper.readValue(json, APIGatewayProxyRequestEvent.class);
    }

    public T getBody(TypeReference typeRef) throws JsonProcessingException {
        if (nonNull(body)) {
            return objectMapper.readValue(body, typeRef);
        }
        return null;
    }

    public Map getHeaders() {
        return headers;
    }

    public Map getQueryParameters() {
        return queryParameters;
    }

    public Map> getMultiValueQueryParameters() {
        return multiValueQueryParameters;
    }

    public Map getPathParameters() {
        return pathParameters;
    }

    public Map getRequestContext() {
        JavaType mapType = objectMapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
        return objectMapper.convertValue(requestContext, mapType);
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    @JsonAnyGetter
    public Map getOtherProperties() {
        return otherProperties;
    }

    public void setOtherProperties(Map otherProperties) {
        this.otherProperties = otherProperties;
    }

    public HandlerRequestBuilder withUserName(String userName) {
        ObjectNode claims = getAuthorizerClaimsNode();
        claims.put(USER_NAME_CLAIM, userName);
        return this;
    }

    @Deprecated(since = "1.25.5")
    @JacocoGenerated
    public HandlerRequestBuilder withNvaUsername(String nvaUsername) {
        return withUserName(nvaUsername);
    }

    public HandlerRequestBuilder withCurrentCustomer(URI customerId) {
        ObjectNode claims = getAuthorizerClaimsNode();
        claims.put(CUSTOMER_ID, customerId.toString());
        return this;
    }

    public HandlerRequestBuilder withAuthorizerClaim(String claimName, String claimValue) {
        var authorizerClaimsNode = getAuthorizerClaimsNode();
        authorizerClaimsNode.put(claimName, claimValue);
        return this;
    }

    public HandlerRequestBuilder withTopLevelCristinOrgId(URI topLevelCristinOrgId) {
        ObjectNode claims = getAuthorizerClaimsNode();
        claims.put(TOP_LEVEL_ORG_CRISTIN_ID_CLAIM, topLevelCristinOrgId.toString());
        return this;
    }

    public HandlerRequestBuilder withPersonCristinId(URI personCristinId) {
        ObjectNode claims = getAuthorizerClaimsNode();
        claims.put(PERSON_CRISTIN_ID, personCristinId.toString());
        return this;
    }

    public HandlerRequestBuilder withPersonNin(String personNin) {
        ObjectNode claims = getAuthorizerClaimsNode();
        claims.put(PERSON_NIN_CLAIM, personNin);
        return this;
    }

    public HandlerRequestBuilder withFeideId(String feideId) {
        ObjectNode claims = getAuthorizerClaimsNode();
        claims.put(FEIDE_ID_CLAIM, feideId);
        return this;
    }

    public HandlerRequestBuilder withRoles(String roles) {
        ObjectNode claims = getAuthorizerClaimsNode();
        claims.put(APPLICATION_ROLES_CLAIM, roles);
        return this;
    }

    public HandlerRequestBuilder withAccessRights(URI customerId, AccessRight... accessRights) {
        for (AccessRight accessRight : accessRights) {
            var accessRightEntry = new AccessRightEntry(accessRight, customerId);
            addAccessRightToCognitoGroups(accessRightEntry);
        }
        return this;
    }

    public HandlerRequestBuilder withRequestContextValue(String propertyName, String value) {
        initializeRequestContextIfNotExists();
        requestContext.put(propertyName, value);
        return this;
    }

    public HandlerRequestBuilder withScope(String scope) {
        var authorizerClaims = getAuthorizerClaimsNode();
        authorizerClaims.put(SCOPE_CLAIM, scope);
        return this;
    }

    public HandlerRequestBuilder withIssuer(String issuer) {
        var authorizerClaims = getAuthorizerClaimsNode();
        authorizerClaims.put(ISS_CLAIM, issuer);
        return this;
    }

    public HandlerRequestBuilder withClientId(String clientId) {
        var authorizerClaims = getAuthorizerClaimsNode();
        authorizerClaims.put(CLIENT_ID_CLAIM, clientId);
        return this;
    }

    private void addAccessRightToCognitoGroups(AccessRightEntry accessRight) {
        var claims = getAuthorizerClaimsNode();
        appendAccessRightClaimToAccessRightClaims(claims, accessRight);
    }

    private void appendAccessRightClaimToAccessRightClaims(ObjectNode claims, AccessRightEntry accessRight) {
        var existingAccessRights = extractAccessRights(claims);
        existingAccessRights.add(accessRight);
        var newClaim = existingAccessRights.stream()
                           .map(AccessRightEntry::toString)
                           .collect(Collectors.joining(ENTRIES_DELIMITER));
        claims.put(GROUPS_CLAIM, newClaim);
    }
    private Collection extractAccessRights(ObjectNode claims) {
        return claims.has(GROUPS_CLAIM)
                   ? AccessRightEntry.fromCsv(claims.get(GROUPS_CLAIM).textValue()).collect(Collectors.toList())
                   : new ArrayList<>();
    }

    private ObjectNode getAuthorizerClaimsNode() {
        ObjectNode authorizerNode = populateAuthorizerNode();
        var claimsNode = getChildNode(authorizerNode, CLAIMS_NODE);
        authorizerNode.set(CLAIMS_NODE, claimsNode);
        return claimsNode;
    }

    private ObjectNode getChildNode(ObjectNode parentNode, String childNodeName) {
        return Optional.ofNullable(parentNode)
                   .map(parent -> parent.get(childNodeName))
                   .filter(JsonNode::isObject)
                   .map(ObjectNode.class::cast)
                   .orElse(objectMapper.createObjectNode());
    }

    private void initializeRequestContextIfNotExists() {
        if (isNull(requestContext)) {
            requestContext = objectMapper.createObjectNode();
        }
    }

    private ObjectNode populateAuthorizerNode() {
        initializeRequestContextIfNotExists();
        ObjectNode authorizerNode = getChildNode(requestContext, AUTHORIZER_NODE);
        requestContext.set(AUTHORIZER_NODE, authorizerNode);
        return authorizerNode;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy