-
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.
ID-377 Add pact contract provider tests. (#60)
* ID-377 Add pact provider verification tests. * Add url resolution test. * ID-377 Add pact contract provider verification. * Turn on pact broker * Add pact broker url * Add pact broker user/pass * Tag unit tests vs pact tests * Fix pact broker url
- Loading branch information
1 parent
ac78352
commit b218ae7
Showing
22 changed files
with
236 additions
and
15 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
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
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
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
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
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
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
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
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
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
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
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
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
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
150 changes: 150 additions & 0 deletions
150
service/src/test/java/bio/terra/drshub/controllers/VerifyPactDrsHubApiController.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,150 @@ | ||
package bio.terra.drshub.controllers; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import au.com.dius.pact.provider.junit5.PactVerificationContext; | ||
import au.com.dius.pact.provider.junitsupport.Provider; | ||
import au.com.dius.pact.provider.junitsupport.State; | ||
import au.com.dius.pact.provider.junitsupport.loader.PactBroker; | ||
import au.com.dius.pact.provider.spring.junit5.MockMvcTestTarget; | ||
import au.com.dius.pact.provider.spring.junit5.PactVerificationSpringProvider; | ||
import bio.terra.common.iam.BearerTokenFactory; | ||
import bio.terra.drshub.config.DrsHubConfig; | ||
import bio.terra.drshub.config.DrsProvider; | ||
import bio.terra.drshub.config.ProviderAccessMethodConfig; | ||
import bio.terra.drshub.logging.AuditLogger; | ||
import bio.terra.drshub.models.AccessMethodConfigTypeEnum; | ||
import bio.terra.drshub.models.AccessUrlAuthEnum; | ||
import bio.terra.drshub.models.BondProviderEnum; | ||
import bio.terra.drshub.models.DrsApi; | ||
import bio.terra.drshub.models.DrsHubAuthorization; | ||
import bio.terra.drshub.services.AuthService; | ||
import bio.terra.drshub.services.DrsApiFactory; | ||
import bio.terra.drshub.services.DrsProviderService; | ||
import bio.terra.drshub.services.DrsResolutionService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.github.ga4gh.drs.model.AccessMethod; | ||
import io.github.ga4gh.drs.model.AccessURL; | ||
import io.github.ga4gh.drs.model.Authorizations.SupportedTypesEnum; | ||
import java.sql.Date; | ||
import java.text.SimpleDateFormat; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.TestTemplate; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
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.boot.test.mock.mockito.SpyBean; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
@Tag("Pact") | ||
@WebMvcTest | ||
@ContextConfiguration(classes = {DrsHubApiController.class, PublicApiController.class}) | ||
@Provider("drshub-provider") | ||
@PactBroker() | ||
class VerifyPactsDrsHubApiController { | ||
|
||
@MockBean private DrsHubConfig drsHubConfig; | ||
@MockBean private BearerTokenFactory tokenFactory; | ||
@MockBean private AuthService authService; | ||
@MockBean private DrsApi drsApi; | ||
@MockBean private DrsApiFactory drsApiFactory; | ||
@MockBean private AuditLogger auditLogger; | ||
@SpyBean private DrsResolutionService drsResolutionService; | ||
@SpyBean private DrsProviderService drsProviderService; | ||
|
||
@Autowired private ObjectMapper objectMapper; | ||
|
||
// This mockMVC is what we use to test API requests and responses: | ||
@Autowired private MockMvc mockMvc; | ||
|
||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | ||
|
||
@TestTemplate | ||
@ExtendWith(PactVerificationSpringProvider.class) | ||
void pactVerificationTestTemplate(PactVerificationContext context) { | ||
context.verifyInteraction(); | ||
} | ||
|
||
@BeforeEach | ||
void before(PactVerificationContext context) { | ||
context.setTarget(new MockMvcTestTarget(mockMvc)); | ||
} | ||
|
||
Optional<List<String>> getAuthForAccessMethodType(AccessMethod.TypeEnum accessMethodType) { | ||
return Optional.of(List.of("Bearer: test 123")); | ||
} | ||
|
||
@State({"Drshub is ok"}) | ||
public void checkStatusEndpoint() throws Exception {} | ||
|
||
@State({"resolve Drs url"}) | ||
public void resolveDrsUrl(Map<String, String> providerStateParams) throws Exception { | ||
when(authService.buildAuthorizations(any(), any(), any())) | ||
.thenReturn( | ||
List.of( | ||
new DrsHubAuthorization( | ||
SupportedTypesEnum.PASSPORTAUTH, this::getAuthForAccessMethodType))); | ||
|
||
when(authService.fetchUserServiceAccount(any(), any())).thenReturn(null); | ||
|
||
when(drsApi.getObject(any(), any())).thenReturn(null); | ||
|
||
var drsProvider = DrsProvider.create(); | ||
drsProvider.setHostRegex(".*\\.theanvil\\.io"); | ||
drsProvider.setMetadataAuth(false); | ||
drsProvider.setBondProvider(BondProviderEnum.anvil); | ||
drsProvider.setUseAliasesForLocalizationPath(true); | ||
|
||
var accessMethodConfig = ProviderAccessMethodConfig.create(); | ||
accessMethodConfig.setAuth(AccessUrlAuthEnum.passport); | ||
accessMethodConfig.setType(AccessMethodConfigTypeEnum.gs); | ||
accessMethodConfig.setFetchAccessUrl(true); | ||
|
||
var accessMethodConfigs = new ArrayList<ProviderAccessMethodConfig>(); | ||
accessMethodConfigs.add(accessMethodConfig); | ||
drsProvider.setAccessMethodConfigs(accessMethodConfigs); | ||
drsProvider.setName(providerStateParams.get("bondProvider")); | ||
|
||
when(drsHubConfig.getDrsProviders()).thenReturn(Map.of("anvil", drsProvider)); | ||
|
||
when(drsApiFactory.getApiFromUriComponents(any(), any())).thenReturn(drsApi); | ||
var drsObject = | ||
new io.github.ga4gh.drs.model.DrsObject() | ||
.id("1234567890") | ||
.checksums( | ||
List.of( | ||
new io.github.ga4gh.drs.model.Checksum() | ||
.checksum(providerStateParams.get("fileHash")) | ||
.type("md5"))) | ||
.createdTime(Date.from(Instant.now())) | ||
.description("test") | ||
.mimeType("application/json") | ||
.size(Long.parseLong(providerStateParams.get("fileSize"))) | ||
.updatedTime(dateFormat.parse(providerStateParams.get("timeCreated"))) | ||
.createdTime(dateFormat.parse(providerStateParams.get("timeCreated"))) | ||
.accessMethods( | ||
List.of( | ||
new AccessMethod() | ||
.accessId(providerStateParams.get("fileId")) | ||
.type(AccessMethod.TypeEnum.GS))) | ||
.version("1.0"); | ||
|
||
when(drsApi.getObject(any(), any())).thenReturn(drsObject); | ||
|
||
var accessUrl = | ||
new AccessURL() | ||
.url(providerStateParams.get("accessUrl")) | ||
.headers(List.of("Header", "Example")); | ||
|
||
when(drsApi.postAccessURL(any(), any(), any())).thenReturn(accessUrl); | ||
} | ||
} |
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
Oops, something went wrong.