Commit 4cd51fb4 authored by Matija Obreza's avatar Matija Obreza

Merge branch '518-complex-dois-404' into 'master'

Resolve "Complex DOIs 404"

Closes #518

See merge request genesys-pgr/genesys-server!545
parents b25e9d70 2bc4d534
......@@ -29,6 +29,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
......@@ -73,6 +74,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -164,13 +166,13 @@ public class AccessionController {
/**
* Gets the accession
*
* @param uuid the uuid
* @return the subset
*/
@RequestMapping(value = "/10.{doi1:[0-9]+}/{doi2:.+}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
@GetMapping(value = "/10.{doi1:[0-9]+}/**", produces = { MediaType.APPLICATION_JSON_VALUE })
@JsonView({ JsonViews.Protected.class })
public Accession getByDoi(@PathVariable("doi1") String doi1, @PathVariable("doi2") String doi2) {
return accessionService.getByDoi("10." + doi1 + "/" + doi2);
public Accession getByDoi(final HttpServletRequest request) {
final String doi = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/").length());
return accessionService.getByDoi(doi);
}
/**
......@@ -292,10 +294,12 @@ public class AccessionController {
return accessionService.toUUID(identifiers);
}
@RequestMapping(value = "/details/10.{doi1:[0-9]+}/{doi2:.+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "/details/10.{doi1:[0-9]+}/**", produces = MediaType.APPLICATION_JSON_VALUE)
@JsonView(JsonViews.Root.class)
public AccessionService.AccessionDetails getAccessionDetailsByDoi(@PathVariable("doi1") String doi1, @PathVariable("doi2") String doi2) {
Accession accession = accessionService.getByDoi("10." + doi1 + "/" + doi2);
public AccessionService.AccessionDetails getAccessionDetailsByDoi(final HttpServletRequest request) {
final String doi = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/details/").length());
Accession accession = accessionService.getByDoi(doi);
return accessionService.getAccessionDetails(accession);
}
......@@ -307,10 +311,11 @@ public class AccessionController {
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/auditlog/10.{doi1:[0-9]+}/{doi2:.+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "/auditlog/10.{doi1:[0-9]+}/**", produces = MediaType.APPLICATION_JSON_VALUE)
@JsonView(JsonViews.Root.class)
public AccessionService.AccessionAuditLog getAccessionAuditLogByDoi(@PathVariable("doi1") String doi1, @PathVariable("doi2") String doi2) {
Accession accession = accessionService.getByDoi("10." + doi1 + "/" + doi2);
public AccessionService.AccessionAuditLog getAccessionAuditLogByDoi(final HttpServletRequest request) {
final String doi = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/auditlog/").length());
Accession accession = accessionService.getByDoi(doi);
return accessionService.getAuditLog(accession);
}
......
......@@ -73,7 +73,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
......@@ -156,14 +158,13 @@ public class AccessionController extends ApiBaseController {
/**
* Gets the accession
*
* @param doi1 the doi
* @param doi2 the doi
* @return the subset
*/
@RequestMapping(value = "/10.{doi1:[0-9]+}/{doi2:.+}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
@GetMapping(value = "/10.{doi1:[0-9]+}/**", produces = { MediaType.APPLICATION_JSON_VALUE })
@JsonView({ JsonViews.Protected.class })
public Accession getByDoi(@PathVariable("doi1") String doi1, @PathVariable("doi2") String doi2) {
return accessionService.getByDoi("10." + doi1 + "/" + doi2);
public Accession getByDoi(final HttpServletRequest request) {
final String doi = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/").length());
return accessionService.getByDoi(doi);
}
/**
......@@ -286,10 +287,12 @@ public class AccessionController extends ApiBaseController {
return accessionService.toUUID(identifiers);
}
@RequestMapping(value = "/details/10.{doi1:[0-9]+}/{doi2:.+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "/details/10.{doi1:[0-9]+}/**", produces = MediaType.APPLICATION_JSON_VALUE)
@JsonView(JsonViews.Root.class)
public AccessionService.AccessionDetails getAccessionDetailsByDoi(@PathVariable("doi1") String doi1, @PathVariable("doi2") String doi2) {
Accession accession = accessionService.getByDoi("10." + doi1 + "/" + doi2);
public AccessionService.AccessionDetails getAccessionDetailsByDoi(final HttpServletRequest request) {
final String doi = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/details/").length());
Accession accession = accessionService.getByDoi(doi);
return accessionService.getAccessionDetails(accession);
}
......@@ -301,10 +304,11 @@ public class AccessionController extends ApiBaseController {
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/auditlog/10.{doi1:[0-9]+}/{doi2:.+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "/auditlog/10.{doi1:[0-9]+}/**", produces = MediaType.APPLICATION_JSON_VALUE)
@JsonView(JsonViews.Root.class)
public AccessionService.AccessionAuditLog getAccessionAuditLogByDoi(@PathVariable("doi1") String doi1, @PathVariable("doi2") String doi2) {
Accession accession = accessionService.getByDoi("10." + doi1 + "/" + doi2);
public AccessionService.AccessionAuditLog getAccessionAuditLogByDoi(final HttpServletRequest request) {
final String doi = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/auditlog/").length());
Accession accession = accessionService.getByDoi(doi);
return accessionService.getAuditLog(accession);
}
......
......@@ -25,10 +25,7 @@ import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
......@@ -1186,7 +1183,7 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
private Accession fetch(UUID uuid) throws Exception, JsonProcessingException {
/*@formatter:off*/
String responseBody = mockMvc.perform(get(org.genesys2.server.api.v1.AccessionController.CONTROLLER_URL+ "/" + uuid))
String responseBody = mockMvc.perform(get(AccessionController.CONTROLLER_URL+ "/" + uuid))
.andExpect(status().isOk())
// .andDo(MockMvcResultHandlers.print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
......@@ -1237,4 +1234,80 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
}
}
}
private Accession fetch(String doi) throws Exception, JsonProcessingException {
/*@formatter:off*/
String responseBody = mockMvc.perform(get(AccessionController.CONTROLLER_URL+ "/{doi}", doi))
.andExpect(status().isOk())
// .andDo(MockMvcResultHandlers.print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.institute.code", notNullValue()))
.andExpect(jsonPath("$.accessionNumber", notNullValue()))
.andExpect(jsonPath("$.doi", equalTo(doi)))
.andReturn().getResponse().getContentAsString();
/*@formatter:on*/
// Test fetch details
mockMvc.perform(get(AccessionController.CONTROLLER_URL+ "/details/{doi}", doi))
.andExpect(status().isOk());
return objectMapper.readValue(responseBody, Accession.class);
}
@Test
public void getAccessionBySimpleDoi() throws Exception {
ObjectNode accessionJson = setUpAccession();
// GLIS DOI format
String glisDoi = "10.12345/1231*";
accessionJson.put("doi", glisDoi);
upsert(accessionJson);
assertThat(accessionRepository.count(), is(1L));
Accession accession = fetch(glisDoi);
assertThat(accession, notNullValue());
assertThat(accession.getDoi(), is(glisDoi));
}
@Test
public void getAccessionByComplexDoi() throws Exception {
ObjectNode accessionJson = setUpAccession();
// IPK DOI format
String complexDoi = "10.25642/AAA1/GRAC/0005324";
accessionJson.put("doi", complexDoi);
upsert(accessionJson);
assertThat(accessionRepository.count(), is(1L));
Accession accession = fetch(complexDoi);
assertThat(accession, notNullValue());
assertThat(accession.getDoi(), is(complexDoi));
}
@Test
public void getAccessionAuditLog() throws Exception {
ObjectNode accessionJson = setUpAccession();
upsert(accessionJson);
// IPK DOI format
String complexDoi = "10.25642/AAA1/GRAC/0005324";
accessionJson.put("doi", complexDoi);
upsert(accessionJson);
assertThat(accessionRepository.count(), is(1L));
Accession accession = fetch(complexDoi);
assertThat(accession, notNullValue());
assertThat(accession.getDoi(), is(complexDoi));
/*@formatter:off*/
mockMvc.perform(get(org.genesys2.server.api.v1.AccessionController.CONTROLLER_URL+ "/auditlog/{doi}", complexDoi))
.andExpect(status().isOk())
// .andDo(MockMvcResultHandlers.print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.auditAccession").isMap());
/*@formatter:on*/
}
}
......@@ -18,40 +18,22 @@ package org.genesys.test.server.api.v2;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.oneOf;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.io.StringReader;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Sets;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import org.apache.commons.lang3.RandomUtils;
import org.genesys.test.server.api.AbstractAccessionControllerTest;
import org.genesys2.server.api.v2.AccessionController;
......@@ -68,6 +50,14 @@ import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Sets;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
/**
* @author Matija Obreza
* @author Maxym Borodenko
......@@ -1126,5 +1116,81 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
}
}
}
private Accession fetch(String doi) throws Exception, JsonProcessingException {
/*@formatter:off*/
String responseBody = mockMvc.perform(get(AccessionController.CONTROLLER_URL+ "/{doi}", doi))
.andExpect(status().isOk())
// .andDo(MockMvcResultHandlers.print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.institute.code", notNullValue()))
.andExpect(jsonPath("$.accessionNumber", notNullValue()))
.andExpect(jsonPath("$.doi", equalTo(doi)))
.andReturn().getResponse().getContentAsString();
/*@formatter:on*/
// Test fetch details
mockMvc.perform(get(AccessionController.CONTROLLER_URL+ "/details/{doi}", doi))
.andExpect(status().isOk());
return objectMapper.readValue(responseBody, Accession.class);
}
@Test
public void getAccessionBySimpleDoi() throws Exception {
ObjectNode accessionJson = setUpAccession();
// GLIS DOI format
String glisDoi = "10.12345/1231*";
accessionJson.put("doi", glisDoi);
upsert(accessionJson);
assertThat(accessionRepository.count(), is(1L));
Accession accession = fetch(glisDoi);
assertThat(accession, notNullValue());
assertThat(accession.getDoi(), is(glisDoi));
}
@Test
public void getAccessionByComplexDoi() throws Exception {
ObjectNode accessionJson = setUpAccession();
// IPK DOI format
String complexDoi = "10.25642/AAA1/GRAC/0005324";
accessionJson.put("doi", complexDoi);
upsert(accessionJson);
assertThat(accessionRepository.count(), is(1L));
Accession accession = fetch(complexDoi);
assertThat(accession, notNullValue());
assertThat(accession.getDoi(), is(complexDoi));
}
@Test
public void getAccessionAuditLog() throws Exception {
ObjectNode accessionJson = setUpAccession();
upsert(accessionJson);
// IPK DOI format
String complexDoi = "10.25642/AAA1/GRAC/0005324";
accessionJson.put("doi", complexDoi);
upsert(accessionJson);
assertThat(accessionRepository.count(), is(1L));
Accession accession = fetch(complexDoi);
assertThat(accession, notNullValue());
assertThat(accession.getDoi(), is(complexDoi));
/*@formatter:off*/
mockMvc.perform(get(AccessionController.CONTROLLER_URL+ "/auditlog/{doi}", complexDoi))
.andExpect(status().isOk())
// .andDo(MockMvcResultHandlers.print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.auditAccession").isMap());
/*@formatter:on*/
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment