Skip to content

Commit

Permalink
Fixed tests due to QanaryTripleStoreProxy, Added new test for QanaryP…
Browse files Browse the repository at this point in the history
…ipelineCompoennt
  • Loading branch information
dschiese committed Jun 28, 2024
1 parent ddf1a35 commit 8749f91
Show file tree
Hide file tree
Showing 6 changed files with 464 additions and 449 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package eu.wdaqua.qanary;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.Mockito.mock;

import java.util.LinkedList;
import java.util.List;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.values.Registration;
import de.codecentric.boot.admin.server.domain.values.StatusInfo;
import eu.wdaqua.qanary.business.QanaryComponent;
import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreProxy;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
Expand All @@ -17,79 +15,78 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.values.Registration;
import de.codecentric.boot.admin.server.domain.values.StatusInfo;
import eu.wdaqua.qanary.business.QanaryComponent;
import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector;
import java.util.LinkedList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.Mockito.mock;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = QanaryPipeline.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ComponentScan("eu.wdaqua.qanary")
@AutoConfigureWebClient
class QanaryComponentRegistrationChangeNotifierTest {

@Autowired
QanaryComponentRegistrationChangeNotifier myNotifier;

@MockBean
private QanaryTripleStoreConnector myQanaryTripleStoreConnector;

final static String COMPONENTNAME = "myMockedInstance";

/**
* add component and test its availability
*/
@Test
void testAddComponentAndCheckAvailability() {

assertNotEquals(null, myNotifier);
myNotifier.getAvailableComponents().clear();
assertEquals(0, myNotifier.getAvailableComponentNames().size());

Instance myMockedInstance = Mockito.mock(Instance.class);
String myMockedInstanceName = COMPONENTNAME;

Registration myMockedRegistration = mock(Registration.class);
Mockito.when(myMockedInstance.getRegistration()).thenReturn(myMockedRegistration);
StatusInfo myMockedStatusInfo = mock(StatusInfo.class);
Mockito.when(myMockedInstance.getStatusInfo()).thenReturn(myMockedStatusInfo);

myNotifier.addAvailableComponent(myMockedInstanceName, myMockedInstance);
Mockito.when(myMockedInstance.getRegistration().getName()).thenReturn(COMPONENTNAME);

assertEquals(1, myNotifier.getAvailableComponentNames().size());

assertNotEquals(null, myNotifier.getAvailableComponents().getOrDefault(COMPONENTNAME, null));
assertEquals(COMPONENTNAME, myNotifier.getAvailableComponentNames().get(0));
assertEquals(COMPONENTNAME, myNotifier.getAvailableComponents().get(COMPONENTNAME).getRegistration().getName());
}

/**
* set status of component to UP and test if it is considered as usable
*/
@Test
void testAddComponentAndCheckAvailabilityAndUp() {
this.testAddComponentAndCheckAvailability();

Instance myMockedInstance = myNotifier.getAvailableComponents().getOrDefault(COMPONENTNAME, null);
assertNotEquals(null, myMockedInstance);

// test for UP component
extracted(myMockedInstance, true, 1);
// test for DOWN component
extracted(myMockedInstance, false, 0);
}

private void extracted(Instance myMockedInstance, boolean up, int expectedNumberOfUsableComponents) {
Mockito.when(myMockedInstance.getStatusInfo().isUp()).thenReturn(up);

List<String> requestedComponentNames = new LinkedList<>();
requestedComponentNames.add(COMPONENTNAME);
List<QanaryComponent> availableComponents = myNotifier.getAvailableComponentsFromNames(requestedComponentNames);
assertEquals(expectedNumberOfUsableComponents, availableComponents.size());

// check if component status is correctly evaluated
assertEquals(up, myNotifier.isComponentUsable(myMockedInstance));
}
final static String COMPONENTNAME = "myMockedInstance";
@Autowired
QanaryComponentRegistrationChangeNotifier myNotifier;
@MockBean
private QanaryTripleStoreProxy myQanaryTripleStoreConnector;

/**
* add component and test its availability
*/
@Test
void testAddComponentAndCheckAvailability() {

assertNotEquals(null, myNotifier);
myNotifier.getAvailableComponents().clear();
assertEquals(0, myNotifier.getAvailableComponentNames().size());

Instance myMockedInstance = Mockito.mock(Instance.class);
String myMockedInstanceName = COMPONENTNAME;

Registration myMockedRegistration = mock(Registration.class);
Mockito.when(myMockedInstance.getRegistration()).thenReturn(myMockedRegistration);
StatusInfo myMockedStatusInfo = mock(StatusInfo.class);
Mockito.when(myMockedInstance.getStatusInfo()).thenReturn(myMockedStatusInfo);

myNotifier.addAvailableComponent(myMockedInstanceName, myMockedInstance);
Mockito.when(myMockedInstance.getRegistration().getName()).thenReturn(COMPONENTNAME);

assertEquals(1, myNotifier.getAvailableComponentNames().size());

assertNotEquals(null, myNotifier.getAvailableComponents().getOrDefault(COMPONENTNAME, null));
assertEquals(COMPONENTNAME, myNotifier.getAvailableComponentNames().get(0));
assertEquals(COMPONENTNAME, myNotifier.getAvailableComponents().get(COMPONENTNAME).getRegistration().getName());
}

/**
* set status of component to UP and test if it is considered as usable
*/
@Test
void testAddComponentAndCheckAvailabilityAndUp() {
this.testAddComponentAndCheckAvailability();

Instance myMockedInstance = myNotifier.getAvailableComponents().getOrDefault(COMPONENTNAME, null);
assertNotEquals(null, myMockedInstance);

// test for UP component
extracted(myMockedInstance, true, 1);
// test for DOWN component
extracted(myMockedInstance, false, 0);
}

private void extracted(Instance myMockedInstance, boolean up, int expectedNumberOfUsableComponents) {
Mockito.when(myMockedInstance.getStatusInfo().isUp()).thenReturn(up);

List<String> requestedComponentNames = new LinkedList<>();
requestedComponentNames.add(COMPONENTNAME);
List<QanaryComponent> availableComponents = myNotifier.getAvailableComponentsFromNames(requestedComponentNames);
assertEquals(expectedNumberOfUsableComponents, availableComponents.size());

// check if component status is correctly evaluated
assertEquals(up, myNotifier.isComponentUsable(myMockedInstance));
}
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,63 @@
package eu.wdaqua.qanary;

import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector;
import org.apache.http.HttpResponse;
import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnectorVirtuoso;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.ResourceFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.mockito.InjectMocks;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.reactive.function.client.WebClient;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.Objects;

import static org.mockito.Mockito.doNothing;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWireMock(port = 8080) //
public class QanaryPipelineComponentTest {

private final String questionID = "http://localhost:8080";
private final String EXAMPLE_QUESTION = "Example question";

protected ClassLoader classLoader = this.getClass().getClassLoader();
@MockBean
private QanaryTripleStoreConnector qanaryTripleStoreConnector;
QanaryTripleStoreConnectorVirtuoso qanaryTripleStoreConnectorVirtuoso;
@InjectMocks
QanaryPipelineComponent qanaryPipelineComponent;
URI testUri = new URI("testUri");

@Autowired
private QanaryPipelineComponent qanaryPipelineComponent;
public QanaryPipelineComponentTest() throws URISyntaxException {
}

@Before
public void setup() {
//doNothing().when(qanaryTripleStoreConnector).connect();
WebClient webClient = WebClient.builder().build();
ReflectionTestUtils.setField(qanaryPipelineComponent, "webClient", webClient);
doNothing().when(qanaryTripleStoreConnectorVirtuoso).connect();
}

private String convertResponseToString(HttpResponse response) throws IOException {
InputStream responseStream = response.getEntity().getContent();
Scanner scanner = new Scanner(responseStream, "UTF-8");
String responseString = scanner.useDelimiter("\\Z").next();
scanner.close();
return responseString;
@Test
public void constructQueryFromModelTest() throws IOException {
Model model = ModelFactory.createDefaultModel();
model.add(ResourceFactory.createStatement(
ResourceFactory.createResource("annotation"),
ResourceFactory.createProperty("oa:annotatedBy"),
ResourceFactory.createPlainLiteral("test")
));
String query = qanaryPipelineComponent.constructQueryFromModel(model, testUri, "/insert_constructed_triples.rq");
String expectedResult = readFileFromTestResources("constructQueryFromModelTestResultQuery");

Assertions.assertNotEquals(expectedResult, query);
}

private String readFileFromTestResources(String path) throws IOException {
File file = new File(Objects.requireNonNull(classLoader.getResource(path)).getFile());
return new String(Files.readAllBytes(file.toPath()));
}


}
Loading

0 comments on commit 8749f91

Please sign in to comment.