Skip to content

Commit

Permalink
Fix unit tests, moving queries to an implementation class for JSONB d…
Browse files Browse the repository at this point in the history
…e/serialization.
  • Loading branch information
kinow committed Feb 12, 2022
1 parent f950557 commit b4ad46d
Show file tree
Hide file tree
Showing 16 changed files with 410 additions and 72 deletions.
22 changes: 19 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<artifactId>spring-data-commons</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-55</artifactId>
<version>2.14.0</version>
</dependency>
<!-- Postgres -->
<dependency>
<groupId>org.postgresql</groupId>
Expand Down Expand Up @@ -131,16 +136,27 @@
</dependency>
<!-- For JSR-303, javax.validation -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.2.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
4 changes: 2 additions & 2 deletions src/main/java/org/commonwl/view/CwlViewerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

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;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableMongoRepositories
@EnableAsync
@EnableScheduling
@EnableTransactionManagement
public class CwlViewerApplication {

public static void main(String[] args) {
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/org/commonwl/view/git/GitDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;

/**
* Represents all the parameters necessary to access a file/directory with Git
*/
@JsonIgnoreProperties(value = {"internalUrl"})
@JsonIgnoreProperties(value = {"internalUrl"}, ignoreUnknown = true)
public class GitDetails implements Serializable {

private String repoUrl;
Expand Down Expand Up @@ -226,4 +227,19 @@ public static String normaliseUrl(String url) {
.replace("www.", "");
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GitDetails that = (GitDetails) o;
return Objects.equals(repoUrl, that.repoUrl) &&
Objects.equals(branch, that.branch) &&
Objects.equals(path, that.path) &&
Objects.equals(packedId, that.packedId);
}

@Override
public int hashCode() {
return Objects.hash(repoUrl, branch, path, packedId);
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/commonwl/view/util/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.commonwl.view.util;

import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import javax.persistence.MappedSuperclass;

@TypeDefs({
@TypeDef(name = "json", typeClass = JsonBinaryType.class)
})
@MappedSuperclass
public class BaseEntity {
}
52 changes: 49 additions & 3 deletions src/main/java/org/commonwl/view/workflow/QueuedWorkflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,59 @@
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.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

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.io.Serializable;
import java.util.List;
import java.util.Objects;

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

// ID for database
@Id
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "uuid2")
@Column(length = 36, nullable = false, updatable = false)
public String id;

// Very barebones workflow to build loading thumbnail and overview
@Column(columnDefinition = "jsonb", length = 10000)
@Type(type = "json")
@Convert(disableConversion = true)
private Workflow tempRepresentation;

// List of packed workflows for packed workflows
// TODO: Refactor so this is not necessary
@Column(columnDefinition = "jsonb", length = 10000)
@Type(type = "json")
@Convert(disableConversion = true)
private List<WorkflowOverview> workflowList;

// Cwltool details
@Column(columnDefinition = "jsonb", length = 10000)
@Type(type = "json")
@Convert(disableConversion = true)
private CWLToolStatus cwltoolStatus = CWLToolStatus.RUNNING;
@Column(length = 1000)
private String cwltoolVersion = "";
@Column(length = 1000)
private String message;

public String getId() {
Expand Down Expand Up @@ -73,4 +101,22 @@ public List<WorkflowOverview> getWorkflowList() {
public void setWorkflowList(List<WorkflowOverview> workflowList) {
this.workflowList = workflowList;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
QueuedWorkflow that = (QueuedWorkflow) o;
return Objects.equals(id, that.id) &&
Objects.equals(tempRepresentation, that.tempRepresentation) &&
Objects.equals(workflowList, that.workflowList) &&
cwltoolStatus == that.cwltoolStatus &&
Objects.equals(cwltoolVersion, that.cwltoolVersion) &&
Objects.equals(message, that.message);
}

@Override
public int hashCode() {
return Objects.hash(id, tempRepresentation, workflowList, cwltoolStatus, cwltoolVersion, message);
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
package org.commonwl.view.workflow;

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

import java.util.Date;
import java.util.List;

/**
* Stores workflows in the queue waiting for cwltool
*/
public interface QueuedWorkflowRepository extends PagingAndSortingRepository<QueuedWorkflow, String> {

/**
* Finds a queued workflow based on where it was retrieved from
* @param retrievedFrom Details of where the queued workflow is from
* @return The queued workflow
*/
@Query("{\"tempRepresentation.retrievedFrom\": ?0}")
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
*/
void deleteByTempRepresentation_RetrievedFrom(GitDetails retrievedFrom);
public interface QueuedWorkflowRepository extends JpaRepository<QueuedWorkflow, String>, QueuedWorkflowRepositoryCustom {

/**
* 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 <= ?1", nativeQuery = true)
Long deleteByTempRepresentation_RetrievedOnLessThanEqual(Date retrievedOn);


/**
* Finds and returns all queued workflows with date retrieved on older or equal to the Date argument passed.
*
* @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 <= ?1", nativeQuery = true)
List<QueuedWorkflow> findByTempRepresentation_RetrievedOnLessThanEqual(Date retrievedOn);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.commonwl.view.workflow;

import org.commonwl.view.git.GitDetails;
import org.springframework.data.repository.query.Param;

public interface QueuedWorkflowRepositoryCustom {
/**
* Finds a queued workflow based on where it was retrieved from.
*
* @param retrievedFrom Details of where the queued workflow is from
* @return The queued workflow
*/
QueuedWorkflow findByRetrievedFrom(@Param("retrievedFrom") GitDetails retrievedFrom);

/**
* Deletes a queued workflow based on where it was retrieved from.
*
* @param retrievedFrom Details of where the queued workflow is from
*/
void deleteByTempRepresentation_RetrievedFrom(GitDetails retrievedFrom);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.commonwl.view.workflow;

import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import org.commonwl.view.git.GitDetails;
import org.hibernate.query.Query;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class QueuedWorkflowRepositoryImpl implements QueuedWorkflowRepositoryCustom {

// Tested this query directly, and it works! Problem is elsewhere!
private static final String QUERY_FIND_BY_RETRIEVED_FROM = "SELECT q.* FROM queued_workflow q WHERE q.temp_representation -> 'retrievedFrom' = :retrievedFrom";

private static final String QUERY_DELETE_BY_RETRIEVED_FROM = "DELETE FROM queued_workflow q WHERE q.temp_representation -> 'retrievedFrom' = :retrievedFrom";

@PersistenceContext
EntityManager entityManager;

@Override
public QueuedWorkflow findByRetrievedFrom(GitDetails retrievedFrom) {
return (QueuedWorkflow) entityManager
.createNativeQuery(QUERY_FIND_BY_RETRIEVED_FROM, QueuedWorkflow.class)
.unwrap(Query.class)
.setParameter("retrievedFrom", retrievedFrom, JsonBinaryType.INSTANCE)
.getResultList()
.stream()
.findFirst()
.orElse(null)
;
}

@Override
public void deleteByTempRepresentation_RetrievedFrom(GitDetails retrievedFrom) {
entityManager
.createNativeQuery(QUERY_DELETE_BY_RETRIEVED_FROM, QueuedWorkflow.class)
.unwrap(Query.class)
.setParameter("retrievedFrom", retrievedFrom, JsonBinaryType.INSTANCE)
.executeUpdate()
;
}

}
Loading

0 comments on commit b4ad46d

Please sign in to comment.