Skip to content
Snippets Groups Projects
Commit f76ecb97 authored by I. Nüske's avatar I. Nüske
Browse files

ENH: Added deletion of FDOs

- expanded API and Application to support FDO deletion
- unit test for deletion
parent 1bc4d4b0
Branches
No related tags found
1 merge request!5Draft: Permanent Delete (Service)
Pipeline #53946 failed
...@@ -247,6 +247,33 @@ paths: ...@@ -247,6 +247,33 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Error' $ref: '#/components/schemas/Error'
delete:
tags:
- FDOs
description: "Delete an FDO."
operationId: deleteFDO
parameters:
- $ref: '#/components/parameters/Prefix'
- $ref: '#/components/parameters/Suffix'
responses:
"200":
description: "Content of deleted FDO."
content:
application/json:
schema:
type: object
required: ["data"]
properties:
data:
$ref: '#/components/schemas/DigitalObject'
links:
$ref: '#/components/schemas/Links'
"404":
description: "Could not resolve pid."
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components: components:
schemas: schemas:
OperationsLogRecord: OperationsLogRecord:
......
...@@ -107,6 +107,24 @@ public class FDOApiImpl extends BaseController implements FdoApi { ...@@ -107,6 +107,24 @@ public class FDOApiImpl extends BaseController implements FdoApi {
} }
} }
@Override
public ResponseEntity<ResolvePID200Response> deleteFDO(String prefix, String suffix) {
try (Manager manager = getManager()) {
String pid = prefix + "/" + suffix;
DigitalObject fdo = manager.resolvePID(pid);
boolean success = manager.deleteFDO(pid);
if (success) {
return ResponseEntity.ok(createResponse(fdo));
} else {
throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR, "Could not delete PID " + pid);
}
} catch (PidUnresolvableException e) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Not found. Could not resolve PID " + e.getPid());
}
}
@Override @Override
public ResponseEntity<ResolvePID200Response> resolvePID(String prefix, String suffix) { public ResponseEntity<ResolvePID200Response> resolvePID(String prefix, String suffix) {
String pid = prefix + "/" + suffix; String pid = prefix + "/" + suffix;
......
package com.indiscale.fdo.manager.service.fdo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.gson.JsonObject;
import com.indiscale.fdo.manager.api.DigitalObject;
import com.indiscale.fdo.manager.api.FDO;
import com.indiscale.fdo.manager.api.Manager;
import com.indiscale.fdo.manager.api.PidUnresolvableException;
import com.indiscale.fdo.manager.mock.MockManager;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.server.ResponseStatusException;
/** Test the {@link FDOApiImpl} class */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FDOApiTest {
private static final Manager mockitoManager = mock(MockManager.class);
public static class TestFDOApiImpl extends FDOApiImpl {
@Override
public Manager getManager() {
return mockitoManager;
}
}
private final FDOApiImpl api = new FDOApiTest.TestFDOApiImpl();
@Test
void testFDOApiWithMockitoManager() throws PidUnresolvableException {
// Test FDOs
DigitalObject mockDO =
new DigitalObject() {
@Override
public String getPID() {
return "prefix/suffix";
}
@Override
public JsonObject getAttributes() {
return null;
}
@Override
public boolean isFDO() {
return false;
}
@Override
public FDO toFDO() {
return null;
}
};
DigitalObject mockDO2 =
new DigitalObject() {
@Override
public String getPID() {
return "suffix/prefix";
}
@Override
public JsonObject getAttributes() {
return null;
}
@Override
public boolean isFDO() {
return false;
}
@Override
public FDO toFDO() {
return null;
}
};
// Configure Test returns, commented out returns are there so that each set of returns has same
// index.
when(mockitoManager.resolvePID("prefix/suffix"))
.thenReturn(mockDO)
.thenReturn(mockDO2)
.thenThrow(PidUnresolvableException.class)
.thenReturn(mockDO)
.thenReturn(mockDO);
when(mockitoManager.deleteFDO("prefix/suffix"))
.thenReturn(true)
.thenReturn(true)
// .thenReturn(true) will not be called as PidUnresolvableException was thrown
.thenReturn(true)
.thenReturn(false);
// 1,2,4 should pass as resolvePID returns successfully and deleteFDO returns true
// 3 should fail because resolvePID throws PidUnresolvableException
// 5 should fail because deleteFDO returns false
// 6 should fail because the mockitoManager is not configured for "random/string"
assertEquals(api.deleteFDO("prefix", "suffix").getBody().getData().getPid(), "prefix/suffix");
assertEquals(api.deleteFDO("prefix", "suffix").getBody().getData().getPid(), "suffix/prefix");
assertThrows(ResponseStatusException.class, () -> api.deleteFDO("prefix", "suffix"));
assertEquals(api.deleteFDO("prefix", "suffix").getBody().getData().getPid(), "prefix/suffix");
assertThrows(ResponseStatusException.class, () -> api.deleteFDO("prefix", "suffix"));
assertThrows(ResponseStatusException.class, () -> api.deleteFDO("random", "string"));
// resolvePID("prefix/suffix") is called from 1,2,3,4,5
// resolvePID("random/string") is called from 6
// deleteFDO("prefix/suffix") is called from 1,2,4,5
verify(mockitoManager, times(5)).resolvePID("prefix/suffix");
verify(mockitoManager, times(1)).resolvePID("random/string");
verify(mockitoManager, times(4)).deleteFDO("prefix/suffix");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment