From 3d3cbbaa1a31f3d5cd7ad974d1276c5ac33eb649 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Tue, 17 Sep 2019 20:57:14 +0200
Subject: [PATCH] proper status code is returned when invalid input file is
 passed for conversion

---
 .../api/convert/ConvertRestImpl.java          |  9 +++--
 .../mapviewer/web/AllIntegrationTests.java    |  1 +
 .../web/ConvertControllerIntegrationTest.java | 37 +++++++++++++++++++
 3 files changed, 44 insertions(+), 3 deletions(-)
 create mode 100644 web/src/test/java/lcsb/mapviewer/web/ConvertControllerIntegrationTest.java

diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/convert/ConvertRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/convert/ConvertRestImpl.java
index d763144b87..6e42cc705f 100644
--- a/rest-api/src/main/java/lcsb/mapviewer/api/convert/ConvertRestImpl.java
+++ b/rest-api/src/main/java/lcsb/mapviewer/api/convert/ConvertRestImpl.java
@@ -51,9 +51,12 @@ public class ConvertRestImpl extends BaseRestImpl {
   }
 
   public ByteArrayOutputStream converToImage(String fromFormat, String toFormat, String input)
-      throws InvalidInputDataExecption, SBMLException, IOException, ConverterException, DrawingException,
-      QueryException {
-    return converToImage(fromFormat, toFormat, input, 0.0, 0.0);
+      throws IOException, DrawingException, QueryException {
+    try {
+      return converToImage(fromFormat, toFormat, input, 0.0, 0.0);
+    } catch (InvalidInputDataExecption | ConverterException e) {
+      throw new QueryException("Input file is invalid", e);
+    }
   }
 
   public Map<String, Object> getInformation() {
diff --git a/web/src/test/java/lcsb/mapviewer/web/AllIntegrationTests.java b/web/src/test/java/lcsb/mapviewer/web/AllIntegrationTests.java
index ffb65322d3..d54b136d00 100644
--- a/web/src/test/java/lcsb/mapviewer/web/AllIntegrationTests.java
+++ b/web/src/test/java/lcsb/mapviewer/web/AllIntegrationTests.java
@@ -9,6 +9,7 @@ import org.junit.runners.Suite.SuiteClasses;
     ChemicalControllerIntegrationTest.class,
     CommentControllerIntegrationTest.class,
     CommentControllerIntegrationTestWithoutTransaction.class,
+    ConvertControllerIntegrationTest.class,
     DrugControllerIntegrationTest.class,
     EndPointsInputValidationTests.class,
     FileControllerIntegrationTest.class,
diff --git a/web/src/test/java/lcsb/mapviewer/web/ConvertControllerIntegrationTest.java b/web/src/test/java/lcsb/mapviewer/web/ConvertControllerIntegrationTest.java
new file mode 100644
index 0000000000..01b4928513
--- /dev/null
+++ b/web/src/test/java/lcsb/mapviewer/web/ConvertControllerIntegrationTest.java
@@ -0,0 +1,37 @@
+package lcsb.mapviewer.web;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.web.servlet.RequestBuilder;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+public class ConvertControllerIntegrationTest extends ControllerIntegrationTest {
+
+  @Test
+  public void testConvertInvalidFile() throws Exception {
+    String body = "invalid content";
+    RequestBuilder request = post("/convert/image/SBGN-ML:svg")
+        .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+        .content(body);
+
+    mockMvc.perform(request)
+        .andExpect(status().isBadRequest());
+  }
+
+  @Test
+  public void testConvertInvalidMapFormat() throws Exception {
+    String body = "invalid content";
+    RequestBuilder request = post("/convert/image/unknown:svg")
+        .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+        .content(body);
+
+    mockMvc.perform(request)
+        .andExpect(status().isBadRequest());
+  }
+
+}
-- 
GitLab