Updated OAuth2Client JSON definition and controller tests

Change-Id: Id1db5c848cac2fa71acd4c3aa31a3ca30ab4b4ef
diff --git a/full/Changes b/full/Changes
index 2df16a6..c23caf4 100644
--- a/full/Changes
+++ b/full/Changes
@@ -10,6 +10,8 @@
    parameters (margaretha) 
 2021-03-29
  - Added client type in the client list web-service (margaretha)  
+2021-04-19
+ - Updated OAuth2Client JSON definition and controller tests (margaretha)
  
 # version 0.63
 26/10/2020
diff --git a/full/src/main/java/de/ids_mannheim/korap/oauth2/service/OAuth2ClientService.java b/full/src/main/java/de/ids_mannheim/korap/oauth2/service/OAuth2ClientService.java
index 5406387..770c01a 100644
--- a/full/src/main/java/de/ids_mannheim/korap/oauth2/service/OAuth2ClientService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/oauth2/service/OAuth2ClientService.java
@@ -81,9 +81,14 @@
 
     public OAuth2ClientDto registerClient (OAuth2ClientJson clientJson,
             String registeredBy) throws KustvaktException {
-        
-        ParameterChecker.checkNameValue(clientJson.getName(), "clientName");
-        
+        try {
+            ParameterChecker.checkNameValue(clientJson.getName(), "clientName");
+        }
+        catch (KustvaktException e) {
+            throw new KustvaktException(e.getStatusCode(), e.getMessage(),
+                    OAuth2Error.INVALID_REQUEST);
+        }
+    
         String url = clientJson.getUrl();
         if (url != null && !url.isEmpty()) {
             if (!urlValidator.isValid(url)) {
@@ -126,6 +131,10 @@
                     clientJson.getType(), url, redirectURI, registeredBy,
                     clientJson.getDescription());
         }
+        catch (KustvaktException e) {
+            throw new KustvaktException(e.getStatusCode(),
+                    e.getMessage(), OAuth2Error.INVALID_REQUEST);
+        }
         catch (Exception e) {
             Throwable cause = e;
             Throwable lastCause = null;
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/input/OAuth2ClientJson.java b/full/src/main/java/de/ids_mannheim/korap/web/input/OAuth2ClientJson.java
index 381edc0..b11ac63 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/input/OAuth2ClientJson.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/input/OAuth2ClientJson.java
@@ -1,5 +1,7 @@
 package de.ids_mannheim.korap.web.input;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+
 import de.ids_mannheim.korap.oauth2.constant.OAuth2ClientType;
 
 /**
@@ -23,6 +25,7 @@
     private String url;
     // redirect URI determines where the OAuth 2.0 service will return
     // the user to after they have authorized a client.
+    @JsonProperty("redirect_uri")
     private String redirectURI;
     
 
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java
index 817e835..fec1ba2 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java
@@ -4,12 +4,16 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.Charset;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.Set;
 
 import javax.ws.rs.core.MultivaluedMap;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.http.entity.ContentType;
 import org.junit.Test;
 
@@ -162,10 +166,28 @@
         ClientResponse response = registerClient(username, json);
         String entity = response.getEntity(String.class);
         JsonNode node = JsonUtils.readTree(entity);
-        assertEquals(StatusCodes.INVALID_ARGUMENT,
-                node.at("/errors/0/0").asInt());
         assertEquals("clientName must contain at least 3 characters",
-                node.at("/errors/0/1").asText());
+                node.at("/error_description").asText());
+        assertEquals("invalid_request",
+                node.at("/error").asText());
+        assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
+    }
+    
+    @Test
+    public void testRegisterClientMissingDescription ()
+            throws UniformInterfaceException, ClientHandlerException,
+            KustvaktException {
+        OAuth2ClientJson json = new OAuth2ClientJson();
+        json.setName("R client");
+        json.setType(OAuth2ClientType.PUBLIC);
+
+        ClientResponse response = registerClient(username, json);
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+        assertEquals("client description is null",
+                node.at("/error_description").asText());
+        assertEquals("invalid_request",
+                node.at("/error").asText());
         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
     }
     
@@ -193,6 +215,35 @@
     }
     
     @Test
+    public void testRegisterClientUsingPlainJson ()
+            throws UniformInterfaceException, ClientHandlerException,
+            KustvaktException, IOException {
+
+        InputStream is = getClass().getClassLoader()
+                .getResourceAsStream("json/oauth2_public_client.json");
+        String json = IOUtils.toString(is, Charset.defaultCharset());
+
+        ClientResponse response = resource().path(API_VERSION).path("oauth2")
+                .path("client").path("register")
+                .header(Attributes.AUTHORIZATION,
+                        HttpAuthorizationHandler
+                                .createBasicAuthorizationHeaderValue(username,
+                                        "password"))
+                .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
+                .entity(json).post(ClientResponse.class);
+
+        String entity = response.getEntity(String.class);
+        assertEquals(Status.OK.getStatusCode(), response.getStatus());
+        JsonNode node = JsonUtils.readTree(entity);
+        String clientId = node.at("/client_id").asText();
+        assertNotNull(clientId);
+        assertTrue(node.at("/client_secret").isMissingNode());
+
+        testResetPublicClientSecret(clientId);
+        testAccessTokenAfterDeregistration(clientId, null, null);
+    }
+    
+    @Test
     public void testRegisterDesktopApp () throws UniformInterfaceException,
             ClientHandlerException, KustvaktException {
         OAuth2ClientJson json = new OAuth2ClientJson();
diff --git a/full/src/test/resources/json/oauth2_public_client.json b/full/src/test/resources/json/oauth2_public_client.json
new file mode 100644
index 0000000..14ab89a
--- /dev/null
+++ b/full/src/test/resources/json/oauth2_public_client.json
@@ -0,0 +1,6 @@
+{
+  "name":"my client",
+  "type": "PUBLIC",
+  "redirect_uri": "https://my.client.com",
+  "description":"my public client"
+}
\ No newline at end of file