Skip to content

Commit

Permalink
update QueryHandlerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
nck-mlcnv committed Oct 6, 2023
1 parent b50fd1c commit 507910c
Showing 1 changed file with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package org.aksw.iguana.cc.query.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.aksw.iguana.cc.query.selector.impl.LinearQuerySelector;
import org.aksw.iguana.cc.query.source.impl.FolderQuerySourceTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

public class QueryHandlerTest {

Path tempDir;

List<FolderQuerySourceTest.Query> queries;

@Before
@BeforeEach
public void createFolder() throws IOException {
tempDir = Files.createTempDirectory("folder-query-source-test-dir");

Expand All @@ -38,23 +39,65 @@ public void createFolder() throws IOException {
Collections.sort(queries);
}

@After
@AfterEach
public void removeFolder() throws IOException {
org.apache.commons.io.FileUtils.deleteDirectory(tempDir.toFile());
}


@Test
public void testDeserialization() throws Exception {
var json = String.format("""
{"path":"%s","format":"folder","order":"linear","lang":"SPARQL"}
""", tempDir);
""", tempDir.toString().replaceAll("\\\\", "\\\\\\\\")); // windows
final var mapper = new ObjectMapper();
QueryHandler queryHandler = assertDoesNotThrow(() -> mapper.readValue(json, QueryHandler.class));
final var selector = queryHandler.getQuerySelectorInstance();
assertTrue(selector instanceof LinearQuerySelector);
assertEquals(queries.size(), queryHandler.getQueryCount());
QueryHandler.QueryStringWrapper nextQuery = queryHandler.getNextQuery(queryHandler.getQuerySelectorInstance());
assertEquals(0, nextQuery.index());
assertEquals(nextQuery.query(), queries.get(0).content());
assertNotEquals(0, queryHandler.hashCode());
for (int i = 0; i < queryHandler.getQueryCount(); i++) {
assertEquals(i, selector.getCurrentIndex());
final var wrapper = queryHandler.getNextQuery(selector);
assertEquals(queries.get(i).content(), wrapper.query());
assertEquals(i, wrapper.index());
}
}

@Test
public void testQueryStreamWrapper() throws IOException {
var json = String.format("""
{"path":"%s","format":"folder","order":"linear","lang":"SPARQL"}
""", tempDir.toString().replaceAll("\\\\", "\\\\\\\\")); // windows
final var mapper = new ObjectMapper();
QueryHandler queryHandler = assertDoesNotThrow(() -> mapper.readValue(json, QueryHandler.class));
final var selector = queryHandler.getQuerySelectorInstance();
assertTrue(selector instanceof LinearQuerySelector);
assertEquals(queries.size(), queryHandler.getQueryCount());
assertNotEquals(0, queryHandler.hashCode());
for (int i = 0; i < queryHandler.getQueryCount(); i++) {
assertEquals(i, selector.getCurrentIndex());
final var wrapper = queryHandler.getNextQueryStream(selector);
final var acutalQuery = new String(wrapper.queryInputStream().readAllBytes(), StandardCharsets.UTF_8);
assertEquals(queries.get(i).content(), acutalQuery);
assertEquals(i, wrapper.index());
}
}

@Test
public void testQueryIDs() {
var json = String.format("""
{"path":"%s","format":"folder","order":"linear","lang":"SPARQL"}
""", tempDir.toString().replaceAll("\\\\", "\\\\\\\\")); // windows
final var mapper = new ObjectMapper();
QueryHandler queryHandler = assertDoesNotThrow(() -> mapper.readValue(json, QueryHandler.class));
final var selector = queryHandler.getQuerySelectorInstance();
assertTrue(selector instanceof LinearQuerySelector);
assertEquals(queries.size(), queryHandler.getQueryCount());
assertNotEquals(0, queryHandler.hashCode());
final var allQueryIDs = queryHandler.getAllQueryIds();
for (int i = 0; i < queryHandler.getQueryCount(); i++) {
assertEquals(queryHandler.hashCode() + ":" + i, allQueryIDs[i]);
assertEquals(allQueryIDs[i], queryHandler.getQueryId(i));
}
}
}

0 comments on commit 507910c

Please sign in to comment.