controls in api method

This commit is contained in:
Michele Artini 2021-06-29 09:15:49 +02:00
parent 707588d69d
commit 6342ae829d
3 changed files with 21 additions and 34 deletions

View File

@ -33,22 +33,34 @@ public class DirectIndexController extends AbstractDnetController {
private XmlRecordConverter xmlRecordConverter;
@PostMapping("/")
public String feedResult(@RequestBody final ResultEntry pub) throws DirectIndexApiException {
log.debug(pub);
public String feedResult(@RequestBody final ResultEntry r) throws DirectIndexApiException {
log.debug(r);
final RecordInfo info = new RecordInfo();
if (StringUtils.isNotBlank(pub.getOpenaireId())) {
info.setId(pub.getOpenaireId());
info.setOperation("UPDATE");
} else if (StringUtils.isNoneBlank(pub.getOriginalId(), pub.getCollectedFromId())) {
info.setId(xmlRecordConverter.calculateOpenaireId(pub.getOriginalId(), pub.getCollectedFromId()));
if (StringUtils.isNotBlank(r.getOpenaireId())) {
if (r.getOpenaireId().matches("^\\w{12}::\\w{32}$")) {
info.setId(r.getOpenaireId());
info.setOperation("UPDATE");
} else {
throw new DirectIndexApiException("Invalid openaireId: " + r.getOpenaireId() + " - regex ^\\w{12}::\\w{32}$ not matched");
}
} else if (StringUtils.isNoneBlank(r.getOriginalId(), r.getCollectedFromId())) {
final String openaireId = xmlRecordConverter.calculateOpenaireId(r.getOriginalId(), r.getCollectedFromId());
r.setOpenaireId(openaireId);
info.setId(openaireId);
info.setOperation("INSERT");
} else {
throw new DirectIndexApiException("Missing identifier fields: [openaireId] or [originalId, collectedFromId]");
}
if (StringUtils.isBlank(r.getTitle())) { throw new DirectIndexApiException("A required field is missing: title"); }
if (StringUtils.isBlank(r.getUrl())) { throw new DirectIndexApiException("A required field is missing: url"); }
if (StringUtils.isBlank(r.getAccessRightCode())) { throw new DirectIndexApiException("A required field is missing: accessRightCode"); }
if (StringUtils.isBlank(r.getResourceType())) { throw new DirectIndexApiException("A required field is missing: resourceType"); }
if (StringUtils.isBlank(r.getCollectedFromId())) { throw new DirectIndexApiException("A required field is missing: collectedFromId"); }
if (StringUtils.isBlank(r.getType())) { throw new DirectIndexApiException("A required field is missing: type"); }
info.setBody(pub.toJson());
info.setType(pub.getType());
info.setBody(r.toJson());
info.setType(r.getType());
info.setCreatedBy("TODO"); // TODO
info.setCreationDate(OffsetDateTime.now());
info.setExecutionDate(null);

View File

@ -39,27 +39,9 @@ public class XmlRecordConverter {
public String convert(final ResultEntry r) throws DirectIndexApiException {
if (StringUtils.isBlank(r.getOriginalId()) && StringUtils.isBlank(r.getOpenaireId())) {
throw new DirectIndexApiException("One of the following fields is required: originalId or openaireId");
}
if (StringUtils.isBlank(r.getTitle())) { throw new DirectIndexApiException("A required field is missing: title"); }
if (StringUtils.isBlank(r.getUrl())) { throw new DirectIndexApiException("A required field is missing: url"); }
if (StringUtils.isBlank(r.getAccessRightCode())) { throw new DirectIndexApiException("A required field is missing: accessRightCode"); }
if (StringUtils.isBlank(r.getResourceType())) { throw new DirectIndexApiException("A required field is missing: resourceType"); }
if (StringUtils.isBlank(r.getCollectedFromId())) { throw new DirectIndexApiException("A required field is missing: collectedFromId"); }
if (StringUtils.isBlank(r.getType())) { throw new DirectIndexApiException("A required field is missing: type"); }
final DatasourceEntry collectedFromEntry = getDatasourceInfo(r.getCollectedFromId());
final DatasourceEntry hostedByEntry = getDatasourceInfo(r.getHostedById());
if (StringUtils.isBlank(r.getOpenaireId())) {
r.setOpenaireId(calculateOpenaireId(r.getOriginalId(), collectedFromEntry));
}
if (!r.getOpenaireId().matches("^\\w{12}::\\w{32}$")) {
throw new DirectIndexApiException("Invalid openaireId: " + r.getOpenaireId() + " - regex ^\\w{12}::\\w{32}$ not matched");
}
final VelocityContext vc = new VelocityContext();
vc.put("esc", (Function<String, String>) s -> StringEscapeUtils.escapeXml11(s));
vc.put("util", new OpenAIRESubmitterUtils(community_api));

View File

@ -1,6 +1,5 @@
package eu.dnetlib.openaire.directindex.mapping;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@ -28,7 +27,6 @@ import org.mockito.quality.Strictness;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.openaire.directindex.DirectIndexApiException;
import eu.dnetlib.openaire.directindex.input.DatasourceEntry;
import eu.dnetlib.openaire.directindex.input.PidEntry;
import eu.dnetlib.openaire.directindex.input.ResultEntry;
@ -230,11 +228,6 @@ public class XmlRecordConverterTest {
}
@Test
public void testAsIndexRecord_wrongOpenaireId() throws Exception {
assertThrows(DirectIndexApiException.class, () -> testAsIndexRecord_json("test_record_wrong_openaireId.json"));
}
@Test
public void testAsIndexRecord_json_zenodo() throws Exception {