Updated dbException. Fixed the resource update service.

Change-Id: Ie3762953927411b8db121f460fed59661df6f659
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java
index 0226217..a5e91da 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java
@@ -5,11 +5,13 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.Charset;
@@ -20,13 +22,18 @@
 import java.security.NoSuchAlgorithmException;
 import java.security.UnrecoverableKeyException;
 import java.security.cert.CertificateException;
+import java.util.List;
 import java.util.UUID;
 
 import javax.net.ssl.SSLContext;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.utils.URIBuilder;
@@ -34,6 +41,7 @@
 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 import org.apache.http.conn.ssl.SSLContexts;
 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
+import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.eclipse.jetty.util.ssl.SslContextFactory;
@@ -41,8 +49,10 @@
 import org.junit.Test;
 
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.io.Files;
 import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.core.util.MultivaluedMapImpl;
 
 import de.ids_mannheim.korap.config.Attributes;
 import de.ids_mannheim.korap.config.BeanConfigTest;
@@ -68,10 +78,120 @@
  *
  */
 public class KustvaktServerTest extends BeanConfigTest {
+    private static ObjectMapper mapper = new ObjectMapper();
+
+
+    @Test
+    public void testRegisterBadPassword ()
+            throws URISyntaxException, ClientProtocolException, IOException {
+        HttpClient httpClient = HttpClients.createDefault();
+
+        MultivaluedMap<String, String> map = new MultivaluedMapImpl();
+        map.putSingle("username", "kusvakt");
+        map.putSingle("email", "kustvakt@ids-mannheim.de");
+        map.putSingle("password", "password");
+        map.putSingle("firstName", "kustvakt");
+        map.putSingle("lastName", "user");
+        map.putSingle("address", "Mannheim");
+
+        URIBuilder builder = new URIBuilder();
+        builder.setScheme("http").setHost("localhost").setPort(8089)
+                .setPath("/api/v0.1/user/register");
+        URI uri = builder.build();
+        HttpPost httppost = new HttpPost(uri);
+        StringEntity entity = new StringEntity(JsonUtils.toJSON(map));
+        httppost.setEntity(entity);
+        httppost.addHeader(HttpHeaders.CONTENT_TYPE,
+                MediaType.APPLICATION_JSON);
+        httppost.addHeader(HttpHeaders.USER_AGENT, "Apache HTTP Client");
+        httppost.addHeader(HttpHeaders.HOST, "localhost");
+
+        HttpResponse response = httpClient.execute(httppost);
+        assertEquals(ClientResponse.Status.NOT_ACCEPTABLE.getStatusCode(),
+                response.getStatusLine().getStatusCode());
+
+        HttpEntity responseEntity = response.getEntity();
+        JsonNode errorNode = mapper.readTree(responseEntity.getContent());
+        assertEquals(
+                "[The value for the parameter password is not valid or acceptable.]",
+                errorNode.get("errors").get(0).get(2).asText());
+
+    }
+
+
+    @Test
+    public void testRegisterExistingUsername ()
+            throws URISyntaxException, ClientProtocolException, IOException {
+        HttpClient httpClient = HttpClients.createDefault();
+
+        MultivaluedMap<String, String> map = new MultivaluedMapImpl();
+        map.putSingle("username", "kustvakt");
+        map.putSingle("email", "kustvakt@ids-mannheim.de");
+        map.putSingle("password", "password1234");
+        map.putSingle("firstName", "kustvakt");
+        map.putSingle("lastName", "user");
+        map.putSingle("address", "Mannheim");
+
+        URIBuilder builder = new URIBuilder();
+        builder.setScheme("http").setHost("localhost").setPort(8089)
+                .setPath("/api/v0.1/user/register");
+        URI uri = builder.build();
+        HttpPost httppost = new HttpPost(uri);
+        StringEntity entity = new StringEntity(JsonUtils.toJSON(map));
+        httppost.setEntity(entity);
+        httppost.addHeader(HttpHeaders.CONTENT_TYPE,
+                MediaType.APPLICATION_JSON);
+        httppost.addHeader(HttpHeaders.USER_AGENT, "Apache HTTP Client");
+        httppost.addHeader(HttpHeaders.HOST, "localhost");
+
+        HttpResponse response = httpClient.execute(httppost);
+        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
+                response.getStatusLine().getStatusCode());
+
+        HttpEntity responseEntity = response.getEntity();
+        JsonNode errorNode = mapper.readTree(responseEntity.getContent());
+        assertEquals(
+                "[The value for the parameter password is not valid or acceptable.]",
+                errorNode.get("errors").asText());
+    }
+
+
+    @Test
+    public void testRegisterUser ()
+            throws URISyntaxException, ClientProtocolException, IOException {
+        HttpClient httpClient = HttpClients.createDefault();
+
+        MultivaluedMap<String, String> map = new MultivaluedMapImpl();
+        map.putSingle("username", "testUser");
+        map.putSingle("email", "testUser@ids-mannheim.de");
+        map.putSingle("password", "testPassword1234");
+        map.putSingle("firstName", "test");
+        map.putSingle("lastName", "user");
+        map.putSingle("address", "Mannheim");
+
+        URIBuilder builder = new URIBuilder();
+        builder.setScheme("http").setHost("localhost").setPort(8089)
+                .setPath("/api/v0.1/user/register");
+        URI uri = builder.build();
+        HttpPost httppost = new HttpPost(uri);
+        StringEntity entity = new StringEntity(JsonUtils.toJSON(map));
+        httppost.setEntity(entity);
+        httppost.addHeader(HttpHeaders.CONTENT_TYPE,
+                MediaType.APPLICATION_JSON);
+        httppost.addHeader(HttpHeaders.USER_AGENT, "Apache HTTP Client");
+        httppost.addHeader(HttpHeaders.HOST, "localhost");
+
+        HttpResponse response = httpClient.execute(httppost);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatusLine().getStatusCode());
+
+    }
+
+
     @Test
     public void testCreatePolicy () throws IOException, URISyntaxException {
 
-        HttpClient httpClient = HttpClients.createDefault();;
+        HttpClient httpClient = HttpClients.createDefault();
 
         String id = UUID.randomUUID().toString();
         URIBuilder builder = new URIBuilder();
@@ -82,8 +202,7 @@
                 .setParameter("description", "Goethe corpus")
                 .setParameter("group", "public")
                 .setParameter("perm", Permission.READ.name())
-                .setParameter("loc", "")
-                .setParameter("expire", "");
+                .setParameter("loc", "").setParameter("expire", "");
 
         URI uri = builder.build();
         HttpPost httppost = new HttpPost(uri);
@@ -101,7 +220,7 @@
     public void testCreatePolicyForFoundry ()
             throws IOException, URISyntaxException {
 
-        HttpClient httpClient = HttpClients.createDefault();;
+        HttpClient httpClient = HttpClients.createDefault();
 
         String id = UUID.randomUUID().toString();
         URIBuilder builder = new URIBuilder();
@@ -131,7 +250,7 @@
     public void testCreatePolicyWithMultiplePermissions ()
             throws IOException, URISyntaxException {
 
-        HttpClient httpClient = HttpClients.createDefault();;
+        HttpClient httpClient = HttpClients.createDefault();
 
         String id = UUID.randomUUID().toString();
         URIBuilder builder = new URIBuilder();
@@ -213,6 +332,24 @@
         return httpclient.execute(httppost);
 
     }
+    
+    @Test
+    public void testResourceUpdate ()
+            throws IOException, URISyntaxException {
+
+        HttpClient httpclient = HttpClients.createDefault();
+        URIBuilder builder = new URIBuilder();
+        builder.setScheme("http").setHost("localhost").setPort(8089)
+                .setPath("/api/v0.1/virtualcollection/00df953b-2227-4c23-84c1-5532c07bf8ce")
+                .setParameter("name", "Goethe collection")
+                .setParameter("description", "Goethe collection");
+        URI uri = builder.build();
+        HttpPost httppost = new HttpPost(uri);
+        httppost.addHeader(Attributes.AUTHORIZATION,
+                BasicHttpAuth.encode("kustvakt", "kustvakt2015"));
+        HttpResponse response = httpclient.execute(httppost);
+
+    }
 
 
     private void checkResourceInDB (String id) throws KustvaktException {
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceServiceTest.java b/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceServiceTest.java
index fed448d..544c790 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceServiceTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/full/ResourceServiceTest.java
@@ -6,19 +6,22 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.io.IOException;
 import java.util.Iterator;
 
+import org.apache.http.HttpStatus;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.sun.jersey.api.client.ClientResponse;
 
 import de.ids_mannheim.korap.config.Attributes;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
-import de.ids_mannheim.korap.exceptions.StatusCodes;
 import de.ids_mannheim.korap.handlers.ResourceDao;
 import de.ids_mannheim.korap.query.serialize.QuerySerializer;
 import de.ids_mannheim.korap.resources.KustvaktResource;
@@ -32,7 +35,8 @@
  * @date 14/01/2016
  */
 public class ResourceServiceTest extends FastJerseyTest {
-	
+    private static ObjectMapper mapper = new ObjectMapper();
+    
     @BeforeClass
     public static void configure () throws Exception {
         FastJerseyTest.setPackages("de.ids_mannheim.korap.web.service.full",
@@ -266,7 +270,7 @@
     }
 
     @Test
-    public void testResourceStore() throws KustvaktException {
+    public void testResourceStore() throws KustvaktException, JsonProcessingException, IOException {
     	// resource store service
         ClientResponse response = resource()
                 .path(getAPIVersion())
@@ -303,7 +307,7 @@
         assertNotNull(res);
         Assert.assertEquals("Goethe",res.getName().toString());
         
-        // no update resource service
+//         no update resource service
         response = resource()
                 .path(getAPIVersion())
                 .path("virtualcollection")
@@ -313,7 +317,11 @@
                         BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
                 .post(ClientResponse.class);
         
-        assertEquals(StatusCodes.NOTHING_CHANGED, response.getStatus());
+        assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatus());
+        JsonNode errorNode = mapper.readTree(response.getEntityInputStream());
+        assertEquals(
+                "[No change has found.]",
+                errorNode.get("errors").get(0).get(2).asText());
         
         // update resource service
         response = resource()
@@ -325,6 +333,9 @@
                         BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
                 .post(ClientResponse.class);
         
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        
         res = dao.findbyId(id,
                 User.UserFactory.getDemoUser());
         assertNotNull(res);