From f65d7995f4b83b99aa3c6c586cb29c3afb074478 Mon Sep 17 00:00:00 2001
From: Jesper Zedlitz <jesper@zedlitz.de>
Date: Fri, 4 Mar 2022 08:39:25 +0100
Subject: [PATCH] =?UTF-8?q?Test=20f=C3=BCr=20CkanAPI::getResources?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../de/landsh/opendata/ckan/CkanApiTest.java  |  66 ++++---
 ...hule-mit-einem-positiven-testergebnis.json | 172 ++++++++++++++++++
 2 files changed, 214 insertions(+), 24 deletions(-)
 create mode 100644 src/test/resources/package_show__testungen-in-der-schule-mit-einem-positiven-testergebnis.json

diff --git a/src/test/java/de/landsh/opendata/ckan/CkanApiTest.java b/src/test/java/de/landsh/opendata/ckan/CkanApiTest.java
index 595e632..991312e 100644
--- a/src/test/java/de/landsh/opendata/ckan/CkanApiTest.java
+++ b/src/test/java/de/landsh/opendata/ckan/CkanApiTest.java
@@ -1,9 +1,5 @@
 package de.landsh.opendata.ckan;
 
-import de.landsh.opendata.ckan.ApiKey;
-import de.landsh.opendata.ckan.CkanAPI;
-import de.landsh.opendata.ckan.Resource;
-import de.landsh.opendata.ckan.RestClient;
 import org.apache.commons.io.IOUtils;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpUriRequest;
@@ -19,6 +15,10 @@ import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 
 public class CkanApiTest {
 
@@ -49,7 +49,7 @@ public class CkanApiTest {
         final JSONObject dataset = ckanAPI.readDataset("kindertagesstatten1");
         Assert.assertNotNull(dataset);
 
-        Assert.assertEquals(new URI("http://localhost/api/3/action/package_show?id=kindertagesstatten1"), argument.getValue().getURI());
+        assertEquals(new URI("http://localhost/api/3/action/package_show?id=kindertagesstatten1"), argument.getValue().getURI());
     }
 
     @Test
@@ -60,7 +60,7 @@ public class CkanApiTest {
         Mockito.when(restClient.executeHttpRequest(argument.capture())).thenReturn(json);
 
         String result = ckanAPI.getCollection("kindertagesstatten1");
-        Assert.assertEquals("ed667223-6205-43f6-a2da-0acba4d53ddd", result);
+        assertEquals("ed667223-6205-43f6-a2da-0acba4d53ddd", result);
     }
 
     @Test
@@ -71,7 +71,7 @@ public class CkanApiTest {
         Mockito.when(restClient.executeHttpRequest(argument.capture())).thenReturn(json);
 
         String result = ckanAPI.getCollection("badegewasser-infrastruktur1");
-        Assert.assertEquals("6f30a595-9210-4f24-8873-b52c72401468", result);
+        assertEquals("6f30a595-9210-4f24-8873-b52c72401468", result);
     }
 
     /**
@@ -84,11 +84,10 @@ public class CkanApiTest {
         final ArgumentCaptor<HttpUriRequest> argument = ArgumentCaptor.forClass(HttpUriRequest.class);
         Mockito.when(restClient.executeHttpRequest(argument.capture())).thenReturn(json);
 
-        Assert.assertNull( ckanAPI.getCollection("geschaftsverteilungsplan-melund-stand-15-07-2020"));
+        Assert.assertNull(ckanAPI.getCollection("geschaftsverteilungsplan-melund-stand-15-07-2020"));
     }
 
 
-
     @Test
     public void getOrganization() throws Exception {
         JSONObject json = new JSONObject(IOUtils.toString(getClass().getResourceAsStream("/package_show__kindertagesstatten1.json"), StandardCharsets.UTF_8));
@@ -97,7 +96,7 @@ public class CkanApiTest {
         Mockito.when(restClient.executeHttpRequest(argument.capture())).thenReturn(json);
 
         String result = ckanAPI.getOrganization("kindertagesstatten1");
-        Assert.assertEquals("f2d024c8-dbcc-4786-837e-d4eca1a23a57", result);
+        assertEquals("f2d024c8-dbcc-4786-837e-d4eca1a23a57", result);
     }
 
     @Test
@@ -108,7 +107,7 @@ public class CkanApiTest {
         Mockito.when(restClient.executeHttpRequest(argument.capture())).thenReturn(json);
 
         String result = ckanAPI.getAccessURL("kindertagesstatten1");
-        Assert.assertEquals("http://185.223.104.6/data/sozmin/kita_2019-09-18.csv", result);
+        assertEquals("http://185.223.104.6/data/sozmin/kita_2019-09-18.csv", result);
     }
 
     @Test
@@ -120,8 +119,8 @@ public class CkanApiTest {
         Mockito.when(restClient.executeRawHttpRequest(argument.capture())).thenReturn(mockResponse);
 
         String result = ckanAPI.findNewestDataset("mycollection");
-        Assert.assertEquals("mydata", result);
-        Assert.assertEquals(new URI("http://localhost/collection/mycollection/aktuell"), argument.getValue().getURI());
+        assertEquals("mydata", result);
+        assertEquals(new URI("http://localhost/collection/mycollection/aktuell"), argument.getValue().getURI());
     }
 
     @Test
@@ -130,12 +129,12 @@ public class CkanApiTest {
 
         final Resource result = ckanAPI.getResource(dataset, true);
         Assert.assertNotNull(result);
-        Assert.assertEquals("kita.csv", result.getName());
-        Assert.assertEquals("96948a3b-b1ca-407c-a33a-60a9ebc49c78", result.getId());
-        Assert.assertEquals("CSV", result.getFormat());
-        Assert.assertEquals("http://185.223.104.6/data/sozmin/kita_2019-09-18.csv", result.getAccessURL());
-        Assert.assertEquals(300618, result.getByteSize());
-        Assert.assertEquals("text/csv", result.getMimeType());
+        assertEquals("kita.csv", result.getName());
+        assertEquals("96948a3b-b1ca-407c-a33a-60a9ebc49c78", result.getId());
+        assertEquals("CSV", result.getFormat());
+        assertEquals("http://185.223.104.6/data/sozmin/kita_2019-09-18.csv", result.getAccessURL());
+        assertEquals(300618, result.getByteSize());
+        assertEquals("text/csv", result.getMimeType());
         Assert.assertNull(result.getChecksum());
     }
 
@@ -168,8 +167,8 @@ public class CkanApiTest {
             Assert.assertTrue(expected.getMessage().contains("mycollection"));
         }
 
-        Assert.assertEquals(new URI("http://localhost/api/3/action/package_show?id=mydataset"), argument.getAllValues().get(0).getURI());
-        Assert.assertEquals(new URI("http://localhost/api/3/action/package_show?id=mycollection"), argument.getAllValues().get(1).getURI());
+        assertEquals(new URI("http://localhost/api/3/action/package_show?id=mydataset"), argument.getAllValues().get(0).getURI());
+        assertEquals(new URI("http://localhost/api/3/action/package_show?id=mycollection"), argument.getAllValues().get(1).getURI());
     }
 
     @Test
@@ -184,7 +183,7 @@ public class CkanApiTest {
             Assert.assertTrue(expected.getMessage().contains("mydataset"));
         }
 
-        Assert.assertEquals(new URI("http://localhost/api/3/action/package_show?id=mydataset"), argument.getAllValues().get(0).getURI());
+        assertEquals(new URI("http://localhost/api/3/action/package_show?id=mydataset"), argument.getAllValues().get(0).getURI());
     }
 
     /**
@@ -205,8 +204,27 @@ public class CkanApiTest {
             Assert.assertTrue(expected.getMessage().contains("is no collection"));
         }
 
-        Assert.assertEquals(new URI("http://localhost/api/3/action/package_show?id=mydataset"), argument.getAllValues().get(0).getURI());
-        Assert.assertEquals(new URI("http://localhost/api/3/action/package_show?id=mycollection"), argument.getAllValues().get(1).getURI());
+        assertEquals(new URI("http://localhost/api/3/action/package_show?id=mydataset"), argument.getAllValues().get(0).getURI());
+        assertEquals(new URI("http://localhost/api/3/action/package_show?id=mycollection"), argument.getAllValues().get(1).getURI());
+    }
+
+    @Test
+    public void getResources() throws IOException {
+        final JSONObject json = new JSONObject(IOUtils.toString(getClass().getResourceAsStream("/package_show__testungen-in-der-schule-mit-einem-positiven-testergebnis.json"), StandardCharsets.UTF_8));
+        
+        // invoke method
+        final List<Resource> result = ckanAPI.getResources(json);
+
+        // check results
+        assertNotNull(result);
+        assertEquals(1, result.size());
+        Resource resource = result.get(0);
+        assertEquals("9de8c47d-ada0-4446-bd28-b9ab1f5396f9",resource.getId());
+        assertEquals("https://opendata-stage.schleswig-holstein.de/dataset/f17419f7-d705-487b-9a96-00778c53ea65/resource/9de8c47d-ada0-4446-bd28-b9ab1f5396f9/download/data.csv",resource.getAccessURL());
+        assertEquals("6bea861d37b495bbb9ef7ad401ee6291",resource.getChecksum());
+        assertEquals("CSV",resource.getFormat());
+        assertEquals("data.csv",resource.getName());
+        assertEquals(772,resource.getByteSize());
     }
 
 }
diff --git a/src/test/resources/package_show__testungen-in-der-schule-mit-einem-positiven-testergebnis.json b/src/test/resources/package_show__testungen-in-der-schule-mit-einem-positiven-testergebnis.json
new file mode 100644
index 0000000..469b6d6
--- /dev/null
+++ b/src/test/resources/package_show__testungen-in-der-schule-mit-einem-positiven-testergebnis.json
@@ -0,0 +1,172 @@
+{
+  "license_title": "gemeinfrei",
+  "maintainer": null,
+  "relationships_as_object": [],
+  "is_new": false,
+  "private": false,
+  "maintainer_email": null,
+  "num_tags": 2,
+  "id": "f17419f7-d705-487b-9a96-00778c53ea65",
+  "metadata_created": "2022-01-17T14:27:55.122258",
+  "metadata_modified": "2022-03-03T02:25:24.416101",
+  "author": null,
+  "author_email": null,
+  "isopen": true,
+  "state": "active",
+  "version": null,
+  "archiver": {
+    "status": "Archived successfully",
+    "is_broken": false,
+    "reason": "",
+    "status_id": 0
+  },
+  "creator_user_id": "7df583a3-350e-47ef-8738-3bbb68bdb954",
+  "type": "dataset",
+  "resources": [
+    {
+      "cache_last_updated": null,
+      "package_id": "f17419f7-d705-487b-9a96-00778c53ea65",
+      "id": "9de8c47d-ada0-4446-bd28-b9ab1f5396f9",
+      "size": 772,
+      "state": "active",
+      "archiver": {
+        "is_broken_printable": "Downloaded OK",
+        "updated": "2022-03-03T03:25:27.999830",
+        "cache_filepath": "/var/lib/ckan/default/ckan_storage/resources/9de/8c4/7d-ada0-4446-bd28-b9ab1f5396f9",
+        "last_success": "2022-03-03T03:25:27.999830",
+        "size": 772,
+        "is_broken": false,
+        "failure_count": 0,
+        "etag": null,
+        "status": "Archived successfully",
+        "url_redirected_to": "https://opendata-stage.schleswig-holstein.de/dataset/f17419f7-d705-487b-9a96-00778c53ea65/resource/9de8c47d-ada0-4446-bd28-b9ab1f5396f9/download/data.csv",
+        "hash": "d3b175a0ec131f7587ab32e02f4189d618eb0195",
+        "status_id": 0,
+        "reason": "",
+        "last_modified": null,
+        "resource_timestamp": "2022-03-03T02:25:24.409512",
+        "mimetype": "text/csv",
+        "cache_url": "https://opendata-stage.schleswig-holstein.de/dataset/f17419f7-d705-487b-9a96-00778c53ea65/resource/9de8c47d-ada0-4446-bd28-b9ab1f5396f9/download/data.csv",
+        "created": "2022-03-03T03:25:25.544769",
+        "first_failure": null
+      },
+      "hash": "6bea861d37b495bbb9ef7ad401ee6291",
+      "description": "",
+      "format": "CSV",
+      "last_modified": "2022-03-03T02:25:22.267183",
+      "url_type": "upload",
+      "mimetype": "text/csv",
+      "cache_url": null,
+      "name": "data.csv",
+      "created": "2022-03-03T02:25:22.407356",
+      "url": "https://opendata-stage.schleswig-holstein.de/dataset/f17419f7-d705-487b-9a96-00778c53ea65/resource/9de8c47d-ada0-4446-bd28-b9ab1f5396f9/download/data.csv",
+      "qa": {
+        "updated": "2022-03-03T03:25:35.546851",
+        "openness_score": 3,
+        "archival_timestamp": "2022-03-03T03:25:27.999830",
+        "format": "CSV",
+        "created": "2022-03-03T03:25:33.097755",
+        "resource_timestamp": null,
+        "openness_score_reason": "The format of the file was not recognized from its contents. URL extension \"csv\" relates to format \"CSV\" and receives score: 3."
+      },
+      "mimetype_inner": null,
+      "hash_algorithm": "http://dcat-ap.de/def/hashAlgorithms/md/5",
+      "position": 0,
+      "revision_id": "66c3dde9-3d57-4d9f-9abc-617f71ecddbb",
+      "resource_type": null
+    }
+  ],
+  "num_resources": 1,
+  "tags": [
+    {
+      "vocabulary_id": null,
+      "state": "active",
+      "display_name": "COVID-19",
+      "id": "e9e96e7e-c23e-4c2f-9aeb-9ffe0421c420",
+      "name": "COVID-19"
+    },
+    {
+      "vocabulary_id": null,
+      "state": "active",
+      "display_name": "Corona",
+      "id": "79a74ef8-01fd-43ff-af15-3923a25d38b1",
+      "name": "Corona"
+    }
+  ],
+  "groups": [
+    {
+      "display_name": "Bildung, Kultur und Sport",
+      "description": "",
+      "image_display_url": "",
+      "title": "Bildung, Kultur und Sport",
+      "id": "educ",
+      "name": "educ"
+    },
+    {
+      "display_name": "Gesundheit",
+      "description": "",
+      "image_display_url": "",
+      "title": "Gesundheit",
+      "id": "heal",
+      "name": "heal"
+    }
+  ],
+  "license_id": "http://dcat-ap.de/def/licenses/ccpdm/1.0",
+  "relationships_as_subject": [],
+  "organization": {
+    "description": "",
+    "created": "2021-10-20T07:36:08.369510",
+    "title": "Ministerium für Bildung, Wissenschaft und Kultur",
+    "name": "mbwk",
+    "is_organization": true,
+    "state": "active",
+    "image_url": "https://opendata.zitsh.de/images/Dachmarke.png",
+    "revision_id": "a17f0ae9-980c-4e3b-bc82-af62f5f74ad3",
+    "type": "organization",
+    "id": "c177327f-a94b-4192-8820-0f2cc6bec762",
+    "approval_status": "approved"
+  },
+  "name": "testungen-in-der-schule-mit-einem-positiven-testergebnis",
+  "language": "http://publications.europa.eu/resource/authority/language/DEU",
+  "url": null,
+  "notes": "Testungen in der Schule mit einem positiven Testergebnis",
+  "owner_org": "c177327f-a94b-4192-8820-0f2cc6bec762",
+  "qa": {
+    "openness_score_reason": "The format of the file was not recognized from its contents. URL extension \"csv\" relates to format \"CSV\" and receives score: 3.",
+    "updated": "2022-03-03T03:25:35.546851",
+    "openness_score": 3
+  },
+  "extras": [
+    {
+      "key": "groups",
+      "value": "educ,heal"
+    },
+    {
+      "key": "issued",
+      "value": "2022-01-17T00:00:00"
+    },
+    {
+      "key": "licenseAttributionByText",
+      "value": ""
+    },
+    {
+      "key": "spatial_text",
+      "value": "Bundesland Schleswig-Holstein"
+    },
+    {
+      "key": "spatial_uri",
+      "value": "http://dcat-ap.de/def/politicalGeocoding/stateKey/01"
+    },
+    {
+      "key": "temporal_end",
+      "value": "2022-01-07T00:00:00"
+    },
+    {
+      "key": "temporal_start",
+      "value": "2021-04-19T00:00:00"
+    }
+  ],
+  "license_url": "http://creativecommons.org/publicdomain/mark/1.0/",
+  "title": "Testungen in der Schule mit einem positiven Testergebnis",
+  "revision_id": "289fb9c5-29b9-454f-8fec-b666854ca10c"
+}
-- 
GitLab