Skip to content

Commit

Permalink
WIP staging work
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Jan 20, 2022
1 parent c26e386 commit c67871e
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 36 deletions.
17 changes: 14 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,27 @@
</dependency>
<!-- For JSR-303, javax.validation -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.1.Final</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.16.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.16.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/commonwl/view/CwlViewerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableMongoRepositories
@EnableAsync
@EnableScheduling
public class CwlViewerApplication {
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/org/commonwl/view/util/JpaConverterJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.commonwl.view.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
* @see <a href="https://stackoverflow.com/questions/25738569/how-to-map-a-map-json-column-to-java-object-with-jpa">https://stackoverflow.com/questions/25738569/how-to-map-a-map-json-column-to-java-object-with-jpa</a>
*/
@Converter(autoApply = true)
public class JpaConverterJson implements AttributeConverter<Object, String> {

private static final ObjectMapper objectMapper = new ObjectMapper();

@Override
public String convertToDatabaseColumn(Object meta) {
try {
return objectMapper.writeValueAsString(meta);
} catch (JsonProcessingException ex) {
return null;
}
}

@Override
public Object convertToEntityAttribute(String dbData) {
try {
return objectMapper.readValue(dbData, Object.class);
} catch (JsonProcessingException ex) {
return null;
}
}
}
17 changes: 15 additions & 2 deletions src/main/java/org/commonwl/view/workflow/QueuedWorkflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,39 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.commonwl.view.cwl.CWLToolStatus;
import org.springframework.data.annotation.Id;

import org.commonwl.view.util.JpaConverterJson;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.List;

/**
* A workflow pending completion of cwltool
*/
@JsonIgnoreProperties(value = {"id", "tempRepresentation", "workflowList"})
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Entity
@Table(name = "queued_workflow")
@SuppressWarnings("JpaAttributeTypeInspection")
public class QueuedWorkflow {

// ID for database
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public String id;

// Very barebones workflow to build loading thumbnail and overview
private Workflow tempRepresentation;

// List of packed workflows for packed workflows
// TODO: Refactor so this is not necessary
@Column(columnDefinition = "jsonb")
@Convert(converter= JpaConverterJson.class)
private List<WorkflowOverview> workflowList;

// Cwltool details
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.commonwl.view.workflow;

import org.commonwl.view.git.GitDetails;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.Date;
Expand All @@ -17,20 +17,22 @@ public interface QueuedWorkflowRepository extends PagingAndSortingRepository<Que
* @param retrievedFrom Details of where the queued workflow is from
* @return The queued workflow
*/
@Query("{\"tempRepresentation.retrievedFrom\": ?0}")
@Query(value = "select q.* from queued_workflow q where q.tempRepresentation->>retrieved_from = ?0", nativeQuery = true)
QueuedWorkflow findByRetrievedFrom(GitDetails retrievedFrom);

/**
* Deletes a queued workflow based on where it was retrieved from
* @param retrievedFrom Details of where the queued workflow is from
*/
@Query(value = "delete from queued_workflow q where q.tempRepresentation->>retrieved_from = ?0", nativeQuery = true)
void deleteByTempRepresentation_RetrievedFrom(GitDetails retrievedFrom);

/**
* Deletes all queued workflows with date retrieved on older or equal to the Date argument passed.
* @param retrievedOn Date of when the queued workflow was retrieved
* @return The number of queued workflows deleted
*/
@Query(value = "delete from queued_workflow q where q.tempRepresentation->>retrieved_on <= ?0", nativeQuery = true)
Long deleteByTempRepresentation_RetrievedOnLessThanEqual(Date retrievedOn);


Expand All @@ -39,6 +41,7 @@ public interface QueuedWorkflowRepository extends PagingAndSortingRepository<Que
* @param retrievedOn Details of where the queued workflow is from
* @return A list of queued workflows
*/
@Query(value = "select q.* from queued_workflow q where q.tempRepresentation->>retrieved_on <= ?0", nativeQuery = true)
List<QueuedWorkflow> findByTempRepresentation_RetrievedOnLessThanEqual(Date retrievedOn);


Expand Down
30 changes: 24 additions & 6 deletions src/main/java/org/commonwl/view/workflow/Workflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@
import org.commonwl.view.cwl.CWLElement;
import org.commonwl.view.cwl.CWLStep;
import org.commonwl.view.git.GitDetails;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.commonwl.view.util.JpaConverterJson;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import java.util.Date;
import java.util.Map;

Expand All @@ -40,18 +46,24 @@
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(value = {"id", "roBundlePath", "roBundleLink"})
@Document
@Entity
@Table(name = "workflow",
indexes = {
@Index(columnList = "retrievedFrom", unique = true),
@Index(columnList = "retrievedOn")
})
@SuppressWarnings("JpaAttributeTypeInspection")
public class Workflow {

// ID for database
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public String id;

// Metadata
@Indexed(unique = true)
private GitDetails retrievedFrom;

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss z")
@Indexed
private Date retrievedOn;

// The last commit from the branch at the time of fetching
Expand All @@ -65,8 +77,14 @@ public class Workflow {
// Contents of the workflow
private String label;
private String doc;
@Column(columnDefinition = "jsonb")
@Convert(converter= JpaConverterJson.class)
private Map<String, CWLElement> inputs;
@Column(columnDefinition = "jsonb")
@Convert(converter= JpaConverterJson.class)
private Map<String, CWLElement> outputs;
@Column(columnDefinition = "jsonb")
@Convert(converter= JpaConverterJson.class)
private Map<String, CWLStep> steps;

// Currently only DockerRequirement is parsed for this
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/commonwl/view/workflow/WorkflowRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

package org.commonwl.view.workflow;

import java.util.List;

import org.commonwl.view.git.GitDetails;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

/**
* Stores and retrieved workflow objects from the database
*
Expand All @@ -48,7 +48,7 @@ public interface WorkflowRepository extends PagingAndSortingRepository<Workflow,
* @param path The path to the workflow within the repository
* @return The workflow model
*/
@Query("{\"lastCommit\": ?0, \"retrievedFrom.path\": ?1}")
@Query(value = "select * from workflow w where w.lastCommit = ?1 and w.retrievedFrom->>path = ?2", nativeQuery = true)
List<Workflow> findByCommitAndPath(String commitId, String path);

/**
Expand All @@ -58,7 +58,7 @@ public interface WorkflowRepository extends PagingAndSortingRepository<Workflow,
* The latest commit ID of the workflow
* @return The workflow model
*/
@Query("{\"lastCommit\": ?0}")
@Query("select w from Workflow w where w.lastCommit = ?1")
List<Workflow> findByCommit(String commitId);

/**
Expand Down
15 changes: 6 additions & 9 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,18 @@ gitAPI.cloneSubmodules = true
#=======================
# Postgres settings
#=======================
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:postgresql://localhost:5432/cwlviewer
spring.datasource.username=sa
spring.datasource.password=sa

spring.sql.init.mode=always
spring.sql.init.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/cwlviewer
spring.datasource.username=postgres
spring.datasource.password=s$cret

spring.jpa.show-sql=false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.data.mongodb.host = localhost
spring.data.mongodb.port = 27017
# For complete list of MongoDB properties, see
# https://github.com/spring-projects/spring-boot/blob/v1.4.2.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java

#=======================
# SPARQL endpoint
#=======================
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.commonwl.view.workflow;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.testcontainers.containers.PostgreSQLContainer;

/**
* A test application context initializer that creates the database Docker container used for functional tests.
*/
public class PostgreSQLContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:9.6.12")
.withDatabaseName("cwlviewer")
.withUsername("sa")
.withPassword("sa");
postgreSQLContainer.start();
TestPropertyValues.of("spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword(),
"spring.jpa.hibernate.ddl-auto=create")
.applyTo(applicationContext.getEnvironment());
applicationContext.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
postgreSQLContainer.stop();
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,39 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

// N.B. "To use embedded mongo, the spring.mongodb.embedded.version property must now be set."
// https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes#embedded-mongo
@Testcontainers
@SpringBootTest(
properties={
"spring.mongodb.embedded.version=3.2.2",
"spring.data.mongodb.port=0"
},
classes={WebConfig.class, CwlViewerApplication.class, QueuedWorkflowRepository.class}
)
@ContextConfiguration(initializers = PostgreSQLContextInitializer.class)
public class QueuedWorkflowRepositoryTest {

@Container
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:9.6.12")
.withDatabaseName("cwlviewer")
.withUsername("sa")
.withPassword("sa");

@Autowired
QueuedWorkflowRepository repository;

@Test
@Transactional
public void deleteQueuedWorkflowByRetrievedFromTest() {

assertNotNull(repository);

// create stub queued workflow
GitDetails gitDetails = new GitDetails("test_repo_url", "test_branch", "test_path");
GitDetails gitDetails = new GitDetails("https://github.com/common-workflow-language/cwlviewer/", "main", "/");
gitDetails.setPackedId("test_packedId");

Workflow workflow = new Workflow();
Expand Down

0 comments on commit c67871e

Please sign in to comment.