Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions api-svc/raid-api/docker-compose/mockserver/expectations.json
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,27 @@
}
}
},
{
"priority": 10,
"httpRequest": {
"method": "POST",
"path": "/dois",
"body": {
"type": "STRING",
"string": "RAID-731-DATACITE-ERROR",
"subString": true
}
},
"httpResponse": {
"statusCode": 429,
"headers": {
"Content-Type": ["application/json"]
},
"body": {
"errors": [{"status": "429", "title": "Too Many Requests"}]
}
}
},
{
"httpRequest": {
"method": "POST",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,46 @@
package au.org.raid.inttest;

import feign.FeignException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

public class DataciteErrorIntegrationTest extends AbstractIntegrationTest {

private static final String MOCKSERVER_EXPECTATION_URL = "http://localhost:1080/mockserver/expectation";
private static final String EXPECTATION_ID = "datacite-error-integration-test";

@Autowired
private RestTemplate restTemplate;
private static final String MOCKSERVER_HOST = "localhost";
private static final int MOCKSERVER_PORT = 1080;
private static final int MOCKSERVER_CONNECT_TIMEOUT_MILLIS = 2000;

@AfterEach
void cleanUpMockServerExpectation() {
final var cleanup = """
[{
"id": "%s",
"priority": 0,
"httpRequest": {
"method": "POST",
"path": "/dois"
},
"httpResponse": {
"statusCode": 201
},
"times": {
"remainingTimes": 0,
"unlimited": false
}
}]
""".formatted(EXPECTATION_ID);
// Sentinel matched by the static 429 expectation in docker-compose/mockserver/expectations.json
private static final String DATACITE_ERROR_TITLE_SENTINEL = "RAID-731-DATACITE-ERROR";

var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
@BeforeEach
void checkMockServerIsReachable() {
Assumptions.assumeTrue(isMockServerReachable(), "mockserver not reachable - skipping DataCite error test");
}

try {
restTemplate.put(MOCKSERVER_EXPECTATION_URL, new HttpEntity<>(cleanup, headers));
} catch (Exception ignored) {
private static boolean isMockServerReachable() {
try (var socket = new Socket()) {
socket.connect(new InetSocketAddress(MOCKSERVER_HOST, MOCKSERVER_PORT), MOCKSERVER_CONNECT_TIMEOUT_MILLIS);
return true;
} catch (IOException e) {
return false;
}
}

@Test
@DisplayName("Mint fails when DataCite returns an error")
void mintFailsOnDataciteError() {
createDataciteErrorExpectation();
createRequest.getTitle().get(0).setText(DATACITE_ERROR_TITLE_SENTINEL + " " + UUID.randomUUID());

try {
raidApi.mintRaid(createRequest);
Expand All @@ -62,38 +49,4 @@ void mintFailsOnDataciteError() {
assertThat(e.status()).isEqualTo(500);
}
}

private void createDataciteErrorExpectation() {
final var expectation = """
[{
"id": "%s",
"priority": 10,
"httpRequest": {
"method": "POST",
"path": "/dois",
"headers": {
"Authorization": ["Basic ZGF0YWNpdGUtdXNlcm5hbWU6ZGF0YWNpdGUtcGFzc3dvcmQ="],
"Content-Type": ["application/json"]
}
},
"httpResponse": {
"statusCode": 429,
"headers": {
"Content-Type": ["application/json"]
},
"body": {
"errors": [{"status": "429", "title": "Too Many Requests"}]
}
},
"times": {
"remainingTimes": 1,
"unlimited": false
}
}]
""".formatted(EXPECTATION_ID);

var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
restTemplate.put(MOCKSERVER_EXPECTATION_URL, new HttpEntity<>(expectation, headers));
}
}
Loading