|
19 | 19 |
|
20 | 20 | package org.eclipse.tractusx.edc.tests;
|
21 | 21 |
|
22 |
| -import static org.awaitility.Awaitility.await; |
23 |
| -import static org.junit.jupiter.api.Assertions.fail; |
24 |
| - |
25 | 22 | import io.cucumber.datatable.DataTable;
|
26 | 23 | import io.cucumber.java.AfterAll;
|
27 | 24 | import io.cucumber.java.en.Given;
|
28 | 25 | import io.cucumber.java.en.Then;
|
29 |
| -import java.io.File; |
30 |
| -import java.io.IOException; |
31 |
| -import java.nio.file.Files; |
32 |
| -import java.time.Duration; |
33 |
| -import java.util.ArrayList; |
34 |
| -import java.util.List; |
35 |
| -import java.util.Map; |
36 |
| -import java.util.Set; |
37 | 26 | import org.eclipse.tractusx.edc.tests.data.Asset;
|
38 | 27 | import org.eclipse.tractusx.edc.tests.data.DataAddress;
|
39 | 28 | import org.eclipse.tractusx.edc.tests.data.Negotiation;
|
|
45 | 34 | import org.eclipse.tractusx.edc.tests.util.Timeouts;
|
46 | 35 | import org.junit.jupiter.api.Assertions;
|
47 | 36 |
|
| 37 | +import java.io.File; |
| 38 | +import java.io.IOException; |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.time.Duration; |
| 41 | +import java.util.ArrayList; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Map; |
| 44 | +import java.util.Set; |
| 45 | + |
| 46 | +import static org.awaitility.Awaitility.await; |
| 47 | +import static org.junit.jupiter.api.Assertions.fail; |
| 48 | + |
48 | 49 | public class S3FileTransferStepsDefs {
|
49 | 50 |
|
50 |
| - @Given("'{connector}' has an empty storage bucket called {string}") |
51 |
| - public void hasEmptyStorageBucket(Connector connector, String bucketName) { |
52 |
| - S3Client s3 = connector.getS3Client(); |
| 51 | + private static final String COMPLETION_MARKER = ".complete"; |
| 52 | + private File fileToTransfer; |
| 53 | + private String assetId; |
| 54 | + private String agreementId; |
53 | 55 |
|
54 |
| - s3.createBucket(bucketName); |
| 56 | + @AfterAll |
| 57 | + public static void bucketsCleanup() { |
| 58 | + S3Client s3 = new S3Client(Environment.byName("Sokrates")); |
| 59 | + s3.deleteAllBuckets(); |
| 60 | + } |
55 | 61 |
|
56 |
| - Assertions.assertTrue(s3.listBuckets().contains(bucketName)); |
57 |
| - Assertions.assertEquals(0, s3.listBucketContent(bucketName).size()); |
58 |
| - } |
| 62 | + @Given("'{connector}' has an empty storage bucket called {string}") |
| 63 | + public void hasEmptyStorageBucket(Connector connector, String bucketName) { |
| 64 | + S3Client s3 = connector.getS3Client(); |
59 | 65 |
|
60 |
| - private File fileToTransfer; |
| 66 | + s3.createBucket(bucketName); |
61 | 67 |
|
62 |
| - @Given("'{connector}' has a storage bucket called {string} with the file called {string}") |
63 |
| - public void hasAStorageBucketWithTheFile(Connector connector, String bucketName, String fileName) |
64 |
| - throws IOException { |
| 68 | + Assertions.assertTrue(s3.listBuckets().contains(bucketName)); |
| 69 | + Assertions.assertEquals(0, s3.listBucketContent(bucketName).size()); |
| 70 | + } |
65 | 71 |
|
66 |
| - S3Client s3 = connector.getS3Client(); |
67 |
| - s3.createBucket(bucketName); |
68 |
| - fileToTransfer = s3.uploadFile(bucketName, fileName); |
| 72 | + @Given("'{connector}' has a storage bucket called {string} with the file called {string}") |
| 73 | + public void hasAStorageBucketWithTheFile(Connector connector, String bucketName, String fileName) |
| 74 | + throws IOException { |
69 | 75 |
|
70 |
| - Set<String> bucketContent = s3.listBucketContent(bucketName); |
| 76 | + S3Client s3 = connector.getS3Client(); |
| 77 | + s3.createBucket(bucketName); |
| 78 | + fileToTransfer = s3.uploadFile(bucketName, fileName); |
71 | 79 |
|
72 |
| - Assertions.assertEquals(1, bucketContent.size()); |
73 |
| - Assertions.assertTrue(bucketContent.contains(fileName)); |
74 |
| - } |
| 80 | + Set<String> bucketContent = s3.listBucketContent(bucketName); |
75 | 81 |
|
76 |
| - @Given("'{connector}' has the following S3 assets") |
77 |
| - public void hasAssets(Connector connector, DataTable table) { |
78 |
| - final DataManagementAPI api = connector.getDataManagementAPI(); |
| 82 | + Assertions.assertEquals(1, bucketContent.size()); |
| 83 | + Assertions.assertTrue(bucketContent.contains(fileName)); |
| 84 | + } |
79 | 85 |
|
80 |
| - parseDataTable(table) |
81 |
| - .forEach( |
82 |
| - asset -> { |
83 |
| - try { |
84 |
| - api.createAsset(asset); |
85 |
| - } catch (IOException e) { |
86 |
| - fail(e.getMessage()); |
87 |
| - } |
88 |
| - }); |
89 |
| - } |
| 86 | + @Given("'{connector}' has the following S3 assets") |
| 87 | + public void hasAssets(Connector connector, DataTable table) { |
| 88 | + DataManagementAPI api = connector.getDataManagementAPI(); |
| 89 | + |
| 90 | + parseDataTable(table) |
| 91 | + .forEach( |
| 92 | + asset -> { |
| 93 | + try { |
| 94 | + api.createAsset(asset); |
| 95 | + } catch (IOException e) { |
| 96 | + fail(e.getMessage()); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
90 | 100 |
|
91 |
| - private String assetId; |
92 |
| - private String agreementId; |
| 101 | + @Then("'{connector}' negotiates the contract successfully with '{connector}'") |
| 102 | + public void negotiateContract(Connector sender, Connector receiver, DataTable dataTable) |
| 103 | + throws IOException { |
93 | 104 |
|
94 |
| - @Then("'{connector}' negotiates the contract successfully with '{connector}'") |
95 |
| - public void negotiateContract(Connector sender, Connector receiver, DataTable dataTable) |
96 |
| - throws IOException { |
| 105 | + String definitionId = dataTable.asMaps().get(0).get("contract offer id"); |
| 106 | + assetId = dataTable.asMaps().get(0).get("asset id"); |
| 107 | + String policyId = dataTable.asMaps().get(0).get("policy id"); |
97 | 108 |
|
98 |
| - String definitionId = dataTable.asMaps().get(0).get("contract offer id"); |
99 |
| - assetId = dataTable.asMaps().get(0).get("asset id"); |
100 |
| - String policyId = dataTable.asMaps().get(0).get("policy id"); |
| 109 | + Policy policy = |
| 110 | + new Policy(policyId, List.of(new Permission("USE", new ArrayList<>(), null))); |
101 | 111 |
|
102 |
| - final Policy policy = |
103 |
| - new Policy(policyId, List.of(new Permission("USE", null, new ArrayList<>()))); |
| 112 | + DataManagementAPI dataManagementAPI = sender.getDataManagementAPI(); |
| 113 | + String receiverIdsUrl = receiver.getEnvironment().getIdsUrl() + "/data"; |
104 | 114 |
|
105 |
| - final DataManagementAPI dataManagementAPI = sender.getDataManagementAPI(); |
106 |
| - final String receiverIdsUrl = receiver.getEnvironment().getIdsUrl() + "/data"; |
| 115 | + Negotiation negotiation = |
| 116 | + dataManagementAPI.initiateNegotiation(receiverIdsUrl, definitionId, assetId, policy); |
| 117 | + negotiation.waitUntilComplete(dataManagementAPI); |
107 | 118 |
|
108 |
| - final Negotiation negotiation = |
109 |
| - dataManagementAPI.initiateNegotiation(receiverIdsUrl, definitionId, assetId, policy); |
110 |
| - negotiation.waitUntilComplete(dataManagementAPI); |
| 119 | + agreementId = dataManagementAPI.getNegotiation(negotiation.getId()).getAgreementId(); |
| 120 | + } |
111 | 121 |
|
112 |
| - agreementId = dataManagementAPI.getNegotiation(negotiation.getId()).getAgreementId(); |
113 |
| - } |
| 122 | + @Then("'{connector}' initiate S3 transfer process from '{connector}'") |
| 123 | + public void initiateTransferProcess(Connector sender, Connector receiver, DataTable dataTable) |
| 124 | + throws IOException { |
| 125 | + DataAddress dataAddress = createDataAddress(dataTable.asMaps().get(0)); |
114 | 126 |
|
115 |
| - @Then("'{connector}' initiate S3 transfer process from '{connector}'") |
116 |
| - public void initiateTransferProcess(Connector sender, Connector receiver, DataTable dataTable) |
117 |
| - throws IOException { |
118 |
| - DataAddress dataAddress = createDataAddress(dataTable.asMaps().get(0)); |
| 127 | + DataManagementAPI dataManagementAPI = sender.getDataManagementAPI(); |
| 128 | + String receiverIdsUrl = receiver.getEnvironment().getIdsUrl() + "/data"; |
119 | 129 |
|
120 |
| - final DataManagementAPI dataManagementAPI = sender.getDataManagementAPI(); |
121 |
| - final String receiverIdsUrl = receiver.getEnvironment().getIdsUrl() + "/data"; |
| 130 | + Transfer transferProcess = |
| 131 | + dataManagementAPI.initiateTransferProcess( |
| 132 | + receiverIdsUrl, agreementId, assetId, dataAddress); |
| 133 | + transferProcess.waitUntilComplete(dataManagementAPI); |
122 | 134 |
|
123 |
| - final Transfer transferProcess = |
124 |
| - dataManagementAPI.initiateTransferProcess( |
125 |
| - receiverIdsUrl, agreementId, assetId, dataAddress); |
126 |
| - transferProcess.waitUntilComplete(dataManagementAPI); |
| 135 | + Assertions.assertNotNull(transferProcess.getId()); |
| 136 | + } |
127 | 137 |
|
128 |
| - Assertions.assertNotNull(transferProcess.getId()); |
129 |
| - } |
| 138 | + @Then("'{connector}' has a storage bucket called {string} with transferred file called {string}") |
| 139 | + public void consumerHasAStorageBucketWithFileTransferred( |
| 140 | + Connector connector, String bucketName, String fileName) throws IOException { |
| 141 | + S3Client s3 = connector.getS3Client(); |
| 142 | + await() |
| 143 | + .pollDelay(Duration.ofMillis(500)) |
| 144 | + .atMost(Timeouts.FILE_TRANSFER) |
| 145 | + .until(() -> isFilePresent(s3, bucketName, fileName + COMPLETION_MARKER)); |
| 146 | + |
| 147 | + Set<String> bucketContent = s3.listBucketContent(bucketName); |
| 148 | + |
| 149 | + Assertions.assertEquals(2, bucketContent.size()); |
| 150 | + Assertions.assertTrue(bucketContent.contains(fileName)); |
| 151 | + Assertions.assertArrayEquals( |
| 152 | + Files.readAllBytes(fileToTransfer.toPath()), |
| 153 | + Files.readAllBytes(s3.downloadFile(bucketName, fileName).toPath())); |
| 154 | + } |
130 | 155 |
|
131 |
| - private static final String COMPLETION_MARKER = ".complete"; |
| 156 | + private boolean isFilePresent(S3Client s3, String bucketName, String fileName) { |
| 157 | + return s3.listBucketContent(bucketName).contains(fileName); |
| 158 | + } |
132 | 159 |
|
133 |
| - @Then("'{connector}' has a storage bucket called {string} with transferred file called {string}") |
134 |
| - public void consumerHasAStorageBucketWithFileTransferred( |
135 |
| - Connector connector, String bucketName, String fileName) throws IOException { |
136 |
| - S3Client s3 = connector.getS3Client(); |
137 |
| - await() |
138 |
| - .pollDelay(Duration.ofMillis(500)) |
139 |
| - .atMost(Timeouts.FILE_TRANSFER) |
140 |
| - .until(() -> isFilePresent(s3, bucketName, fileName + COMPLETION_MARKER)); |
| 160 | + private List<Asset> parseDataTable(DataTable table) { |
| 161 | + List<Asset> assetsWithDataAddresses = new ArrayList<>(); |
141 | 162 |
|
142 |
| - Set<String> bucketContent = s3.listBucketContent(bucketName); |
| 163 | + for (Map<String, String> map : table.asMaps()) { |
| 164 | + String id = map.get("id"); |
| 165 | + String description = map.get("description"); |
| 166 | + assetsWithDataAddresses.add(new Asset(id, description, createDataAddress(map))); |
| 167 | + } |
143 | 168 |
|
144 |
| - Assertions.assertEquals(2, bucketContent.size()); |
145 |
| - Assertions.assertTrue(bucketContent.contains(fileName)); |
146 |
| - Assertions.assertArrayEquals( |
147 |
| - Files.readAllBytes(fileToTransfer.toPath()), |
148 |
| - Files.readAllBytes(s3.downloadFile(bucketName, fileName).toPath())); |
149 |
| - } |
150 |
| - |
151 |
| - private boolean isFilePresent(S3Client s3, String bucketName, String fileName) { |
152 |
| - return s3.listBucketContent(bucketName).contains(fileName); |
153 |
| - } |
154 |
| - |
155 |
| - private List<Asset> parseDataTable(DataTable table) { |
156 |
| - final List<Asset> assetsWithDataAddresses = new ArrayList<>(); |
157 |
| - |
158 |
| - for (Map<String, String> map : table.asMaps()) { |
159 |
| - String id = map.get("id"); |
160 |
| - String description = map.get("description"); |
161 |
| - assetsWithDataAddresses.add(new Asset(id, description, createDataAddress(map))); |
| 169 | + return assetsWithDataAddresses; |
162 | 170 | }
|
163 | 171 |
|
164 |
| - return assetsWithDataAddresses; |
165 |
| - } |
166 |
| - |
167 |
| - private DataAddress createDataAddress(Map<String, String> map) { |
168 |
| - final String bucketName = map.get("data_address_s3_bucket_name"); |
169 |
| - final String region = map.get("data_address_s3_region"); |
170 |
| - final String keyName = map.get("data_address_s3_key_name"); |
171 |
| - return new S3DataAddress(bucketName, region, keyName); |
172 |
| - } |
173 |
| - |
174 |
| - @AfterAll |
175 |
| - public static void bucketsCleanup() { |
176 |
| - S3Client s3 = new S3Client(Environment.byName("Sokrates")); |
177 |
| - s3.deleteAllBuckets(); |
178 |
| - } |
| 172 | + private DataAddress createDataAddress(Map<String, String> map) { |
| 173 | + String bucketName = map.get("data_address_s3_bucket_name"); |
| 174 | + String region = map.get("data_address_s3_region"); |
| 175 | + String keyName = map.get("data_address_s3_key_name"); |
| 176 | + return new S3DataAddress(bucketName, region, keyName); |
| 177 | + } |
179 | 178 | }
|
0 commit comments