-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Samples: add automated test to sample 04.0-file-transfer #1565
Merged
paullatzelsperger
merged 9 commits into
eclipse-edc:main
from
agera-edc:feature/1269-automated-tests-sample-040
Jul 4, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
615c52e
04.0-file-transfer: add automated test in sample 04.0 (#262)
pkirch 7fe1bf8
Merge branch 'eclipse-dataspaceconnector:main' into feature/1269-auto…
pkirch f9b66b9
samples: fix checkstyle errors (#315)
pkirch d86981a
move DataAddress change code into test
pkirch 5c53504
move DataRequest change code into test
pkirch 419499b
revert whitespace
pkirch d29c5c8
reverted bug fix for shallow copy incl. regression tests
pkirch 91a5348
reverted whitespace
pkirch 766879c
reverted whitespace
pkirch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 31 additions & 0 deletions
31
samples/04.0-file-transfer/integration-tests/build.gradle.kts
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,31 @@ | ||
/* | ||
* Copyright (c) 2022 Microsoft Corporation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial test implementation for sample | ||
* | ||
*/ | ||
|
||
plugins { | ||
`java-library` | ||
} | ||
|
||
val restAssured: String by project | ||
val awaitility: String by project | ||
|
||
|
||
dependencies { | ||
testImplementation(project(":extensions:junit")) | ||
testImplementation(testFixtures(project(":common:util"))) | ||
testImplementation("io.rest-assured:rest-assured:${restAssured}") | ||
testImplementation("org.awaitility:awaitility:${awaitility}") | ||
|
||
testCompileOnly(project(":samples:04.0-file-transfer:consumer")) | ||
testCompileOnly(project(":samples:04.0-file-transfer:provider")) | ||
} |
239 changes: 239 additions & 0 deletions
239
...est/java/org/eclipse/dataspaceconnector/extension/sample/test/FileTransferSampleTest.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,239 @@ | ||
/* | ||
* Copyright (c) 2022 Microsoft Corporation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial test implementation for sample | ||
* | ||
*/ | ||
|
||
package org.eclipse.dataspaceconnector.extension.sample.test; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.restassured.RestAssured; | ||
import io.restassured.http.ContentType; | ||
import io.restassured.path.json.JsonPath; | ||
import org.apache.http.HttpStatus; | ||
import org.eclipse.dataspaceconnector.common.util.junit.annotations.EndToEndTest; | ||
import org.eclipse.dataspaceconnector.junit.extensions.EdcRuntimeExtension; | ||
import org.eclipse.dataspaceconnector.junit.testfixtures.TestUtils; | ||
import org.eclipse.dataspaceconnector.spi.types.domain.DataAddress; | ||
import org.eclipse.dataspaceconnector.spi.types.domain.transfer.DataRequest; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.hamcrest.Matchers.emptyString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
@EndToEndTest | ||
public class FileTransferSampleTest { | ||
|
||
static final ObjectMapper MAPPER = new ObjectMapper(); | ||
static final String INITIATE_CONTRACT_NEGOTIATION_URI = "http://localhost:9192/api/v1/data/contractnegotiations"; | ||
static final String LOOK_UP_CONTRACT_AGREEMENT_URI = "http://localhost:9192/api/v1/data/contractnegotiations/{id}"; | ||
static final String INITIATE_TRANSFER_PROCESS_URI = "http://localhost:9192/api/v1/data/transferprocess"; | ||
static final String CONTRACT_OFFER_FILE_PATH = "samples/04.0-file-transfer/contractoffer.json"; | ||
static final String CONSUMER_CONFIG_PROPERTIES_FILE_PATH = "samples/04.0-file-transfer/consumer/config.properties"; | ||
static final String PROVIDER_CONFIG_PROPERTIES_FILE_PATH = "samples/04.0-file-transfer/provider/config.properties"; | ||
static final String TRANSFER_FILE_PATH = "samples/04.0-file-transfer/filetransfer.json"; | ||
// Reuse an already existing file for the test. Could be set to any other existing file in the repository. | ||
static final String SAMPLE_ASSET_FILE_PATH = TRANSFER_FILE_PATH; | ||
static final String DESTINATION_FILE_PATH = "samples/04.0-file-transfer/consumer/requested.test.txt"; | ||
static final Duration TIMEOUT = Duration.ofSeconds(15); | ||
static final Duration POLL_INTERVAL = Duration.ofMillis(500); | ||
static final String API_KEY_HEADER_KEY = "X-Api-Key"; | ||
static final String API_KEY_HEADER_VALUE = "password"; | ||
@RegisterExtension | ||
static EdcRuntimeExtension provider = new EdcRuntimeExtension( | ||
":samples:04.0-file-transfer:provider", | ||
"provider", | ||
Map.of( | ||
// Override 'edc.samples.04.asset.path' implicitly set via property 'edc.fs.config'. | ||
"edc.samples.04.asset.path", getFileFromRelativePath(SAMPLE_ASSET_FILE_PATH).getAbsolutePath(), | ||
"edc.fs.config", getFileFromRelativePath(PROVIDER_CONFIG_PROPERTIES_FILE_PATH).getAbsolutePath() | ||
) | ||
); | ||
@RegisterExtension | ||
static EdcRuntimeExtension consumer = new EdcRuntimeExtension( | ||
":samples:04.0-file-transfer:consumer", | ||
"consumer", | ||
Map.of( | ||
"edc.fs.config", getFileFromRelativePath(CONSUMER_CONFIG_PROPERTIES_FILE_PATH).getAbsolutePath() | ||
) | ||
); | ||
static final File DESTINATION_FILE = getFileFromRelativePath(FileTransferSampleTest.DESTINATION_FILE_PATH); | ||
static final File SAMPLE_ASSET_FILE = getFileFromRelativePath(FileTransferSampleTest.SAMPLE_ASSET_FILE_PATH); | ||
String contractNegotiationId; | ||
String contractAgreementId; | ||
|
||
/** | ||
* Resolves a {@link File} instance from a relative path. | ||
*/ | ||
@NotNull | ||
static File getFileFromRelativePath(String relativePath) { | ||
return new File(TestUtils.findBuildRoot(), relativePath); | ||
} | ||
|
||
/** | ||
* Run all sample steps in one single test. | ||
* Note: Sample steps cannot be separated into single tests because {@link EdcRuntimeExtension} | ||
* runs before each single test. | ||
*/ | ||
@Test | ||
void runSampleSteps() throws Exception { | ||
assertTestPrerequisites(); | ||
|
||
initiateContractNegotiation(); | ||
lookUpContractAgreementId(); | ||
requestTransferFile(); | ||
assertDestinationFileContent(); | ||
|
||
cleanTemporaryTestFiles(); | ||
} | ||
|
||
/** | ||
* Assert that prerequisites are fulfilled before running the test. | ||
* This assertion checks only whether the file to be copied is not existing already. | ||
*/ | ||
void assertTestPrerequisites() { | ||
assertThat(DESTINATION_FILE).doesNotExist(); | ||
} | ||
|
||
/** | ||
* Remove files created while running the tests. | ||
* The copied file will be deleted. | ||
*/ | ||
@SuppressWarnings("ResultOfMethodCallIgnored") | ||
void cleanTemporaryTestFiles() { | ||
DESTINATION_FILE.delete(); | ||
} | ||
|
||
/** | ||
* Assert that the file to be copied exists at the expected location. | ||
* This method waits a duration which is defined in {@link FileTransferSampleTest#TIMEOUT}. | ||
*/ | ||
void assertDestinationFileContent() { | ||
await().atMost(TIMEOUT).pollInterval(POLL_INTERVAL).untilAsserted(() | ||
-> assertThat(DESTINATION_FILE).hasSameBinaryContentAs(SAMPLE_ASSET_FILE)); | ||
} | ||
|
||
/** | ||
* Assert that a POST request to initiate a contract negotiation is successful. | ||
* This method corresponds to the command in the sample: {@code curl -X POST -H "Content-Type: application/json" -H "X-Api-Key: password" -d @samples/04.0-file-transfer/contractoffer.json "http://localhost:9192/api/v1/data/contractnegotiations"} | ||
*/ | ||
void initiateContractNegotiation() { | ||
contractNegotiationId = RestAssured | ||
.given() | ||
.headers(API_KEY_HEADER_KEY, API_KEY_HEADER_VALUE) | ||
.contentType(ContentType.JSON) | ||
.body(new File(TestUtils.findBuildRoot(), CONTRACT_OFFER_FILE_PATH)) | ||
.when() | ||
.post(INITIATE_CONTRACT_NEGOTIATION_URI) | ||
.then() | ||
.statusCode(HttpStatus.SC_OK) | ||
.body("id", not(emptyString())) | ||
.extract() | ||
.jsonPath() | ||
.get("id"); | ||
} | ||
|
||
/** | ||
* Assert that a GET request to look up a contract agreement is successful. | ||
* This method corresponds to the command in the sample: {@code curl -X GET -H 'X-Api-Key: password' "http://localhost:9192/api/v1/data/contractnegotiations/{UUID}"} | ||
*/ | ||
void lookUpContractAgreementId() { | ||
// Wait for transfer to be completed. | ||
await().atMost(TIMEOUT).pollInterval(POLL_INTERVAL) | ||
.untilAsserted(() -> | ||
contractAgreementId = RestAssured | ||
.given() | ||
.headers(API_KEY_HEADER_KEY, API_KEY_HEADER_VALUE) | ||
.when() | ||
.get(LOOK_UP_CONTRACT_AGREEMENT_URI, contractNegotiationId) | ||
.then() | ||
.statusCode(HttpStatus.SC_OK) | ||
.body("state", equalTo("CONFIRMED")) | ||
.body("contractAgreementId", not(emptyString())) | ||
.extract().body().jsonPath().getString("contractAgreementId") | ||
); | ||
} | ||
|
||
/** | ||
* Assert that a POST request to initiate transfer process is successful. | ||
* This method corresponds to the command in the sample: {@code curl -X POST -H "Content-Type: application/json" -H "X-Api-Key: password" -d @samples/04.0-file-transfer/filetransfer.json "http://localhost:9192/api/v1/data/transferprocess"} | ||
* | ||
* @throws IOException Thrown if there was an error accessing the transfer request file defined in {@link FileTransferSampleTest#TRANSFER_FILE_PATH}. | ||
*/ | ||
void requestTransferFile() throws IOException { | ||
File transferJsonFile = getFileFromRelativePath(TRANSFER_FILE_PATH); | ||
DataRequest sampleDataRequest = readAndUpdateDataRequestFromJsonFile(transferJsonFile, contractAgreementId); | ||
|
||
JsonPath jsonPath = RestAssured | ||
.given() | ||
.headers(API_KEY_HEADER_KEY, API_KEY_HEADER_VALUE) | ||
.contentType(ContentType.JSON) | ||
.body(sampleDataRequest) | ||
.when() | ||
.post(INITIATE_TRANSFER_PROCESS_URI) | ||
.then() | ||
.statusCode(HttpStatus.SC_OK) | ||
.body("id", not(emptyString())) | ||
.extract() | ||
.jsonPath(); | ||
|
||
String transferProcessId = jsonPath.get("id"); | ||
|
||
assertThat(transferProcessId).isNotEmpty(); | ||
} | ||
|
||
/** | ||
* Reads a transfer request file with changed value for contract agreement ID and file destination path. | ||
* | ||
* @param transferJsonFile A {@link File} instance pointing to a JSON transfer request file. | ||
* @param contractAgreementId This string containing a UUID will be used as value for the contract agreement ID. | ||
* @return An instance of {@link DataRequest} with changed values for contract agreement ID and file destination path. | ||
* @throws IOException Thrown if there was an error accessing the file given in transferJsonFile. | ||
*/ | ||
static DataRequest readAndUpdateDataRequestFromJsonFile(File transferJsonFile, String contractAgreementId) throws IOException { | ||
// convert JSON file to map | ||
DataRequest sampleDataRequest = MAPPER.readValue(transferJsonFile, DataRequest.class); | ||
|
||
var changedAddressProperties = sampleDataRequest.getDataDestination().getProperties(); | ||
changedAddressProperties.put("path", DESTINATION_FILE.getAbsolutePath()); | ||
|
||
DataAddress newDataDestination = DataAddress.Builder.newInstance() | ||
.properties(changedAddressProperties) | ||
.build(); | ||
|
||
return DataRequest.Builder.newInstance() | ||
// copy unchanged values from JSON file | ||
.id(sampleDataRequest.getId()) | ||
.processId(sampleDataRequest.getProcessId()) | ||
.connectorAddress(sampleDataRequest.getConnectorAddress()) | ||
.protocol(sampleDataRequest.getProtocol()) | ||
.connectorId(sampleDataRequest.getConnectorId()) | ||
.assetId(sampleDataRequest.getAssetId()) | ||
.destinationType(sampleDataRequest.getDestinationType()) | ||
.transferType(sampleDataRequest.getTransferType()) | ||
.managedResources(sampleDataRequest.isManagedResources()) | ||
.properties(sampleDataRequest.getProperties()) | ||
// set changed values | ||
.contractId(contractAgreementId) | ||
.dataDestination(newDataDestination) | ||
.build(); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
var