Skip to content

Commit

Permalink
CONTINT-685 Allow specifying request definition in a resource file.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpage-alfresco committed Dec 5, 2024
1 parent 19e5d98 commit 3964e56
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import com.atlassian.oai.validator.model.SimpleRequest;
import org.junit.jupiter.api.Test;

import org.alfresco.hxi_connector.live_ingester.util.insight_api.HxInsightRequest;
import org.alfresco.hxi_connector.live_ingester.util.insight_api.RequestLoader;

public class OpenApiRequestValidationTest
{

Expand All @@ -43,65 +46,28 @@ public class OpenApiRequestValidationTest
@Test
void testRequestToPresignedUrls()
{
Request request = SimpleRequest.Builder.post("/v1/presigned-urls")
.withHeader("authorization", "string")
.withHeader("content-type", "application/json")
.withHeader("hxp-environment", "string")
.withHeader("user-agent", "string")
.withHeader("count", "string")
.build();
HxInsightRequest hxInsightRequest = RequestLoader.load("/expected-hxinsight-requests/get-presigned-url-request.yml");

Request request = makeRequest(hxInsightRequest);

assertThat(classUnderTest.validateRequest(request).getMessages()).isEqualTo(Collections.emptyList());
}

@Test
void testRequestToIngestionEvents()
{
Request request = SimpleRequest.Builder.post("/v1/ingestion-events")
.withHeader("authorization", "string")
.withHeader("content-type", "application/json")
.withHeader("hxp-environment", "string")
.withHeader("user-agent", "string")
.withBody(expectedBody)
.build();
HxInsightRequest hxInsightRequest = RequestLoader.load("/expected-hxinsight-requests/create-document-request.yml");

Request request = makeRequest(hxInsightRequest);

// assertThat(classUnderTest.validateRequest(request).getMessages()).isEqualTo(expectedBody);
assertThat(classUnderTest.validateRequest(request).getMessages()).isEqualTo(Collections.emptyList());
}

String expectedBody = """
[
{
"objectId": "d71dd823-82c7-477c-8490-04cb0e826e65",
"sourceId" : "alfresco-dummy-source-id-0a63de491876",
"eventType": "create",
"sourceTimestamp": 1611227656423,
"properties": {
"cm:autoVersion": {"value": true},
"createdAt": {"value": 1611227655695},
"modifiedAt": {"value" : 1611227655695},
"cm:versionType": {"value": "MAJOR"},
"aspectsNames": {"value": ["cm:versionable", "cm:auditable"]},
"cm:name": {
"value": "purchase-order-scan.doc",
"annotation" : "name"
},
"type": {"value": "cm:content"},
"createdBy": {"value": "admin"},
"modifiedBy": {"value": "admin"},
"cm:content": {
"file": {
"content-metadata": {
"size": 531152,
"name": "purchase-order-scan.doc",
"content-type": "application/msword"
}
}
},
"ALLOW_ACCESS": {"value": ["GROUP_EVERYONE"]},
"DENY_ACCESS": {"value": []}
}
}
]""";

private static Request makeRequest(HxInsightRequest hxInsightRequest)
{
SimpleRequest.Builder builder = SimpleRequest.Builder.post(hxInsightRequest.url());
hxInsightRequest.headers().forEach(builder::withHeader);
return builder.withBody(hxInsightRequest.body()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* #%L
* Alfresco HX Insight Connector
* %%
* Copyright (C) 2023 - 2024 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.hxi_connector.live_ingester.util.insight_api;

import java.util.Map;

public record HxInsightRequest(String url, Map<String, String> headers, String body)
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* #%L
* Alfresco HX Insight Connector
* %%
* Copyright (C) 2023 - 2024 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.hxi_connector.live_ingester.util.insight_api;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public class RequestLoader

Check warning

Code scanning / PMD

This utility class has a non-private constructor Warning

This utility class has a non-private constructor
{
static ObjectMapper jsonMapper = new ObjectMapper();

Check warning

Code scanning / PMD

Do not use non-final non-private static fields Warning

Do not use non-final non-private static fields
static ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());

Check warning

Code scanning / PMD

Do not use non-final non-private static fields Warning

Do not use non-final non-private static fields

public static HxInsightRequest load(String path)
{
InputStream stream = HxInsightRequest.class.getResourceAsStream(path);

Check warning

Code scanning / PMD

Ensure that resources like this InputStream object are closed after use Warning

Ensure that resources like this InputStream object are closed after use
HashMap<String, Object> data;

Check warning

Code scanning / PMD

Avoid using implementation types like 'HashMap'; use the interface instead Warning

Avoid using implementation types like 'HashMap'; use the interface instead
try
{
data = yamlMapper.readValue(stream.readAllBytes(), HashMap.class);
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
Object body = data.get("body");
String bodyAsString = null;
if (body != null)
{
try
{
bodyAsString = jsonMapper.writeValueAsString(body);
}
catch (JsonProcessingException e)
{
throw new RuntimeException(e);

Check warning

Code scanning / PMD

Avoid throwing raw exception types. Warning

Avoid throwing raw exception types.
}
}
return new HxInsightRequest((String) data.get("url"), (Map) data.get("headers"), bodyAsString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
url: /v1/ingestion-events
headers:
authorization: application/json
content-type: application/json
hxp-environment: string
user-agent: string
body: [
{
"objectId": "d71dd823-82c7-477c-8490-04cb0e826e65",
"sourceId" : "alfresco-dummy-source-id-0a63de491876",
"eventType": "create",
"sourceTimestamp": 1611227656423,
"properties": {
"cm:autoVersion": {"value": true},
"createdAt": {"value": 1611227655695},
"modifiedAt": {"value" : 1611227655695},
"cm:versionType": {"value": "MAJOR"},
"aspectsNames": {"value": ["cm:versionable", "cm:auditable"]},
"cm:name": {
"value": "purchase-order-scan.doc",
"annotation" : "name"
},
"type": {"value": "cm:content"},
"createdBy": {"value": "admin"},
"modifiedBy": {"value": "admin"},
"cm:content": {
"file": {
"content-metadata": {
"size": 531152,
"name": "purchase-order-scan.doc",
"content-type": "application/msword"
}
}
},
"ALLOW_ACCESS": {"value": ["GROUP_EVERYONE"]},
"DENY_ACCESS": {"value": []}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
url: /v1/presigned-urls
headers:
authorization: string
content-type: application/json
hxp-environment: string
user-agent: string
count: string

0 comments on commit 3964e56

Please sign in to comment.