-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
https://broadworkbench.atlassian.net/browse/AJ-1188 This PR add consumer-side contract tests against TDR, which are verified in DataBiosphere/jade-data-repo#1528. See also the pact verification at: https://pact-broker.dsp-eng-tools.broadinstitute.org/dashboard/provider/datarepo/consumer/terra-workspace-data-service.
- Loading branch information
1 parent
0ca3fbc
commit 028bced
Showing
2 changed files
with
145 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
service/src/test/java/org/databiosphere/workspacedataservice/pact/TDRPactTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package org.databiosphere.workspacedataservice.pact; | ||
|
||
import static au.com.dius.pact.consumer.dsl.LambdaDsl.newJsonBody; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import au.com.dius.pact.consumer.MockServer; | ||
import au.com.dius.pact.consumer.dsl.PactDslJsonBody; | ||
import au.com.dius.pact.consumer.dsl.PactDslWithProvider; | ||
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt; | ||
import au.com.dius.pact.consumer.junit5.PactTestFor; | ||
import au.com.dius.pact.core.model.PactSpecVersion; | ||
import au.com.dius.pact.core.model.RequestResponsePact; | ||
import au.com.dius.pact.core.model.annotations.Pact; | ||
import bio.terra.datarepo.model.SnapshotModel; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.databiosphere.workspacedataservice.datarepo.DataRepoClientFactory; | ||
import org.databiosphere.workspacedataservice.datarepo.DataRepoDao; | ||
import org.databiosphere.workspacedataservice.datarepo.DataRepoException; | ||
import org.databiosphere.workspacedataservice.datarepo.HttpDataRepoClientFactory; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
@Tag("pact-test") | ||
@ExtendWith(PactConsumerTestExt.class) | ||
class TDRPactTest { | ||
|
||
@BeforeEach | ||
void setUp() { | ||
// Without this setup, the HttpClient throws a "No thread-bound request found" error | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
// Set the mock request as the current request context | ||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); | ||
} | ||
|
||
static final UUID dummySnapshotId = UUID.fromString("12345678-abc9-012d-3456-e7fab89cd01e"); | ||
|
||
@Pact(consumer = "wds", provider = "datarepo") | ||
public RequestResponsePact noSnapshotPact(PactDslWithProvider builder) { | ||
return builder | ||
.given("snapshot with given id doesn't exist", Map.of("id", dummySnapshotId.toString())) | ||
.uponReceiving("a snapshot request") | ||
.path("/api/repository/v1/snapshots/" + dummySnapshotId) | ||
.query("include=TABLES") | ||
.method("GET") | ||
.willRespondWith() | ||
.status(404) | ||
.toPact(); | ||
} | ||
|
||
@Pact(consumer = "wds", provider = "datarepo") | ||
public RequestResponsePact noAccessToSnapshotPact(PactDslWithProvider builder) { | ||
return builder | ||
.given( | ||
"user does not have access to snapshot with given id", | ||
Map.of("id", dummySnapshotId.toString())) | ||
.uponReceiving("a snapshot request") | ||
.path("/api/repository/v1/snapshots/" + dummySnapshotId) | ||
.matchQuery("include", "TABLES") | ||
.method("GET") | ||
.willRespondWith() | ||
.status(403) | ||
.toPact(); | ||
} | ||
|
||
@Pact(consumer = "wds", provider = "datarepo") | ||
public RequestResponsePact userHasAccessToSnapshotPact(PactDslWithProvider builder) { | ||
var snapshotResponseShape = | ||
new PactDslJsonBody().stringValue("id", dummySnapshotId.toString()).stringType("name"); | ||
return builder | ||
.given( | ||
"user has access to snapshot with given id", Map.of("id", dummySnapshotId.toString())) | ||
.uponReceiving("a snapshot request") | ||
.path("/api/repository/v1/snapshots/" + dummySnapshotId) | ||
.query("include=TABLES") | ||
.method("GET") | ||
.willRespondWith() | ||
.status(200) | ||
.body( | ||
newJsonBody( | ||
snapshot -> { | ||
snapshot.stringValue("id", dummySnapshotId.toString()); | ||
snapshot.stringType("name"); | ||
snapshot.minArrayLike( | ||
"tables", | ||
/* minSize= */ 1, | ||
table -> { | ||
table.stringType("name"); | ||
}); | ||
}) | ||
.build()) | ||
.toPact(); | ||
} | ||
|
||
@Test | ||
@PactTestFor(pactMethod = "noSnapshotPact", pactVersion = PactSpecVersion.V3) | ||
void testNoSnapshot(MockServer mockServer) { | ||
DataRepoClientFactory clientFactory = new HttpDataRepoClientFactory(mockServer.getUrl()); | ||
DataRepoDao dataRepoDao = new DataRepoDao(clientFactory); | ||
|
||
assertThrows( | ||
DataRepoException.class, | ||
() -> dataRepoDao.getSnapshot(dummySnapshotId), | ||
"nonexistent snapshot should return 404"); | ||
} | ||
|
||
@Test | ||
@PactTestFor(pactMethod = "noAccessToSnapshotPact", pactVersion = PactSpecVersion.V3) | ||
void testNoAccessToSnapshot(MockServer mockServer) { | ||
DataRepoClientFactory clientFactory = new HttpDataRepoClientFactory(mockServer.getUrl()); | ||
DataRepoDao dataRepoDao = new DataRepoDao(clientFactory); | ||
|
||
assertThrows( | ||
DataRepoException.class, | ||
() -> dataRepoDao.getSnapshot(dummySnapshotId), | ||
"nonexistent snapshot should return 403"); | ||
} | ||
|
||
@Test | ||
@PactTestFor(pactMethod = "userHasAccessToSnapshotPact", pactVersion = PactSpecVersion.V3) | ||
void testUserHasAccessToSnapshot(MockServer mockServer) { | ||
DataRepoClientFactory clientFactory = new HttpDataRepoClientFactory(mockServer.getUrl()); | ||
DataRepoDao dataRepoDao = new DataRepoDao(clientFactory); | ||
|
||
SnapshotModel snapshot = dataRepoDao.getSnapshot(dummySnapshotId); | ||
assertNotNull(snapshot, "Snapshot request should return a snapshot"); | ||
assertNotNull(snapshot.getName(), "Snapshot response should have a name"); | ||
assertEquals(dummySnapshotId, snapshot.getId(), "Snapshot id should match the requested id"); | ||
|
||
var tables = snapshot.getTables(); | ||
assertFalse(tables.isEmpty()); | ||
tables.forEach(table -> assertNotNull(table.getName())); | ||
} | ||
} |