Skip to content

Commit

Permalink
Modification Bad Request en no content si pas de ppn trouvé
Browse files Browse the repository at this point in the history
Fix sur doi2ppn en cas de ppn non trouvé
  • Loading branch information
pierre-maraval committed Sep 5, 2024
1 parent 157c1f1 commit 4605512
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.abes.sudoc.utils.ExecutionTime;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.ColumnTransformer;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -44,7 +45,7 @@ public String baconProvider035(Integer provider) throws SQLRecoverableException,
}

@ExecutionTime
public String doiToPpn(String doi) throws SQLRecoverableException, UncategorizedSQLException {
public String doiToPpn(String doi) throws UncategorizedSQLException, EmptyResultDataAccessException {
StringBuilder request = new StringBuilder("select a.ppn from autorites.biblio_table_FRBR_0xx a where ");
request.append("upper(SUBSTR(a.datas,1,50)) = '");
request.append(doi.toUpperCase());
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/fr/abes/sudoc/controller/SudocController.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class SudocController {

@ExecutionTime
@GetMapping(value = {"/online_identifier_2_ppn/{type}/{onlineIdentifier}", "/online_identifier_2_ppn/{type}/{onlineIdentifier}/{provider}"}, produces = MediaType.APPLICATION_JSON_VALUE)
public ResultWsDto onlineIdentifier2Ppn(@PathVariable String type, @PathVariable String onlineIdentifier, @PathVariable(required = false) Optional<String> provider) throws IOException, ZoneNotFoundException {
public ResultWsDto onlineIdentifier2Ppn(@PathVariable String type, @PathVariable String onlineIdentifier, @PathVariable(required = false) Optional<String> provider) throws IOException, ZoneNotFoundException, IllegalPpnException {
log.debug("-----------------------------------------------------------");
log.debug("ONLINE IDENTIFIER 2 PPN");
ResultWsDto resultat = new ResultWsDto();
Expand All @@ -64,16 +64,13 @@ public ResultWsDto onlineIdentifier2Ppn(@PathVariable String type, @PathVariable
} catch (IOException ex) {
log.error("erreur dans la récupération de la notice correspondant à l'identifiant " + onlineIdentifier);
throw new IOException(ex);
} catch (IllegalPpnException ex) {
log.warn("Impossible de retrouver une notice correspondant à l'identifiant " + onlineIdentifier);
// throw new IOException(ex); // Pas besoin de throw, il y a juste pas de ppn assosier à cet onlineId
}
return resultat;
}

@ExecutionTime
@GetMapping(value = {"/print_identifier_2_ppn/{type}/{printIdentifier}","/print_identifier_2_ppn/{type}/{printIdentifier}/{provider}"}, produces = MediaType.APPLICATION_JSON_VALUE)
public ResultWsDto printIdentifier2Ppn(@PathVariable String type, @PathVariable String printIdentifier, @PathVariable Optional<String> provider) throws IOException, ZoneNotFoundException {
public ResultWsDto printIdentifier2Ppn(@PathVariable String type, @PathVariable String printIdentifier, @PathVariable Optional<String> provider) throws IOException, ZoneNotFoundException, IllegalPpnException {
log.debug("-----------------------------------------------------------");
log.debug("PRINT IDENTIFIER 2 PPN");
ResultWsDto resultat = new ResultWsDto();
Expand Down Expand Up @@ -127,15 +124,12 @@ public ResultWsDto printIdentifier2Ppn(@PathVariable String type, @PathVariable
} catch (IOException ex) {
log.error("erreur dans la récupération de la notice correspondant à l'identifiant " + printIdentifier);
throw new IOException(ex);
} catch (IllegalPpnException ex){
log.warn("Impossible de retrouver une notice correspondant à l'identifiant " + printIdentifier);
}
return resultat;
}

@ExecutionTime
@GetMapping(value = {"/doi_identifier_2_ppn"}, produces = MediaType.APPLICATION_JSON_VALUE)
public ResultWsDto doiIdentifier2Ppn(@RequestParam(name = "doi") String doi_identifier, @RequestParam(name = "provider") Optional<String> provider) throws IOException {
public ResultWsDto doiIdentifier2Ppn(@RequestParam(name = "doi") String doi_identifier, @RequestParam(name = "provider") Optional<String> provider) throws IOException, IllegalPpnException {
log.debug("DOI IDENTIFIER 2 PPN");
ResultWsDto resultat = new ResultWsDto();
Optional<ElementDto> providerDto = this.providerService.getProviderDisplayName(provider);
Expand All @@ -153,8 +147,6 @@ public ResultWsDto doiIdentifier2Ppn(@RequestParam(name = "doi") String doi_iden
} catch (IOException ex) {
log.error("Erreur dans la récupération de la notice correspondant à l'identifiant");
throw new IOException(ex);
} catch (IllegalPpnException e) {
throw new IOException("Aucun identifiant ne correspond à la notice");
} catch (ZoneNotFoundException e) {
throw new IOException(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ protected ResponseEntity<Object> handleZoneNotFoundException(ZoneNotFoundExcepti
return buildResponseEntity(new ApiReturnError(HttpStatus.BAD_REQUEST, error, ex));
}

@ExceptionHandler(IllegalPpnException.class)
protected ResponseEntity<Object> handleIllegalPpnException(IllegalPpnException ex) {
String warn = ex.getMessage();
log.warn(warn);
return buildResponseEntity(new ApiReturnError(HttpStatus.NO_CONTENT, warn, ex));
}

@ExceptionHandler(IOException.class)
protected ResponseEntity<Object> handleIOException(IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.abes.sudoc.exception;

public class IllegalPpnException extends Throwable {
public class IllegalPpnException extends Exception {
public IllegalPpnException(String s) {
super(s);
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/fr/abes/sudoc/service/DoiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.abes.sudoc.component.BaseXmlFunctionsCaller;
import fr.abes.sudoc.exception.IllegalPpnException;
import fr.abes.sudoc.utils.Utilitaire;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.stereotype.Service;

Expand All @@ -28,16 +29,15 @@ public boolean checkFormat(String doi) {
return doi != null && doi.matches(doiPattern);
}

@Override
public List<String> getPpnFromIdentifiant(String doi) throws IOException, IllegalPpnException {
try {
String ppn = caller.doiToPpn(doi);
if (("").equals(ppn))
throw new IllegalPpnException("Aucune notice ne correspond à la recherche");
else {
return Collections.singletonList(caller.doiToPpn(doi));
}
} catch (SQLRecoverableException ex) {

return Collections.singletonList(caller.doiToPpn(doi));
} catch (UncategorizedSQLException ex) {
throw new IOException("Incident technique lors de l'accès à la base de données");
} catch (EmptyResultDataAccessException ex) {
throw new IllegalPpnException("Aucune notice ne correspond à la recherche");
}
}

Expand Down
39 changes: 19 additions & 20 deletions src/test/java/fr/abes/sudoc/controller/SudocControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -106,7 +105,7 @@ void datToPpnTitreIsNull() throws Exception {

@Test
@DisplayName("datToPpn tryCatchWorks")
void datToPpnTryCatchWorks() throws Exception, IllegalPpnException {
void datToPpnTryCatchWorks() throws Exception {

SearchDatWebDto searchDatRequest = new SearchDatWebDto();
searchDatRequest.setDate(2008);
Expand Down Expand Up @@ -169,7 +168,7 @@ void datToPpnTryCatchNotWorks() throws Exception {

@Test
@DisplayName("test WS online_identifier_2_ppn : serial + ISSN ok + 1 PPN non supprimé de doc élec")
void onlineIdentifier2PpnCas1() throws Exception, IllegalPpnException {
void onlineIdentifier2PpnCas1() throws Exception {
String type = "serial";
String onlineIdentifier = "1234-1234";

Expand Down Expand Up @@ -197,7 +196,7 @@ void onlineIdentifier2PpnCas1() throws Exception, IllegalPpnException {

@Test
@DisplayName("test WS online_identifier_2_ppn : serial + ISSN ok + 2 PPN non supprimés dont un ppn qui n'est pas une notice electronique")
void onlineIdentifier2PpnCas2() throws Exception, IllegalPpnException {
void onlineIdentifier2PpnCas2() throws Exception {
String type = "serial";
String onlineIdentifier = "1234-1234";

Expand Down Expand Up @@ -239,7 +238,7 @@ void onlineIdentifier2PpnCas2() throws Exception, IllegalPpnException {

@Test
@DisplayName("test WS online_identifier_2_ppn : serial + ISSN ok + exception erreur SQL")
void onlineIdentifier2PpnCas3() throws Exception, IllegalPpnException {
void onlineIdentifier2PpnCas3() throws Exception {
String type = "serial";
String onlineIdentifier = "1234-1234";

Expand Down Expand Up @@ -278,7 +277,7 @@ void onlineIdentifier2PpnCas4() throws Exception {

@Test
@DisplayName("test WS online_identifier_2_ppn : check provider non ok")
void onlineIdentifierCheckProviderNonOk() throws Exception, IllegalPpnException {
void onlineIdentifierCheckProviderNonOk() throws Exception {
String type = "serial";
String onlineIdentifier = "1234-1234";
String provider = "CAIRN";
Expand Down Expand Up @@ -309,7 +308,7 @@ void onlineIdentifierCheckProviderNonOk() throws Exception, IllegalPpnException

@Test
@DisplayName("test WS online_identifier_2_ppn : check provider diacritics")
void onlineIdentifierCheckProviderDiacritics() throws Exception, IllegalPpnException {
void onlineIdentifierCheckProviderDiacritics() throws Exception {
String type = "serial";
String onlineIdentifier = "1234-1234";
String provider = "CAèRN";
Expand Down Expand Up @@ -347,7 +346,7 @@ void onlineIdentifierCheckProviderDiacritics() throws Exception, IllegalPpnExcep

@Test
@DisplayName("test WS online_identifier_2_ppn : erreur appel ws provider")
void onlineIdentifer2PpnErreurAppelWs() throws Exception, IllegalPpnException {
void onlineIdentifer2PpnErreurAppelWs() throws Exception {
String type = "serial";
String onlineIdentifier = "1234-1234";
String provider = "CAIRN";
Expand Down Expand Up @@ -386,7 +385,7 @@ void onlineIdentifer2PpnErreurAppelWs() throws Exception, IllegalPpnException {

@Test
@DisplayName("test WS print_identifier_2_ppn : serial + ISSN ok + 1 PPN non supprimé de doc imprimé")
void printIdentifier2PpnCas1() throws Exception, IllegalPpnException {
void printIdentifier2PpnCas1() throws Exception {
String type = "serial";
String printIdentifier = "1234-1234";

Expand Down Expand Up @@ -427,7 +426,7 @@ void printIdentifier2PpnCas0Ppn() throws Exception, IllegalPpnException {
}
@Test
@DisplayName("test WS print_identifier_2_ppn : serial + ISSN ok + 1 PPN supprimé de doc imprimé")
void printIdentifier2PpnCas1Supprime() throws Exception, IllegalPpnException {
void printIdentifier2PpnCas1Supprime() throws Exception {
String type = "serial";
String printIdentifier = "1234-1234";

Expand All @@ -453,7 +452,7 @@ void printIdentifier2PpnCas1Supprime() throws Exception, IllegalPpnException {

@Test
@DisplayName("test WS print_identifier_2_ppn : serial + ISSN ok + 2 PPN non supprimés dont un ppn qui n'est pas une notice imprimée")
void printIdentifier2PpnCas2() throws Exception, IllegalPpnException {
void printIdentifier2PpnCas2() throws Exception {
String type = "serial";
String onlineIdentifier = "1234-1234";

Expand Down Expand Up @@ -498,7 +497,7 @@ void printIdentifier2PpnCas2() throws Exception, IllegalPpnException {

@Test
@DisplayName("test WS print_identifier_2_ppn : serial + ISSN ok + exception erreur SQL")
void printIdentifier2PpnCas3() throws Exception, IllegalPpnException {
void printIdentifier2PpnCas3() throws Exception {
String type = "serial";
String onlineIdentifier = "1234-1234";

Expand Down Expand Up @@ -548,7 +547,7 @@ void printIdentifier2PpnCas5() throws Exception {

@Test
@DisplayName("test WS print_identifer_2_ppn : erreur récupération provider")
void printIdentifer2PpnErreurAppelWs() throws Exception, IllegalPpnException {
void printIdentifer2PpnErreurAppelWs() throws Exception {
String provider = "CAIRN";
String type = "serial";
String printIdentifier = "1234-1234";
Expand Down Expand Up @@ -592,7 +591,7 @@ void printIdentifer2PpnErreurAppelWs() throws Exception, IllegalPpnException {
}

@Test
void doiIdentifier2ppnCasOk() throws Exception, IllegalPpnException {
void doiIdentifier2ppnCasOk() throws Exception {
String doi = "10.1006/jmbi.1998.2354";

String provider = "CAIRN";
Expand Down Expand Up @@ -641,7 +640,7 @@ void doiIdentifier2ppnErreurFormat() throws Exception {
}

@Test
void doiIdentifier2ppnNoPpnFound() throws Exception, IllegalPpnException {
void doiIdentifier2ppnNoPpnFound() throws Exception {
String doi = "10.1006/jmbi.1998.2354";

String provider = "CAIRN";
Expand All @@ -653,13 +652,13 @@ void doiIdentifier2ppnNoPpnFound() throws Exception, IllegalPpnException {
Mockito.when(doiService.getPpnFromIdentifiant(doi)).thenThrow(new IllegalPpnException("Aucune notice ne correspond à la recherche"));

this.mockMvc.perform(get("/api/v1/doi_identifier_2_ppn/?doi=" + doi + "&provider=" + provider))
.andExpect(status().isBadRequest())
.andExpect(result -> Assertions.assertTrue((result.getResolvedException() instanceof IOException)))
.andExpect(result -> Assertions.assertEquals("Aucun identifiant ne correspond à la notice", Objects.requireNonNull(result.getResolvedException()).getMessage()));
.andExpect(status().isNoContent())
.andExpect(result -> Assertions.assertTrue((result.getResolvedException() instanceof IllegalPpnException)))
.andExpect(result -> Assertions.assertEquals("Aucune notice ne correspond à la recherche", Objects.requireNonNull(result.getResolvedException()).getMessage()));
}

@Test
void doiIdentifier2ppnErreurProvider() throws Exception, IllegalPpnException {
void doiIdentifier2ppnErreurProvider() throws Exception {
String doi = "10.1006/jmbi.1998.2354";

String provider = "CAIRN";
Expand Down Expand Up @@ -692,7 +691,7 @@ void doiIdentifier2ppnErreurProvider() throws Exception, IllegalPpnException {
}

@Test
void doiIdentifier2ppnErreurTypeSupportNotice() throws Exception, IllegalPpnException {
void doiIdentifier2ppnErreurTypeSupportNotice() throws Exception {
String doi = "10.1006/jmbi.1998.2354";

String provider = "CAIRN";
Expand Down

0 comments on commit 4605512

Please sign in to comment.