Added VC reference and MapUtil tests.
Change-Id: I2892044b1efdc7fe0fa2aca74f65da3f580f77d5
diff --git a/core/Changes b/core/Changes
index 29195ff..5b04a5e 100644
--- a/core/Changes
+++ b/core/Changes
@@ -16,6 +16,8 @@
- Removed codes related to user registration & password management (margaretha)
22/01/2019
- Added create, edit, retrieve user default setting controllers (margaretha)
+24/01/2019
+ - Added default setting key validation & fixed UserdataTest (margaretha)
version 0.61.4
diff --git a/full/Changes b/full/Changes
index 6b41749..fe15ea8 100644
--- a/full/Changes
+++ b/full/Changes
@@ -27,6 +27,10 @@
23/01/2019
- Added default setting key validation (margaretha)
- Fixed UserdataTest (margaretha)
+24/01/2019
+ - Added default setting key validation & fixed UserdataTest (margaretha)
+25/01/2019
+ - Added VC reference and MapUtil tests (margaretha)
# version 0.61.4
14/11/2018
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/utils/MapUtils.java b/full/src/main/java/de/ids_mannheim/korap/web/utils/MapUtils.java
index b4d9266..7d1913f 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/utils/MapUtils.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/utils/MapUtils.java
@@ -8,25 +8,31 @@
import javax.ws.rs.core.MultivaluedMap;
-/** Utility methods for maps
+/**
+ * Utility methods for maps
*
* @author margaretha
*
*/
public class MapUtils {
- /** Converts {@link MultivaluedMap} to {@link Map}
+ /**
+ * Converts {@link MultivaluedMap} to {@link Map}
*
* @param multivaluedMap
* @return
*/
public static Map<String, String> toMap (
MultivaluedMap<String, String> multivaluedMap) {
+
+ if (multivaluedMap == null) {
+ return new HashMap<String, String>();
+ }
Set<String> keySet = multivaluedMap.keySet();
Map<String, String> map = new HashMap<String, String>(keySet.size());
-
- for (String key : keySet){
+
+ for (String key : keySet) {
List<String> values = multivaluedMap.get(key);
String value = values.stream().collect(Collectors.joining(" "));
map.put(key, value);
diff --git a/full/src/test/java/de/ids_mannheim/korap/misc/MapUtilsTest.java b/full/src/test/java/de/ids_mannheim/korap/misc/MapUtilsTest.java
new file mode 100644
index 0000000..e95615a
--- /dev/null
+++ b/full/src/test/java/de/ids_mannheim/korap/misc/MapUtilsTest.java
@@ -0,0 +1,44 @@
+package de.ids_mannheim.korap.misc;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+
+import org.junit.Test;
+
+import de.ids_mannheim.korap.web.utils.MapUtils;
+import edu.emory.mathcs.backport.java.util.Arrays;
+
+public class MapUtilsTest {
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testConvertToMap () {
+ MultivaluedMap<String, String> mm =
+ new MultivaluedHashMap<String, String>();
+ mm.put("k1", Arrays.asList(new String[] { "a", "b", "c" }));
+ mm.put("k2", Arrays.asList(new String[] { "d", "e", "f" }));
+
+ Map<String, String> map = MapUtils.toMap(mm);
+ assertEquals("a b c", map.get("k1"));
+ assertEquals("d e f", map.get("k2"));
+ }
+
+ @Test
+ public void testConvertNullMap () {
+ Map<String, String> map = MapUtils.toMap(null);
+ assertEquals(0, map.size());
+ }
+
+ @Test
+ public void testConvertEmptyMap () {
+ MultivaluedMap<String, String> mm =
+ new MultivaluedHashMap<String, String>();
+ Map<String, String> map = MapUtils.toMap(mm);
+ assertEquals(0, map.size());
+ }
+
+}
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/VCReferenceTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/VCReferenceTest.java
index 997da1a..59b4d9a 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/VCReferenceTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/VCReferenceTest.java
@@ -17,6 +17,7 @@
import de.ids_mannheim.korap.dao.VirtualCorpusDao;
import de.ids_mannheim.korap.entity.VirtualCorpus;
import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
import de.ids_mannheim.korap.util.QueryException;
import de.ids_mannheim.korap.utils.JsonUtils;
import net.sf.ehcache.CacheManager;
@@ -29,7 +30,7 @@
private VirtualCorpusDao dao;
@Test
- public void testVCRef ()
+ public void testRefPredefinedVC ()
throws KustvaktException, IOException, QueryException {
testSearchWithoutVCRefOr();
testSearchWithoutVCRefAnd();
@@ -109,4 +110,32 @@
JsonNode node = JsonUtils.readTree(ent);
assertEquals(2, node.at("/documents").asInt());
}
+
+ @Test
+ public void testRefVCNotExist () throws KustvaktException {
+ ClientResponse response = resource().path(API_VERSION).path("search")
+ .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
+ .queryParam("cq", "referTo \"username/vc1\"")
+ .get(ClientResponse.class);
+
+ String ent = response.getEntity(String.class);
+ JsonNode node = JsonUtils.readTree(ent);
+ assertEquals(StatusCodes.NO_RESULT_FOUND,
+ node.at("/errors/0/0").asInt());
+ assertEquals("username/vc1", node.at("/errors/0/2").asText());
+ }
+
+ @Test
+ public void testRefNotAuthorized() throws KustvaktException {
+ ClientResponse response = resource().path(API_VERSION).path("search")
+ .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
+ .queryParam("cq", "referTo \"dory/dory VC\"")
+ .get(ClientResponse.class);
+
+ String ent = response.getEntity(String.class);
+ JsonNode node = JsonUtils.readTree(ent);
+ assertEquals(StatusCodes.AUTHORIZATION_FAILED,
+ node.at("/errors/0/0").asInt());
+ assertEquals("guest", node.at("/errors/0/2").asText());
+ }
}