From 86d82b99c0ed26296a34f0dd16058f8000ca6bc8 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Wed, 26 Jul 2017 12:08:14 +0200
Subject: [PATCH] serialization issue when chemical didn't have casId defined

---
 .../java/lcsb/mapviewer/api/BaseRestImpl.java |  6 +-
 .../projects/chemicals/ChemicalRestImpl.java  | 12 ++--
 .../chemicals/ChemicalRestImplTest.java       | 67 +++++++++++++++++++
 .../api/projects/drugs/DrugRestImplTest.java  |  2 +-
 4 files changed, 80 insertions(+), 7 deletions(-)
 create mode 100644 rest-api/src/test/java/lcsb/mapviewer/api/projects/chemicals/ChemicalRestImplTest.java

diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/BaseRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/BaseRestImpl.java
index 635f9985de..57882b3194 100644
--- a/rest-api/src/main/java/lcsb/mapviewer/api/BaseRestImpl.java
+++ b/rest-api/src/main/java/lcsb/mapviewer/api/BaseRestImpl.java
@@ -75,8 +75,8 @@ public abstract class BaseRestImpl {
 	};
 
 	protected Map<String, Object> createAnnotation(MiriamData annotation) {
-		Map<String, Object> result = new HashMap<>();
 		if (annotation != null && annotation.getDataType() != null) {
+			Map<String, Object> result = new HashMap<>();
 			if (annotation.getDataType().getUris().size() > 0) {
 				try {
 					result.put("link", miriamConnector.getUrlString(annotation));
@@ -95,8 +95,10 @@ public abstract class BaseRestImpl {
 			result.put("type", annotation.getDataType().name());
 			result.put("resource", annotation.getResource());
 			result.put("id", annotation.getId());
+			return result;
+		} else {
+			throw new InvalidArgumentException("invalid miriam data: " + annotation);
 		}
-		return result;
 	};
 
 	protected Map<String, Object> createAnnotation(AnnotationView annotation) {
diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/chemicals/ChemicalRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/chemicals/ChemicalRestImpl.java
index 702cb56cb2..949e985122 100644
--- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/chemicals/ChemicalRestImpl.java
+++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/chemicals/ChemicalRestImpl.java
@@ -120,7 +120,7 @@ public class ChemicalRestImpl extends BaseRestImpl {
 		this.modelService = modelService;
 	}
 
-	private Map<String, Object> prepareChemical(Chemical chemical, Set<String> columnsSet, List<Model> models) {
+	protected Map<String, Object> prepareChemical(Chemical chemical, Set<String> columnsSet, List<Model> models) {
 		Map<String, Object> result = new HashMap<>();
 
 		String description = "Mesh term not available";
@@ -144,14 +144,18 @@ public class ChemicalRestImpl extends BaseRestImpl {
 			} else if (column.equals("references")) {
 				List<Map<String, Object>> references = new ArrayList<>();
 				references.add(createAnnotation(chemical.getChemicalId()));
-				references.add(createAnnotation(chemical.getCasID()));
+				if (chemical.getCasID() != null) {
+					references.add(createAnnotation(chemical.getCasID()));
+				}
 				value = references;
 			} else if (column.equals("directevidencereferences")) {
 				value = createAnnotations(chemical.getDirectEvidencePublication());
 			} else if (column.equals("description")) {
 				value = description;
 			} else if (column.equals("directevidence")) {
-				value = chemical.getDirectEvidence().getValue();
+				if (chemical.getDirectEvidence() != null) {
+					value = chemical.getDirectEvidence().getValue();
+				}
 			} else if (column.equals("synonyms")) {
 				value = synonyms;
 			} else if (column.equals("targets")) {
@@ -164,7 +168,7 @@ public class ChemicalRestImpl extends BaseRestImpl {
 		return result;
 	}
 
-	private Set<String> createChemicalColumnSet(String columns) {
+	protected Set<String> createChemicalColumnSet(String columns) {
 		Set<String> columnsSet = new HashSet<>();
 		if (columns.equals("")) {
 			columnsSet.add("name");
diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/projects/chemicals/ChemicalRestImplTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/projects/chemicals/ChemicalRestImplTest.java
new file mode 100644
index 0000000000..86df7fbcaa
--- /dev/null
+++ b/rest-api/src/test/java/lcsb/mapviewer/api/projects/chemicals/ChemicalRestImplTest.java
@@ -0,0 +1,67 @@
+package lcsb.mapviewer.api.projects.chemicals;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+
+import java.util.ArrayList;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import lcsb.mapviewer.annotation.data.Chemical;
+import lcsb.mapviewer.annotation.data.ChemicalDirectEvidence;
+import lcsb.mapviewer.annotation.data.ChemicalDirectEvidenceTest;
+import lcsb.mapviewer.api.RestTestFunctions;
+import lcsb.mapviewer.model.map.MiriamData;
+import lcsb.mapviewer.model.map.MiriamType;
+import lcsb.mapviewer.model.map.model.Model;
+import lcsb.mapviewer.services.interfaces.IModelService;
+
+public class ChemicalRestImplTest extends RestTestFunctions {
+	Logger									 logger	= Logger.getLogger(ChemicalRestImplTest.class);
+
+	@Autowired
+
+	private ChemicalRestImpl _drugRestImpl;
+
+	@AfterClass
+	public static void tearDownAfterClass() throws Exception {
+	}
+
+	@Before
+	public void setUp() throws Exception {
+	}
+
+	@After
+	public void tearDown() throws Exception {
+	}
+
+	@Test
+	public void testPrepareChemical() throws Exception {
+		try {
+			Chemical chemical = new Chemical();
+			chemical.setChemicalId(new MiriamData(MiriamType.MESH_2012, "D010300"));
+			Map<String, Object> result = _drugRestImpl.prepareChemical(chemical, _drugRestImpl.createChemicalColumnSet(""), new ArrayList<>());
+			assertNotNull(result);
+		} catch (Exception e) {
+			e.printStackTrace();
+			throw e;
+		}
+	}
+
+	protected ChemicalRestImpl createMockChemicalRest(String string) throws Exception {
+		Model model = super.getModelForFile(string, true);
+		IModelService mockModelService = Mockito.mock(IModelService.class);
+		Mockito.when(mockModelService.getLastModelByProjectId(anyString(), any())).thenReturn(model);
+		_drugRestImpl.setModelService(mockModelService);
+		return _drugRestImpl;
+	}
+
+}
diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/projects/drugs/DrugRestImplTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/projects/drugs/DrugRestImplTest.java
index ecdad6a033..554b08329a 100644
--- a/rest-api/src/test/java/lcsb/mapviewer/api/projects/drugs/DrugRestImplTest.java
+++ b/rest-api/src/test/java/lcsb/mapviewer/api/projects/drugs/DrugRestImplTest.java
@@ -40,7 +40,7 @@ public class DrugRestImplTest extends RestTestFunctions {
 
 	@Test
 	@SuppressWarnings("unchecked")
-	public void testGetModelAsImage() throws Exception {
+	public void testGetDrugsByQuery() throws Exception {
 		try {
 			DrugRestImpl drugRestImpl = createMockProjectRest("testFiles/model/sample.xml");
 			Map<String, Object> result = drugRestImpl.getDrugsByQuery(token.getId(), "sample", "", "nadh").get(0);
-- 
GitLab