Skip to content

Commit

Permalink
Updates the test cases for ttl file
Browse files Browse the repository at this point in the history
Signed-off-by: vikrantsingh29 <[email protected]>
  • Loading branch information
vikrantsingh29 committed Jul 5, 2020
1 parent 12037af commit cdfbad4
Showing 1 changed file with 160 additions and 16 deletions.
176 changes: 160 additions & 16 deletions src/test/java/LaunutsApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -27,11 +24,18 @@ public class LaunutsApiTest {

private static String nutsLevel = "";
private static String nutsid = " ";
private static String msg = "";
private static String nutsName = "";
private static JSONArray coordinates = new JSONArray();
private static String nutsidttl = " ";
private static String nutsNamettl = "";
private static String lauId = "";
private static String lauName = "";
private static JSONArray lauCoordinates = new JSONArray();
private static String lauIdTTL = " ";
private static String lauNameTTL = "";
private static String lauIdInvalid = "";
private static String nutsCoordinatesTTL = "";


@BeforeClass
public static void setUpJson() throws IOException, JSONException {
Expand All @@ -45,14 +49,11 @@ public static void setUpJson() throws IOException, JSONException {
String content = EntityUtils.toString(entity);

JSONArray jsonArray = new JSONArray(content);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
nutsid = jsonobject.getString("nutsid");
msg = jsonobject.getString("msg");
nutsLevel = jsonobject.getString("nutsLevel");
nutsName = jsonobject.getString("nutsName");
coordinates = jsonobject.getJSONArray("coordinates");
}
JSONObject jsonobject = jsonArray.getJSONObject(0);
nutsid = jsonobject.getString("nutsId");
nutsLevel = jsonobject.getString("nutsLevel");
nutsName = jsonobject.getString("nutsName");
coordinates = jsonobject.getJSONArray("coordinates");

httpClient.close();
}
Expand Down Expand Up @@ -81,6 +82,92 @@ public static void setUpTTL() throws IOException, JSONException {

Statement statementPreflabel = defaultModel.getProperty(resource, prefLabel);
nutsNamettl = statementPreflabel.getObject().toString();

Property location = defaultModel.createProperty("http://purl.org/dc/terms/Location");
StmtIterator locationItr = defaultModel.listStatements
(resource, location, (RDFNode) null);

if (locationItr.hasNext()) {

Statement statementConformsTo = locationItr.nextStatement();
RDFNode object = statementConformsTo.getObject();
Resource objectAsResource = (Resource) object;
String uri = "http://www.opengis.net/ont/geosparql#";
Property polygon = defaultModel.createProperty(uri + "asWKT");
Statement statement = defaultModel.getProperty(objectAsResource, polygon);
RDFNode node = statement.getObject();
if (node.isLiteral()) {
nutsCoordinatesTTL = node.asLiteral().getString();
}
}

httpClient.close();
}

@BeforeClass
public static void setUpLauJson() throws IOException, JSONException {
String jsonUrl = "http://localhost:8080/launuts/lau/de_09577125/json";

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpJsonGet = new HttpGet(jsonUrl);
CloseableHttpResponse httpResponse = httpClient.execute(httpJsonGet);

HttpEntity entity = httpResponse.getEntity();
String content = EntityUtils.toString(entity);

JSONArray jsonArray = new JSONArray(content);

JSONObject jsonobject = jsonArray.getJSONObject(0);
lauId = jsonobject.getString("lauId");
lauName = jsonobject.getString("lauName");
lauCoordinates = jsonobject.getJSONArray("coordinates");

httpClient.close();
}

@BeforeClass
public static void setUpLauTTL() throws IOException, JSONException {

String urlTTL = "http://localhost:8080/launuts/lau/Dittenheim/ttl";

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpTLlGet = new HttpGet(urlTTL);
CloseableHttpResponse httpResponseTTL = httpClient.execute(httpTLlGet);

StringWriter writer = new StringWriter();
IOUtils.copy(httpResponseTTL.getEntity().getContent(), writer, "UTF-8");
String fixedString = writer.toString();
Model defaultModel = ModelFactory.createDefaultModel();
defaultModel.read(new StringReader(fixedString),
urlTTL,
"TURTLE");

Resource resource = defaultModel.createResource("http://projekt-opal.de/launuts/lau/DE/09577122");

Statement statementlauCode = defaultModel.getProperty(resource, notation);
lauIdTTL = statementlauCode.getObject().toString();

Statement statementPreflabel = defaultModel.getProperty(resource, prefLabel);
lauNameTTL = statementPreflabel.getObject().toString();
httpClient.close();
}

@BeforeClass
public static void negativeTest() throws IOException, JSONException {

String urlTTL = "http://localhost:8080/launuts/lau/Dittenim/json";

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpTLlGet = new HttpGet(urlTTL);
CloseableHttpResponse httpResponse = httpClient.execute(httpTLlGet);

HttpEntity entity = httpResponse.getEntity();
String content = EntityUtils.toString(entity);
JSONArray jsonArray = new JSONArray(content);

JSONObject jsonobject = jsonArray.getJSONObject(0);
lauIdInvalid = jsonobject.getString("lauId");

httpClient.close();
}

Expand All @@ -89,14 +176,19 @@ public void testNutsId() throws Exception {
Assert.assertEquals("DE12", nutsid);
}

@Test
public void testLauId() throws Exception {
Assert.assertEquals("DE_09577125", lauId);
}

@Test
public void testNutsName() throws Exception {
Assert.assertEquals("Karlsruhe", nutsName);
}

@Test
public void testMsg() throws Exception {
Assert.assertEquals("Query was successful", msg);
public void testLauName() throws Exception {
Assert.assertEquals("Ellingen, St", lauName);
}

@Test
Expand All @@ -123,6 +215,29 @@ public void testCoordinates() throws Exception {
coordinatesGot);
}

@Test
public void testLauCoordinates() throws Exception {
String arrays = "[[[49.06881350000003,10.868307500000071],[49.07946209700003,10.874846536000064]," +
"[49.08743217800003,10.873127329000056],[49.09703050000007,10.88661650000006]," +
"[49.08418420000004,10.907111248000035],[49.09281790500006,10.919888021000077]," +
"[49.09022463800005,10.936474906000058],[49.08447845000006,10.941273599000056]," +
"[49.08544671300007,10.952453879000075],[49.090076002000046,10.957865948000062]," +
"[49.08881412200003,10.975496303000057],[49.08278450000006,10.977152500000045]," +
"[49.079923198000074,10.97816343200003],[49.07594637600005,10.98861557500004]," +
"[49.062784571000066,10.992995144000076],[49.06030227400004,10.987197021000043]," +
"[49.05406831400006,10.985828591000029],[49.04750100000007,10.97670850000003]," +
"[49.04533051000004,10.96052885000006],[49.05291532000007,10.951518226000076]," +
"[49.05679616900005,10.936995211000067],[49.06139827900006,10.93386840100004]," +
"[49.06382272500008,10.919888021000077],[49.06140636200007,10.919888021000077]," +
"[49.05922252100004,10.919888021000077],[49.05550200000005,10.899600500000076]," +
"[49.05661288600004,10.882821964000073],[49.06403397200006,10.87693803600007]," +
"[49.06881350000003,10.868307500000071]]]";

String coordinatesGot = lauCoordinates.toString();
Assert.assertEquals(arrays,
coordinatesGot);
}

@Test
public void testNutsNameTTL() throws Exception {
Assert.assertEquals("Karlsruhe", nutsNamettl);
Expand All @@ -132,4 +247,33 @@ public void testNutsNameTTL() throws Exception {
public void testNutsIDTTL() throws Exception {
Assert.assertEquals("DE12", nutsidttl);
}
}

@Test
public void testLauNameTTL() throws Exception {
Assert.assertEquals("Dittenheim", lauNameTTL);
}

@Test
public void testLauIDTTL() throws Exception {
Assert.assertEquals("09577122", lauIdTTL);
}

@Test
public void testInvalidUrl() throws Exception {
Assert.assertNotEquals("Dittenheim", lauIdInvalid);
}

@Test
public void testTTLCoordinates() throws Exception {
//Taken fron launuts.ttl
String estimatedCoordinates = "POLYGON ((49.427 9.604, 49.364 9.443, 49.293 9.049, 49.194 8.818, " +
"49.058 8.878, 49.035 8.876, 48.867 8.929, 48.778 8.804, 48.522 8.769, 48.504 8.756, " +
"48.416 8.774, 48.378 8.737, 48.349 8.304, 48.603 8.222, 48.719 7.96, 48.967 8.233, " +
"48.981 8.261, 48.99 8.277, 49.08 8.34, 49.25 8.413, 49.283 8.467, 49.29 8.487, " +
"49.312 8.463, 49.411 8.497, 49.444 8.473, 49.542 8.423, 49.574 8.423, " +
"49.583 8.422, 49.52 8.581, 49.471 8.932, 49.526 9.083, 49.577 9.103," +
" 49.664 9.411, 49.636 9.507, 49.474 9.519, 49.427 9.604))";

Assert.assertEquals(estimatedCoordinates, nutsCoordinatesTTL);
}
}

0 comments on commit cdfbad4

Please sign in to comment.