Skip to content

Commit

Permalink
FAIRSPC-79: fixed all tests after migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreenwood committed Oct 17, 2024
1 parent 5725884 commit 4a94831
Show file tree
Hide file tree
Showing 30 changed files with 351 additions and 224 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package io.fairspace.saturn.services.metadata.validation;

import com.google.common.annotations.VisibleForTesting;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import io.fairspace.saturn.webdav.DavFactory;

@Component
public class URIPrefixValidator implements MetadataRequestValidator {

private final String restrictedPrefix;

@Autowired
public URIPrefixValidator(@Qualifier("davFactory") DavFactory davFactory) {
this.restrictedPrefix = ((io.milton.resource.Resource) davFactory.root).getUniqueId();
}

@VisibleForTesting
public URIPrefixValidator(String restrictedPrefix) {
this.restrictedPrefix = restrictedPrefix;
}

@Override
public void validate(Model before, Model after, Model removed, Model added, ViolationHandler violationHandler) {
added.listSubjects()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.fairspace.saturn;

import javax.sql.DataSource;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.testcontainers.containers.PostgreSQLContainer;
Expand Down Expand Up @@ -37,4 +41,20 @@ protected SearchProperties buildSearchProperties() {
searchProperties.setMaxJoinItems(50);
return searchProperties;
}

public DataSource getDataSource(ViewDatabaseProperties viewDatabaseProperties) {
var databaseConfig = getHikariConfig(viewDatabaseProperties);
return new HikariDataSource(databaseConfig);
}

private HikariConfig getHikariConfig(ViewDatabaseProperties viewDatabaseProperties) {
var databaseConfig = new HikariConfig();
databaseConfig.setJdbcUrl(viewDatabaseProperties.getUrl());
databaseConfig.setUsername(viewDatabaseProperties.getUsername());
databaseConfig.setPassword(viewDatabaseProperties.getPassword());
databaseConfig.setAutoCommit(viewDatabaseProperties.isAutoCommitEnabled());
databaseConfig.setConnectionTimeout(viewDatabaseProperties.getConnectionTimeout());
databaseConfig.setMaximumPoolSize(viewDatabaseProperties.getMaxPoolSize());
return databaseConfig;
}
}
16 changes: 12 additions & 4 deletions projects/saturn/src/test/java/io/fairspace/saturn/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.time.Instant;
import java.util.HashMap;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.jena.rdf.model.Model;
import org.jetbrains.annotations.NotNull;
Expand All @@ -15,9 +17,9 @@
import org.springframework.security.oauth2.jwt.Jwt;

import io.fairspace.saturn.auth.RequestContext;
import io.fairspace.saturn.config.ViewsConfig;
import io.fairspace.saturn.config.properties.JenaProperties;
import io.fairspace.saturn.config.properties.StoreParamsProperties;
import io.fairspace.saturn.config.properties.ViewsProperties;
import io.fairspace.saturn.rdf.SparqlUtils;
import io.fairspace.saturn.services.users.User;

Expand Down Expand Up @@ -120,15 +122,21 @@ public static void setupRequestContext() {
setupRequestContext(USER);
}

public static ViewsConfig loadViewsConfig(String path) {
public static ObjectMapper getYamlObjectMapper() {
return new ObjectMapper(new YAMLFactory());
}

public static ViewsProperties loadViewsConfig(String path) {
var settingsFile = new File(path);
if (settingsFile.exists()) {
try {
return ViewsConfig.MAPPER.readValue(settingsFile, ViewsConfig.class);
ViewsProperties viewsProperties = getYamlObjectMapper().readValue(settingsFile, ViewsProperties.class);
viewsProperties.init();
return viewsProperties;
} catch (IOException e) {
throw new RuntimeException("Error loading search configuration", e);
}
}
return new ViewsConfig();
return new ViewsProperties();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public class BaseControllerTest {
@MockBean
private JwtAuthConverterProperties jwtAuthConverterProperties;

@MockBean
private Services services;

@TestConfiguration
static class CustomObjectMapperConfig {
@Bean
Expand All @@ -31,8 +28,4 @@ public ObjectMapper objectMapper() {
.findAndRegisterModules(); // Automatically registers JavaTimeModule, etc.
}
}

protected Services getService() {
return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.fairspace.saturn.controller;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand All @@ -27,11 +26,6 @@ class MaintenanceControllerTest extends BaseControllerTest {
@MockBean
private MaintenanceService maintenanceService;

@BeforeEach
public void setUp() {
when(getService().getMaintenanceService()).thenReturn(maintenanceService);
}

@Test
void testStartReindex() throws Exception {
doNothing().when(maintenanceService).startRecreateIndexTask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -17,7 +16,6 @@
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
Expand All @@ -35,11 +33,6 @@ public class MetadataControllerTest extends BaseControllerTest {
@MockBean
private MetadataService metadataService;

@BeforeEach
void setUp() {
when(getService().getMetadataService()).thenReturn(metadataService);
}

@Test
public void testGetMetadata() throws Exception {
Model mockModel = ModelFactory.createDefaultModel(); // Create an empty Jena model for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class SearchControllerTest extends BaseControllerTest {

@Test
void testSearchFiles() throws Exception {
when(getService().getFileSearchService()).thenReturn(fileSearchService);
var mockResults = List.of(
SearchResultDto.builder()
.id("file1.txt")
Expand Down Expand Up @@ -79,7 +78,6 @@ void testSearchFiles() throws Exception {

@Test
void testLookupSearch() throws Exception {
when(getService().getSearchService()).thenReturn(searchService);
var mockResults = List.of(
SearchResultDto.builder()
.id("file1.txt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import io.fairspace.saturn.config.ViewsConfig;
import io.fairspace.saturn.config.properties.ViewsProperties;
import io.fairspace.saturn.controller.dto.CountDto;
import io.fairspace.saturn.controller.dto.FacetDto;
import io.fairspace.saturn.controller.dto.FacetsDto;
Expand Down Expand Up @@ -42,18 +41,12 @@ public class ViewControllerTest extends BaseControllerTest {
@MockBean
private ViewService viewService;

@MockBean
@MockBean(name = "queryService")
private QueryService queryService;

@Autowired
private ObjectMapper objectMapper;

@BeforeEach
public void setUp() {
when(getService().getViewService()).thenReturn(viewService);
when(getService().getQueryService()).thenReturn(queryService);
}

@Test
public void testGetViewsSuccess() throws Exception {
var viewDto = new ViewDto("view1", "View 1", List.of(), 100L);
Expand Down Expand Up @@ -104,8 +97,7 @@ public void testGetViewDataSuccess() throws Exception {
@Test
public void testGetFacetsSuccess() throws Exception {
// Mock data for getFacets
var facetDto = new FacetDto("facet1", "Facet 1", ViewsConfig.ColumnType.Set, List.of(), null, null, null);
var mockFacetsDto = new FacetsDto(List.of(facetDto));
var facetDto = new FacetDto("facet1", "Facet 1", ViewsProperties.ColumnType.Set, List.of(), null, null, null);

when(viewService.getFacets()).thenReturn(List.of(facetDto));

Expand All @@ -114,8 +106,8 @@ public void testGetFacetsSuccess() throws Exception {
.andExpect(jsonPath("$.facets", hasSize(1)))
.andExpect(jsonPath("$.facets[0].name", is("facet1")))
.andExpect(jsonPath("$.facets[0].title", is("Facet 1")))
.andExpect(
jsonPath("$.facets[0].type", is(ViewsConfig.ColumnType.Set.getName()))); // Empty options list
.andExpect(jsonPath(
"$.facets[0].type", is(ViewsProperties.ColumnType.Set.getName()))); // Empty options list
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package io.fairspace.saturn.controller;

import org.apache.jena.rdf.model.Model;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.test.web.servlet.MockMvc;

import static org.apache.jena.riot.RDFDataMgr.loadModel;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -16,6 +20,14 @@ class VocabularyControllerTest extends BaseControllerTest {
@Autowired
private MockMvc mockMvc;

@TestConfiguration
static class CustomVocabularyConfig {
@Bean
public Model vocabulary() {
return loadModel("vocabulary.ttl");
}
}

@Test
void testGetVocabularyWithJsonLd() throws Exception {
mockMvc.perform(get("/vocabulary/").header(HttpHeaders.ACCEPT, "application/ld+json"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
Expand Down Expand Up @@ -40,11 +39,6 @@ class WorkspaceControllerTest extends BaseControllerTest {
@MockBean
private WorkspaceService workspaceService;

@BeforeEach
void setUp() {
when(getService().getWorkspaceService()).thenReturn(workspaceService);
}

@Test
void createWorkspace_shouldReturnCreatedWorkspace() throws Exception {
Workspace workspace = new Workspace();
Expand Down
Loading

0 comments on commit 4a94831

Please sign in to comment.