Commit 09e2b39f authored by Piotr Gawron's avatar Piotr Gawron
Browse files

IMeshService implemented

parent b9f1ed7b
package lcsb.mapviewer.api.mesh;
import java.util.Map;
import java.util.TreeMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.annotation.data.MeSH;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.interfaces.IMeshService;
@RestController
@RequestMapping(value = "/api/mesh", produces = MediaType.APPLICATION_JSON_VALUE)
......@@ -21,16 +29,26 @@ public class MeshController extends BaseController {
@SuppressWarnings("unused")
private Logger logger = LogManager.getLogger();
private MeshRestImpl meshRest;
private IMeshService meshService;
@Autowired
public MeshController(MeshRestImpl meshRest) {
this.meshRest = meshRest;
public MeshController(IMeshService meshService) {
this.meshService = meshService;
}
@GetMapping(value = "/{id:.+}")
public Map<String, Object> getMesh(@PathVariable(value = "id") String id)
throws AnnotatorException, ObjectNotFoundException {
return meshRest.getMesh(id);
MeSH mesh = meshService.getMesh(new MiriamData(MiriamType.MESH_2012, id));
if (mesh == null) {
throw new ObjectNotFoundException("Object not found: " + id);
}
Map<String, Object> result = new TreeMap<>();
result.put("name", mesh.getName());
result.put("id", mesh.getMeSHId());
result.put("description", mesh.getDescription());
result.put("synonyms", mesh.getSynonyms());
return result;
}
}
\ No newline at end of file
......@@ -4,13 +4,11 @@ import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import lcsb.mapviewer.api.mesh.AllMeshTests;
import lcsb.mapviewer.api.projects.AllProjectTests;
import lcsb.mapviewer.api.users.AllUserTests;
@RunWith(Suite.class)
@SuiteClasses({
AllMeshTests.class,
AllProjectTests.class,
AllUserTests.class,
})
......
package lcsb.mapviewer.api.mesh;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ MeshRestImplTest.class })
public class AllMeshTests {
}
package lcsb.mapviewer.api.mesh;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.api.RestTestFunctions;
import lcsb.mapviewer.services.ObjectNotFoundException;
public class MeshRestImplTest extends RestTestFunctions {
Logger logger = LogManager.getLogger();
@Autowired
MeshRestImpl meshRestImpl;
@Test
public void test() throws ObjectNotFoundException, AnnotatorException {
Map<String, Object> result = meshRestImpl.getMesh("D010300");
assertNotNull(result);
String name = (String) result.get("name");
assertTrue(name.toLowerCase().contains("parkinson"));
}
}
package lcsb.mapviewer.api.mesh;
package lcsb.mapviewer.services.impl;
import java.util.Map;
import java.util.TreeMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -12,39 +7,23 @@ import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.data.MeSH;
import lcsb.mapviewer.annotation.services.MeSHParser;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.interfaces.IMeshService;
@Transactional
@Service
public class MeshRestImpl extends BaseRestImpl {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = LogManager.getLogger();
public class MeshService implements IMeshService {
private MeSHParser meSHParser;
@Autowired
public MeshRestImpl(MeSHParser meSHParser) {
public MeshService(MeSHParser meSHParser) {
this.meSHParser = meSHParser;
}
public Map<String, Object> getMesh(String id) throws AnnotatorException, ObjectNotFoundException {
MeSH mesh = meSHParser.getMeSH(new MiriamData(MiriamType.MESH_2012, id));
if (mesh == null) {
throw new ObjectNotFoundException("Object not found: " + id);
}
Map<String, Object> result = new TreeMap<>();
result.put("name", mesh.getName());
result.put("id", mesh.getMeSHId());
result.put("description", mesh.getDescription());
result.put("synonyms", mesh.getSynonyms());
return result;
@Override
public MeSH getMesh(MiriamData meshID) throws AnnotatorException {
return meSHParser.getMeSH(meshID);
}
}
package lcsb.mapviewer.services.interfaces;
import lcsb.mapviewer.annotation.data.MeSH;
import lcsb.mapviewer.annotation.services.annotators.AnnotatorException;
import lcsb.mapviewer.model.map.MiriamData;
public interface IMeshService {
MeSH getMesh(MiriamData miriamData) throws AnnotatorException;
}
package lcsb.mapviewer.web;
import static org.junit.Assert.assertTrue;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
......@@ -8,18 +9,29 @@ import static org.springframework.restdocs.request.RequestDocumentation.paramete
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringJUnit4ClassRunner.class)
public class MeshControllerIntegrationTest extends ControllerIntegrationTest {
Logger logger = LogManager.getLogger();
@Autowired
private ObjectMapper objectMapper;
@Before
public void setup() {
}
......@@ -31,9 +43,9 @@ public class MeshControllerIntegrationTest extends ControllerIntegrationTest {
@Test
public void testGetTaxonomies() throws Exception {
RequestBuilder request = get("/api/mesh/{meshId}", "D002095");
RequestBuilder request = get("/api/mesh/{meshId}", "D010300");
mockMvc.perform(request)
String response = mockMvc.perform(request)
.andDo(document("mesh/get_mesh",
pathParameters(parameterWithName("meshId").description("mesh id")),
responseFields(
......@@ -49,7 +61,14 @@ public class MeshControllerIntegrationTest extends ControllerIntegrationTest {
fieldWithPath("id")
.description("mesh id")
.type("string"))))
.andExpect(status().is2xxSuccessful());
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
Map<String, Object> result = objectMapper.readValue(response, new TypeReference<Map<String, Object>>() {
});
String name = (String) result.get("name");
assertTrue(name.toLowerCase().contains("parkinson"));
}
}
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