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
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ public class DatasetDownloadEnums {

@Getter
public enum Condition {
UUID("UUID"),
START_DATE("START_DATE"),
END_DATE("END_DATE"),
MIN_LATITUDE("MIN_LAT"),
MAX_LATITUDE("MAX_LAT"),
MIN_LONGITUDE("MIN_LON"),
MAX_LONGITUDE("MAX_LON"),
RECIPIENT("RECIPIENT");
UUID("uuid"),
START_DATE("start_date"),
END_DATE("end_date"),
MULTI_POLYGON("multi_polygon"),
RECIPIENT("recipient");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import au.org.aodn.ogcapi.processes.model.ProcessList;
import au.org.aodn.ogcapi.processes.model.Results;
import au.org.aodn.ogcapi.server.core.model.InlineValue;
import au.org.aodn.ogcapi.server.core.model.enumeration.DatasetDownloadEnums;
import au.org.aodn.ogcapi.server.core.model.enumeration.ProcessIdEnum;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
Expand Down Expand Up @@ -44,14 +45,11 @@ public ResponseEntity<InlineResponse200> execute(
if (processID.equals(ProcessIdEnum.DOWNLOAD_DATASET.getValue())) {
try {
var response = restServices.downloadData(
(String) body.getInputs().get("collectionId"),
(String) body.getInputs().get("start_date"),
(String) body.getInputs().get("end_date"),
(String) body.getInputs().get("min_lat"),
(String) body.getInputs().get("min_lon"),
(String) body.getInputs().get("max_lat"),
(String) body.getInputs().get("max_lon"),
(String) body.getInputs().get("recipient")
(String) body.getInputs().get(DatasetDownloadEnums.Condition.UUID.getValue()),
(String) body.getInputs().get(DatasetDownloadEnums.Condition.START_DATE.getValue()),
(String) body.getInputs().get(DatasetDownloadEnums.Condition.END_DATE.getValue()),
body.getInputs().get(DatasetDownloadEnums.Condition.MULTI_POLYGON.getValue()),
(String) body.getInputs().get(DatasetDownloadEnums.Condition.RECIPIENT.getValue())
);

var value = new InlineValue(response.getBody());
Expand All @@ -61,6 +59,8 @@ public ResponseEntity<InlineResponse200> execute(
return ResponseEntity.ok(results);

} catch (Exception e) {

// TODO: currently all the errors return badRequest. This should be changed to return the correct status code
log.error(e.getMessage());
var response = new Results();
var value = new InlineValue("Error while getting dataset");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package au.org.aodn.ogcapi.server.processes;

import au.org.aodn.ogcapi.server.core.model.enumeration.DatasetDownloadEnums;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import software.amazon.awssdk.services.batch.BatchClient;
import software.amazon.awssdk.services.batch.model.*;
import software.amazon.awssdk.services.batch.model.SubmitJobRequest;
import software.amazon.awssdk.services.batch.model.SubmitJobResponse;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
Expand All @@ -28,22 +27,15 @@ public ResponseEntity<String> downloadData(
String id,
String startDate,
String endDate,
String minLat,
String minLon,
String maxLat,
String maxLon,
Object polygons,
String recipient
) {
try {
) throws JsonProcessingException {

Map<String, String> parameters = new HashMap<>();
parameters.put(DatasetDownloadEnums.Condition.UUID.getValue(), id);
parameters.put(DatasetDownloadEnums.Condition.START_DATE.getValue(), startDate);
parameters.put(DatasetDownloadEnums.Condition.END_DATE.getValue(), endDate);
parameters.put(DatasetDownloadEnums.Condition.MIN_LATITUDE.getValue(), minLat);
parameters.put(DatasetDownloadEnums.Condition.MIN_LONGITUDE.getValue(), minLon);
parameters.put(DatasetDownloadEnums.Condition.MAX_LATITUDE.getValue(), maxLat);
parameters.put(DatasetDownloadEnums.Condition.MAX_LONGITUDE.getValue(), maxLon);
parameters.put(DatasetDownloadEnums.Condition.MULTI_POLYGON.getValue(), objectMapper.writeValueAsString(polygons));
parameters.put(DatasetDownloadEnums.Condition.RECIPIENT.getValue(), recipient);


Expand All @@ -54,68 +46,18 @@ public ResponseEntity<String> downloadData(
parameters);
log.info("Job submitted with ID: " + jobId);
return ResponseEntity.ok("Job submitted with ID: " + jobId);
} catch (Exception e) {

log.error("Error while getting dataset");
log.error(e.getMessage());
return ResponseEntity.badRequest().body("Error while getting dataset");
}
}

private String submitJob(String jobName, String jobQueue, String jobDefinition, Map<String, String> parameters) {

List<KeyValuePair> environmentVariables = parameters.entrySet().stream()
.map(entry ->
KeyValuePair
.builder()
.name(entry.getKey())
.value(entry.getValue())
.build()
)
.toList();

SubmitJobRequest submitJobRequest = SubmitJobRequest.builder()
.jobName(jobName)
.jobQueue(jobQueue)
.jobDefinition(jobDefinition)
.parameters(parameters)
.containerOverrides(co -> co.environment(environmentVariables))
.build();

SubmitJobResponse submitJobResponse = batchClient.submitJob(submitJobRequest);
return submitJobResponse.jobId();
}



// TODO: This feature doesn't work yet. Will be implemented in the future as this one is not urgent
public boolean isJobQueueValid(String jobQueueName) throws IOException {

var remoteJobQueueDetail = getRemoteJobQueueBy(jobQueueName);
var localJobQueueDetail = getLocalJobQueueDetailBy(jobQueueName);

return remoteJobQueueDetail.equals(localJobQueueDetail);
}

public JobQueueDetail getRemoteJobQueueBy(String name) {
var request = DescribeJobQueuesRequest
.builder()
.jobQueues(name)
.build();

var jobQueues = batchClient.describeJobQueues(request).jobQueues();

if (jobQueues != null && jobQueues.size() == 1) {
return jobQueues.get(0);
}
return null;
}

public JobQueueDetail getLocalJobQueueDetailBy(String jobQueueName) throws IOException {
var configJsonPath = "server/src/main/java/au/org/aodn/ogcapi/server/processes/config/" + jobQueueName + ".json";
var jsonFile = new File(configJsonPath);
var jsonStr = objectMapper.writeValueAsString(objectMapper.readValue(jsonFile, Object.class));
return objectMapper.readValue(jsonStr, JobQueueDetail.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import au.org.aodn.ogcapi.processes.model.InlineResponse200;
import au.org.aodn.ogcapi.processes.model.Results;
import au.org.aodn.ogcapi.server.core.model.InlineValue;
import au.org.aodn.ogcapi.server.core.model.enumeration.DatasetDownloadEnums;
import au.org.aodn.ogcapi.server.core.model.enumeration.ProcessIdEnum;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -17,7 +19,8 @@
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -35,39 +38,38 @@ public class RestApiTest {
public void setUp() {
executeRequest = new Execute();
Map<String, Object> inputs = new HashMap<>();
inputs.put("collectionId", "test-collection-id");
inputs.put("start_date", "2023-01-01");
inputs.put("end_date", "2023-01-31");
inputs.put("min_lat", "-10.0");
inputs.put("min_lon", "110.0");
inputs.put("max_lat", "10.0");
inputs.put("max_lon", "150.0");
inputs.put("recipient", "test@example.com");
inputs.put(DatasetDownloadEnums.Condition.UUID.getValue(), "test-uuid");
inputs.put(DatasetDownloadEnums.Condition.START_DATE.getValue(), "2023-01-01");
inputs.put(DatasetDownloadEnums.Condition.END_DATE.getValue(), "2023-01-31");
inputs.put(DatasetDownloadEnums.Condition.MULTI_POLYGON.getValue(), "test-multipolygon");
inputs.put(DatasetDownloadEnums.Condition.RECIPIENT.getValue(), "test@example.com");
executeRequest.setInputs(inputs);
}

@Test
public void testExecuteDownloadDatasetSuccess() {
when(restServices.downloadData(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
public void testExecuteDownloadDatasetSuccess() throws JsonProcessingException {
when(restServices.downloadData(any(), any(), any(), any(), any()))
.thenReturn(ResponseEntity.ok("Job submitted with ID: test-job-id"));

ResponseEntity<InlineResponse200> response = restApi.execute(ProcessIdEnum.DOWNLOAD_DATASET.getValue(), executeRequest);

assertEquals(200, response.getStatusCode().value());
assertInstanceOf(Results.class, response.getBody());
Results results = (Results) response.getBody();
assert results != null;
InlineValue message = (InlineValue) results.get("message");
assertEquals("Job submitted with ID: test-job-id", message.message());
}

@Test
public void testExecuteDownloadDatasetError() {
when(restServices.downloadData(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
public void testExecuteDownloadDatasetError() throws JsonProcessingException {
when(restServices.downloadData(any(), any(), any(), any(), any()))
.thenThrow(new RuntimeException("Error while getting dataset"));

ResponseEntity<InlineResponse200> response = restApi.execute(ProcessIdEnum.DOWNLOAD_DATASET.getValue(), executeRequest);

assertEquals(400, response.getStatusCode().value());
assertInstanceOf(Results.class, response.getBody());
Results results = (Results) response.getBody();
assert results != null;
InlineValue error = (InlineValue) results.get("error");
Expand All @@ -79,6 +81,7 @@ public void testExecuteUnknownProcessId() {
ResponseEntity<InlineResponse200> response = restApi.execute("unknown-process-id", executeRequest);

assertEquals(400, response.getStatusCode().value());
assertInstanceOf(Results.class, response.getBody());
Results results = (Results) response.getBody();
assert results != null;
InlineValue error = (InlineValue) results.get("error");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package au.org.aodn.ogcapi.server.processes;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
Expand All @@ -19,41 +22,50 @@ public class RestServicesTest {
@Mock
private BatchClient batchClient;

@Mock
private ObjectMapper objectMapper;

@InjectMocks
private RestServices restServices;

private AutoCloseable closeableMock;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
closeableMock = MockitoAnnotations.openMocks(this);
}

@AfterEach void cleanUp() throws Exception {
closeableMock.close();
}

@Test
public void testDownloadDataSuccess() {
public void testDownloadDataSuccess() throws JsonProcessingException {
// Arrange
String jobId = "12345";
SubmitJobResponse submitJobResponse = SubmitJobResponse.builder().jobId(jobId).build();
when(batchClient.submitJob(any(SubmitJobRequest.class))).thenReturn(submitJobResponse);
when(objectMapper.writeValueAsString(any())).thenReturn("test-multipolygon");

// Act
ResponseEntity<String> response = restServices.downloadData(
"id", "2021-01-01", "2021-01-31", "10.0", "20.0", "30.0", "40.0", "recipient@example.com");
"test-uuid", "2023-01-01", "2023-01-31", "test-multipolygon", "test@example.com");

// Assert
assertEquals(ResponseEntity.ok("Job submitted with ID: " + jobId), response);
verify(batchClient, times(1)).submitJob(any(SubmitJobRequest.class));
}

@Test
public void testDownloadDataFailure() {
public void testDownloadDataJsonProcessingException() throws JsonProcessingException {
// Arrange
when(batchClient.submitJob(any(SubmitJobRequest.class))).thenThrow(new RuntimeException("AWS Batch error"));
when(objectMapper.writeValueAsString(any())).thenThrow(new JsonProcessingException("Error") {});

// Act
ResponseEntity<String> response = restServices.downloadData(
"id", "2021-01-01", "2021-01-31", "10.0", "20.0", "30.0", "40.0", "recipient@example.com");

// Assert
assertEquals(ResponseEntity.badRequest().body("Error while getting dataset"), response);
verify(batchClient, times(1)).submitJob(any(SubmitJobRequest.class));
// Act & Assert
try {
restServices.downloadData("test-uuid", "2023-01-01", "2023-01-31", "test-multipolygon", "test@example.com");
} catch (JsonProcessingException e) {
assertEquals("Error", e.getMessage());
}
}
}