]> WPIA git - gigi.git/commitdiff
add: fill the new columns and test the adjusted api-call
authorFelix Dörre <felix@dogcraft.de>
Tue, 28 Jun 2016 13:55:27 +0000 (15:55 +0200)
committerFelix Dörre <felix@dogcraft.de>
Tue, 28 Jun 2016 22:22:22 +0000 (00:22 +0200)
fixes #52

Change-Id: Id05d2ec156f4fb09d11da91eb83967b0b143808f

src/org/cacert/gigi/api/CATSImport.java
src/org/cacert/gigi/dbObjects/CATS.java
tests/org/cacert/gigi/api/ImportCATSResult.java

index c5745d689de9171e0c3883d09a2e3c76c06ec043..49960cd45387c2caf9cdced6d090cda761c35835 100644 (file)
@@ -33,6 +33,12 @@ public class CATSImport extends APIPoint {
             resp.sendError(500, "Error, requires mid, variant and date");
             return;
         }
+        String language = req.getParameter("language");
+        String version = req.getParameter("version");
+        if (language == null || version == null) {
+            resp.sendError(500, "Error, requires also language and version");
+            return;
+        }
         int id;
         try {
             id = Integer.parseInt(target);
@@ -48,6 +54,6 @@ public class CATSImport extends APIPoint {
         System.out.println("CATS: " + target + ": " + testType);
         User targetUser = (User) o;
         System.out.println(targetUser.getId());
-        CATS.enterResult(targetUser, testType, new Date(Long.parseLong(date)));
+        CATS.enterResult(targetUser, testType, new Date(Long.parseLong(date)), language, version);
     }
 }
index 18d5f7928280594d0f7a94f3f9d72fc5231a9ccf..700d76ee21175291101fadf1a5dde1bab532a275 100644 (file)
@@ -42,11 +42,13 @@ public class CATS {
         return i;
     }
 
-    public static void enterResult(User user, String testType, Date passDate) {
-        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?, `pass_date`=?")) {
+    public static void enterResult(User user, String testType, Date passDate, String language, String version) {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?, `pass_date`=?, `language`=?, `version`=?")) {
             ps.setInt(1, user.getId());
             ps.setInt(2, getID(testType));
             ps.setTimestamp(3, new Timestamp(passDate.getTime()));
+            ps.setString(4, language);
+            ps.setString(5, version);
             ps.execute();
         }
     }
index ae2a588f203345ecfd0afa12c6d45cd8aaac71bb..4a90500dc6dc722fe448168e512d0fe56d55d356 100644 (file)
@@ -79,15 +79,35 @@ public class ImportCATSResult extends ClientTest {
 
     }
 
+    @Test
+    public void testImportCATSFailures() throws GigiApiException, IOException, GeneralSecurityException, InterruptedException {
+        assertEquals(1, u.getTrainings().length);
+        assertNotEquals(200, executeImportQuery("").getResponseCode());
+        assertNotEquals(200, executeImportQuery("mid=" + u.getId()).getResponseCode());
+        assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training").getResponseCode());
+        assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training&date=" + System.currentTimeMillis()).getResponseCode());
+        assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training&date=" + System.currentTimeMillis() + "&language=en").getResponseCode());
+        assertNotEquals(200, executeImportQuery("mid=" + u.getId() + "&variant=Test+Training&date=" + System.currentTimeMillis() + "&version=1.0").getResponseCode());
+        assertEquals(1, u.getTrainings().length);
+        apiImport(u, "Test Training");
+        assertEquals(2, u.getTrainings().length);
+
+    }
+
     private void apiImport(User target, String test) throws IOException, MalformedURLException, NoSuchAlgorithmException, KeyManagementException, UnsupportedEncodingException, GeneralSecurityException {
+        HttpURLConnection connection = executeImportQuery("mid=" + target.getId() + "&variant=" + URLEncoder.encode(test, "UTF-8") + "&date=" + System.currentTimeMillis() + "&language=en&version=1.0");
+        if (connection.getResponseCode() != 200) {
+            throw new Error(connection.getResponseMessage());
+        }
+    }
+
+    private HttpURLConnection executeImportQuery(String query) throws IOException, MalformedURLException, NoSuchAlgorithmException, KeyManagementException, UnsupportedEncodingException, Error {
         HttpURLConnection connection = (HttpURLConnection) new URL("https://" + getServerName().replaceFirst("^www.", "api.") + CATSImport.PATH).openConnection();
         authenticateClientCert(pk, ce, connection);
         connection.setDoOutput(true);
         OutputStream os = connection.getOutputStream();
-        os.write(("mid=" + target.getId() + "&variant=" + URLEncoder.encode(test, "UTF-8") + "&date=" + System.currentTimeMillis()).getBytes("UTF-8"));
-        if (connection.getResponseCode() != 200) {
-            throw new Error(connection.getResponseMessage());
-        }
+        os.write(query.getBytes("UTF-8"));
+        return connection;
     }
 
     private String apiLookup(Certificate target) throws IOException, MalformedURLException, NoSuchAlgorithmException, KeyManagementException, UnsupportedEncodingException, GeneralSecurityException {