Skip to content

Commit 3d5b6ff

Browse files
kinowmr-c
authored andcommitted
Fix unit tests, moving queries to an implementation class for JSONB de/serialization.
1 parent ad442c3 commit 3d5b6ff

16 files changed

+420
-72
lines changed

pom.xml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
<artifactId>spring-data-commons</artifactId>
7171
<version>2.6.2</version>
7272
</dependency>
73+
<dependency>
74+
<groupId>com.vladmihalcea</groupId>
75+
<artifactId>hibernate-types-55</artifactId>
76+
<version>2.14.0</version>
77+
</dependency>
7378
<!-- Postgres -->
7479
<dependency>
7580
<groupId>org.postgresql</groupId>
@@ -131,16 +136,27 @@
131136
</dependency>
132137
<!-- For JSR-303, javax.validation -->
133138
<dependency>
134-
<groupId>org.hibernate.validator</groupId>
135-
<artifactId>hibernate-validator</artifactId>
136-
<version>7.0.2.Final</version>
139+
<groupId>org.springframework.boot</groupId>
140+
<artifactId>spring-boot-starter-validation</artifactId>
137141
</dependency>
138142
<!-- Test dependencies -->
139143
<dependency>
140144
<groupId>org.springframework.boot</groupId>
141145
<artifactId>spring-boot-starter-test</artifactId>
142146
<scope>test</scope>
143147
</dependency>
148+
<dependency>
149+
<groupId>org.testcontainers</groupId>
150+
<artifactId>junit-jupiter</artifactId>
151+
<version>1.16.2</version>
152+
<scope>test</scope>
153+
</dependency>
154+
<dependency>
155+
<groupId>org.testcontainers</groupId>
156+
<artifactId>postgresql</artifactId>
157+
<version>1.16.2</version>
158+
<scope>test</scope>
159+
</dependency>
144160
<dependency>
145161
<groupId>org.mockito</groupId>
146162
<artifactId>mockito-all</artifactId>

src/main/java/org/commonwl/view/CwlViewerApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121

2222
import org.springframework.boot.SpringApplication;
2323
import org.springframework.boot.autoconfigure.SpringBootApplication;
24-
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
2524
import org.springframework.scheduling.annotation.EnableAsync;
2625
import org.springframework.scheduling.annotation.EnableScheduling;
26+
import org.springframework.transaction.annotation.EnableTransactionManagement;
2727

2828
@SpringBootApplication
29-
@EnableMongoRepositories
3029
@EnableAsync
3130
@EnableScheduling
31+
@EnableTransactionManagement
3232
public class CwlViewerApplication {
3333

3434
public static void main(String[] args) {

src/main/java/org/commonwl/view/git/GitDetails.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
import java.io.Serializable;
2525
import java.net.URI;
2626
import java.net.URISyntaxException;
27+
import java.util.Objects;
2728

2829
/**
2930
* Represents all the parameters necessary to access a file/directory with Git
3031
*/
31-
@JsonIgnoreProperties(value = {"internalUrl"})
32+
@JsonIgnoreProperties(value = {"internalUrl"}, ignoreUnknown = true)
3233
public class GitDetails implements Serializable {
3334

3435
private String repoUrl;
@@ -226,4 +227,19 @@ public static String normaliseUrl(String url) {
226227
.replace("www.", "");
227228
}
228229

230+
@Override
231+
public boolean equals(Object o) {
232+
if (this == o) return true;
233+
if (o == null || getClass() != o.getClass()) return false;
234+
GitDetails that = (GitDetails) o;
235+
return Objects.equals(repoUrl, that.repoUrl) &&
236+
Objects.equals(branch, that.branch) &&
237+
Objects.equals(path, that.path) &&
238+
Objects.equals(packedId, that.packedId);
239+
}
240+
241+
@Override
242+
public int hashCode() {
243+
return Objects.hash(repoUrl, branch, path, packedId);
244+
}
229245
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.commonwl.view.util;
2+
3+
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
4+
import org.hibernate.annotations.TypeDef;
5+
import org.hibernate.annotations.TypeDefs;
6+
7+
import javax.persistence.MappedSuperclass;
8+
9+
@TypeDefs({
10+
@TypeDef(name = "json", typeClass = JsonBinaryType.class)
11+
})
12+
@MappedSuperclass
13+
public class BaseEntity {
14+
}

src/main/java/org/commonwl/view/workflow/QueuedWorkflow.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,59 @@
33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonInclude;
55
import org.commonwl.view.cwl.CWLToolStatus;
6-
import org.springframework.data.annotation.Id;
7-
6+
import org.commonwl.view.util.BaseEntity;
7+
import org.hibernate.annotations.GenericGenerator;
8+
import org.hibernate.annotations.Type;
9+
10+
import javax.persistence.Column;
11+
import javax.persistence.Convert;
12+
import javax.persistence.Entity;
13+
import javax.persistence.GeneratedValue;
14+
import javax.persistence.GenerationType;
15+
import javax.persistence.Id;
16+
import javax.persistence.Table;
17+
import java.io.Serializable;
818
import java.util.List;
19+
import java.util.Objects;
920

1021
/**
1122
* A workflow pending completion of cwltool
1223
*/
1324
@JsonIgnoreProperties(value = {"id", "tempRepresentation", "workflowList"})
1425
@JsonInclude(JsonInclude.Include.NON_EMPTY)
15-
public class QueuedWorkflow {
26+
@Entity
27+
@Table(name = "queued_workflow")
28+
@SuppressWarnings("JpaAttributeTypeInspection")
29+
public class QueuedWorkflow extends BaseEntity implements Serializable {
1630

1731
// ID for database
1832
@Id
33+
@GenericGenerator(name = "uuid2", strategy = "uuid2")
34+
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "uuid2")
35+
@Column(length = 36, nullable = false, updatable = false)
1936
public String id;
2037

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

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

2851
// Cwltool details
52+
@Column(columnDefinition = "jsonb", length = 10000)
53+
@Type(type = "json")
54+
@Convert(disableConversion = true)
2955
private CWLToolStatus cwltoolStatus = CWLToolStatus.RUNNING;
56+
@Column(length = 1000)
3057
private String cwltoolVersion = "";
58+
@Column(length = 1000)
3159
private String message;
3260

3361
public String getId() {
@@ -73,4 +101,22 @@ public List<WorkflowOverview> getWorkflowList() {
73101
public void setWorkflowList(List<WorkflowOverview> workflowList) {
74102
this.workflowList = workflowList;
75103
}
104+
105+
@Override
106+
public boolean equals(Object o) {
107+
if (this == o) return true;
108+
if (o == null || getClass() != o.getClass()) return false;
109+
QueuedWorkflow that = (QueuedWorkflow) o;
110+
return Objects.equals(id, that.id) &&
111+
Objects.equals(tempRepresentation, that.tempRepresentation) &&
112+
Objects.equals(workflowList, that.workflowList) &&
113+
cwltoolStatus == that.cwltoolStatus &&
114+
Objects.equals(cwltoolVersion, that.cwltoolVersion) &&
115+
Objects.equals(message, that.message);
116+
}
117+
118+
@Override
119+
public int hashCode() {
120+
return Objects.hash(id, tempRepresentation, workflowList, cwltoolStatus, cwltoolVersion, message);
121+
}
76122
}
Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
11
package org.commonwl.view.workflow;
22

3-
import org.commonwl.view.git.GitDetails;
4-
import org.springframework.data.mongodb.repository.Query;
5-
import org.springframework.data.repository.PagingAndSortingRepository;
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.Query;
65

76
import java.util.Date;
87
import java.util.List;
98

109
/**
1110
* Stores workflows in the queue waiting for cwltool
1211
*/
13-
public interface QueuedWorkflowRepository extends PagingAndSortingRepository<QueuedWorkflow, String> {
14-
15-
/**
16-
* Finds a queued workflow based on where it was retrieved from
17-
* @param retrievedFrom Details of where the queued workflow is from
18-
* @return The queued workflow
19-
*/
20-
@Query("{\"tempRepresentation.retrievedFrom\": ?0}")
21-
QueuedWorkflow findByRetrievedFrom(GitDetails retrievedFrom);
22-
23-
/**
24-
* Deletes a queued workflow based on where it was retrieved from
25-
* @param retrievedFrom Details of where the queued workflow is from
26-
*/
27-
void deleteByTempRepresentation_RetrievedFrom(GitDetails retrievedFrom);
12+
public interface QueuedWorkflowRepository extends JpaRepository<QueuedWorkflow, String>, QueuedWorkflowRepositoryCustom {
2813

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

36-
3723
/**
3824
* Finds and returns all queued workflows with date retrieved on older or equal to the Date argument passed.
25+
*
3926
* @param retrievedOn Details of where the queued workflow is from
4027
* @return A list of queued workflows
4128
*/
29+
@Query(value = "select q.* from queued_workflow q where q.tempRepresentation->>retrieved_on <= ?1", nativeQuery = true)
4230
List<QueuedWorkflow> findByTempRepresentation_RetrievedOnLessThanEqual(Date retrievedOn);
4331

44-
4532
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.commonwl.view.workflow;
2+
3+
import org.commonwl.view.git.GitDetails;
4+
import org.springframework.data.repository.query.Param;
5+
6+
public interface QueuedWorkflowRepositoryCustom {
7+
/**
8+
* Finds a queued workflow based on where it was retrieved from.
9+
*
10+
* @param retrievedFrom Details of where the queued workflow is from
11+
* @return The queued workflow
12+
*/
13+
QueuedWorkflow findByRetrievedFrom(@Param("retrievedFrom") GitDetails retrievedFrom);
14+
15+
/**
16+
* Deletes a queued workflow based on where it was retrieved from.
17+
*
18+
* @param retrievedFrom Details of where the queued workflow is from
19+
*/
20+
void deleteByTempRepresentation_RetrievedFrom(GitDetails retrievedFrom);
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.commonwl.view.workflow;
2+
3+
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
4+
import org.commonwl.view.git.GitDetails;
5+
import org.hibernate.query.Query;
6+
7+
import javax.persistence.EntityManager;
8+
import javax.persistence.PersistenceContext;
9+
10+
public class QueuedWorkflowRepositoryImpl implements QueuedWorkflowRepositoryCustom {
11+
12+
// Tested this query directly, and it works! Problem is elsewhere!
13+
private static final String QUERY_FIND_BY_RETRIEVED_FROM = "SELECT q.* FROM queued_workflow q WHERE q.temp_representation -> 'retrievedFrom' = :retrievedFrom";
14+
15+
private static final String QUERY_DELETE_BY_RETRIEVED_FROM = "DELETE FROM queued_workflow q WHERE q.temp_representation -> 'retrievedFrom' = :retrievedFrom";
16+
17+
@PersistenceContext
18+
EntityManager entityManager;
19+
20+
@Override
21+
public QueuedWorkflow findByRetrievedFrom(GitDetails retrievedFrom) {
22+
return (QueuedWorkflow) entityManager
23+
.createNativeQuery(QUERY_FIND_BY_RETRIEVED_FROM, QueuedWorkflow.class)
24+
.unwrap(Query.class)
25+
.setParameter("retrievedFrom", retrievedFrom, JsonBinaryType.INSTANCE)
26+
.getResultList()
27+
.stream()
28+
.findFirst()
29+
.orElse(null)
30+
;
31+
}
32+
33+
@Override
34+
public void deleteByTempRepresentation_RetrievedFrom(GitDetails retrievedFrom) {
35+
entityManager
36+
.createNativeQuery(QUERY_DELETE_BY_RETRIEVED_FROM, QueuedWorkflow.class)
37+
.unwrap(Query.class)
38+
.setParameter("retrievedFrom", retrievedFrom, JsonBinaryType.INSTANCE)
39+
.executeUpdate()
40+
;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)