Skip to content

Commit 7e2c981

Browse files
authored
Clean-up and update Confidential Ledger tests for proxy migration (Azure#36343)
* Update test-resources.json to reflect existing .NET scripts. * Add common environment variable names. * Fix script path for test-resources-pre.ps1 * use envs for names. * Fix naming from Ledge -> Ledger * Add pom.xml imports for reflection during tests. * Change to eastus like .NET. * Fix test for LedgerIdentity. * Adding shared object mapper. * Fix warning in sample. * Fix tests for ledger entries. * Remove assertion of ledger cert. * Update recordings for two tests.
1 parent d5d541e commit 7e2c981

File tree

13 files changed

+311
-120
lines changed

13 files changed

+311
-120
lines changed

sdk/confidentialledger/azure-security-confidentialledger/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
<properties>
3333
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3434
<javaModulesSurefireArgLine>
35+
--add-exports com.azure.core/com.azure.core.implementation=ALL-UNNAMED
3536
--add-exports com.azure.core/com.azure.core.implementation.jackson=ALL-UNNAMED
37+
--add-exports com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED
38+
3639
--add-exports com.azure.http.netty/com.azure.core.http.netty=com.azure.security.confidentialledger
3740

3841
--add-reads com.azure.security.confidentialledger=com.azure.http.netty

sdk/confidentialledger/azure-security-confidentialledger/src/samples/java/com/azure/security/confidentialledger/PostLedgerEntrySample.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.IOException;
77

8+
import com.azure.core.http.HttpHeaderName;
89
import org.junit.jupiter.api.Assertions;
910

1011
import com.azure.core.http.rest.RequestOptions;
@@ -34,7 +35,7 @@ public static void main(String[] args) {
3435
JsonNode responseBodyJson = null;
3536

3637
try {
37-
38+
3839
responseBodyJson = objectMapper.readTree(parsedResponse.toBytes());
3940
} catch (IOException e) {
4041
e.printStackTrace();
@@ -44,12 +45,14 @@ public static void main(String[] args) {
4445
String collectionId = responseBodyJson.get("collectionId").asText();
4546

4647
// you can use the transaction id to get the transaction status
47-
String transactionId = response.getHeaders().get("x-ms-ccf-transaction-id").getValue();
48+
String transactionId = response.getHeaders()
49+
.get(HttpHeaderName.fromString("x-ms-ccf-transaction-id"))
50+
.getValue();
4851

4952
requestOptions = new RequestOptions();
5053
// the transactionId can be retrieved after posting to a ledger (see PostLedgerEntry.java)
5154
Response<BinaryData> transactionResponse = confidentialLedgerClient.getTransactionStatusWithResponse(transactionId, requestOptions);
52-
55+
5356
JsonNode transactionResponseBodyJson = null;
5457

5558
try {

sdk/confidentialledger/azure-security-confidentialledger/src/test/java/com/azure/security/confidentialledger/ConfidentialLedgeCertificateClientTestBase.java renamed to sdk/confidentialledger/azure-security-confidentialledger/src/test/java/com/azure/security/confidentialledger/ConfidentialLedgerCertificateClientTestBase.java

File renamed without changes.

sdk/confidentialledger/azure-security-confidentialledger/src/test/java/com/azure/security/confidentialledger/ConfidentialLedgerClientTestBase.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,51 @@
66

77
import com.azure.core.credential.AccessToken;
88
import com.azure.core.http.HttpClient;
9+
import com.azure.core.http.HttpHeaderName;
910
import com.azure.core.http.netty.NettyAsyncHttpClientBuilder;
1011
import com.azure.core.http.policy.HttpLogDetailLevel;
1112
import com.azure.core.http.policy.HttpLogOptions;
13+
import com.azure.core.http.rest.RequestOptions;
1214
import com.azure.core.http.rest.Response;
1315
import com.azure.core.test.TestBase;
1416
import com.azure.core.test.TestMode;
1517
import com.azure.core.util.BinaryData;
16-
import com.azure.core.util.Configuration;
1718
import com.azure.identity.DefaultAzureCredentialBuilder;
1819
import com.azure.security.confidentialledger.certificate.ConfidentialLedgerCertificateClient;
1920
import com.azure.security.confidentialledger.certificate.ConfidentialLedgerCertificateClientBuilder;
2021
import com.fasterxml.jackson.databind.JsonNode;
2122
import com.fasterxml.jackson.databind.ObjectMapper;
22-
2323
import io.netty.handler.ssl.SslContext;
2424
import io.netty.handler.ssl.SslContextBuilder;
25+
import org.junit.jupiter.api.Assertions;
2526
import reactor.core.publisher.Mono;
2627

28+
import javax.net.ssl.SSLException;
2729
import java.io.ByteArrayInputStream;
2830
import java.io.IOException;
2931
import java.nio.charset.StandardCharsets;
3032
import java.time.OffsetDateTime;
3133

32-
import javax.net.ssl.SSLException;
33-
34-
import org.junit.jupiter.api.Assertions;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3536

3637
class ConfidentialLedgerClientTestBase extends TestBase {
38+
protected static final String TRANSACTION_ID = "transactionId";
39+
protected static final String COLLECTION_ID = "collectionId";
40+
protected static final BinaryData BINARY_DATA =
41+
BinaryData.fromString("{\"contents\":\"New ledger entry contents.\"}");
42+
43+
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
44+
3745
protected ConfidentialLedgerClient confidentialLedgerClient;
3846
protected ConfidentialLedgerClientBuilder confidentialLedgerClientBuilder;
3947
protected ConfidentialLedgerCertificateClient confidentialLedgerCertificateClient;
4048

4149
@Override
4250
protected void beforeTest() {
4351
ConfidentialLedgerCertificateClientBuilder confidentialLedgerCertificateClientBuilder = new ConfidentialLedgerCertificateClientBuilder()
44-
.certificateEndpoint("https://identity.confidential-ledger.core.azure.com")
45-
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC));
52+
.certificateEndpoint(ConfidentialLedgerEnvironment.getConfidentialLedgerIdentityUrl())
53+
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY));
4654

4755
if (getTestMode() == TestMode.PLAYBACK) {
4856
confidentialLedgerCertificateClientBuilder
@@ -58,10 +66,8 @@ protected void beforeTest() {
5866

5967
confidentialLedgerCertificateClient = confidentialLedgerCertificateClientBuilder.buildClient();
6068

61-
String ledgerName = Configuration.getGlobalConfiguration().get("LEDGER_NAME", "java-sdk-live-tests-ledger");
62-
6369
Response<BinaryData> ledgerIdentityWithResponse = confidentialLedgerCertificateClient
64-
.getLedgerIdentityWithResponse(ledgerName, null);
70+
.getLedgerIdentityWithResponse(ConfidentialLedgerEnvironment.getConfidentialLedgerName(), null);
6571
BinaryData identityResponse = ledgerIdentityWithResponse.getValue();
6672
ObjectMapper mapper = new ObjectMapper();
6773
JsonNode jsonNode = null;
@@ -91,7 +97,7 @@ protected void beforeTest() {
9197
HttpClient httpClient = new NettyAsyncHttpClientBuilder(reactorClient).wiretap(true).build();
9298

9399
confidentialLedgerClientBuilder = new ConfidentialLedgerClientBuilder()
94-
.ledgerEndpoint(Configuration.getGlobalConfiguration().get("LEDGER_URI", "https://java-sdk-live-tests-ledger.confidential-ledger.azure.com"))
100+
.ledgerEndpoint(ConfidentialLedgerEnvironment.getConfidentialLedgerUrl())
95101
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC));
96102

97103
if (getTestMode() == TestMode.PLAYBACK) {
@@ -111,4 +117,35 @@ protected void beforeTest() {
111117
confidentialLedgerClient = confidentialLedgerClientBuilder.buildClient();
112118
}
113119

120+
/**
121+
* Posts and asserts that an entry was added. Useful for tests that require a ledger entry.
122+
*
123+
* @return The transaction id associated with the ledger entry.
124+
*/
125+
protected String postLedgerEntry() throws IOException {
126+
// Arrange
127+
final RequestOptions options = new RequestOptions();
128+
final String contents = BINARY_DATA.toString();
129+
130+
// Act
131+
final Response<BinaryData> response =
132+
confidentialLedgerClient.createLedgerEntryWithResponse(BINARY_DATA, options);
133+
134+
// Assert
135+
assertEquals(200, response.getStatusCode());
136+
137+
JsonNode jsonNode = OBJECT_MAPPER.readTree(response.getValue().toBytes());
138+
JsonNode collectionIdNode = jsonNode.get("collectionId");
139+
140+
assertNotNull(collectionIdNode);
141+
assertEquals("subledger:0", collectionIdNode.asText());
142+
143+
String transactionId = response.getHeaders()
144+
.get(HttpHeaderName.fromString("x-ms-ccf-transaction-id"))
145+
.getValue();
146+
147+
assertNotNull(transactionId, "transaction id should exist on headers.");
148+
149+
return transactionId;
150+
}
114151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.security.confidentialledger;
5+
6+
import com.azure.core.util.Configuration;
7+
8+
/**
9+
* Set of environment variables shared in confidential ledger tests.
10+
*/
11+
public class ConfidentialLedgerEnvironment {
12+
13+
private static final Configuration GLOBAL_CONFIGURATION = Configuration.getGlobalConfiguration();
14+
15+
public static String getConfidentialLedgerName() {
16+
return GLOBAL_CONFIGURATION.get("CONFIDENTIALLEDGER_NAME", "java-sdk-live-tests-ledger");
17+
}
18+
19+
public static String getConfidentialLedgerUrl() {
20+
return GLOBAL_CONFIGURATION.get("CONFIDENTIALLEDGER_URL",
21+
"https://" + getConfidentialLedgerName() + ".confidential-ledger.azure.com");
22+
}
23+
24+
public static String getConfidentialLedgerIdentityUrl() {
25+
return GLOBAL_CONFIGURATION.get("CONFIDENTIALLEDGER_IDENTITY_URL", "https://identity.confidential-ledger.core.azure.com");
26+
}
27+
28+
public static String getConfidentialLedgerAdminOid() {
29+
return GLOBAL_CONFIGURATION.get("CONFIDENTIALLEDGER_CLIENT_OBJECTID", "d958292f-5b70-4b66-9502-562217cc7eaa");
30+
}
31+
32+
public static String getClientPEM() {
33+
return GLOBAL_CONFIGURATION.get("CONFIDENTIALLEDGER_CLIENT_PEM", "test-pem");
34+
}
35+
36+
public static String getClientPEMPk() {
37+
return GLOBAL_CONFIGURATION.get("CONFIDENTIALLEDGER_CLIENT_PEM_PK", "test-pek");
38+
}
39+
}

sdk/confidentialledger/azure-security-confidentialledger/src/test/java/com/azure/security/confidentialledger/LedgerEntriesTest.java

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +10,59 @@
1010
import com.azure.core.util.BinaryData;
1111
import com.fasterxml.jackson.databind.JsonNode;
1212
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import org.junit.jupiter.api.Assertions;
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.parallel.Isolated;
16+
1317
import java.io.IOException;
1418
import java.util.ArrayList;
1519
import java.util.List;
1620

17-
import org.junit.jupiter.api.Assertions;
18-
import org.junit.jupiter.api.Test;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
1923

24+
@Isolated("There is an assumption in post ledger entries that checks it is latest entry.")
2025
public final class LedgerEntriesTest extends ConfidentialLedgerClientTestBase {
2126
@Test
2227
public void testPostLedgerEntryTests() throws Exception {
23-
BinaryData entry = BinaryData.fromString("{\"contents\":\"New ledger entry contents.\"}");
28+
// Arrange
2429
RequestOptions requestOptions = new RequestOptions();
25-
Response<BinaryData> response = confidentialLedgerClient.createLedgerEntryWithResponse(entry, requestOptions);
26-
27-
String transactionId = response.getHeaders().get("x-ms-ccf-transaction-id").getValue();
28-
29-
BinaryData parsedResponse = response.getValue();
30-
31-
ObjectMapper objectMapper = new ObjectMapper();
32-
JsonNode responseBodyJson = null;
33-
34-
responseBodyJson = objectMapper.readTree(parsedResponse.toBytes());
35-
36-
Assertions.assertEquals(responseBodyJson.get("collectionId").asText(), "subledger:0");
30+
String transactionId = postLedgerEntry();
3731

3832
Response<BinaryData> transactionResponse = confidentialLedgerClient.getTransactionStatusWithResponse(transactionId, requestOptions);
3933

40-
JsonNode transactionResponseBodyJson = null;
34+
JsonNode transactionResponseBodyJson = OBJECT_MAPPER.readTree(transactionResponse.getValue().toBytes());
4135

42-
try {
43-
transactionResponseBodyJson = objectMapper.readTree(transactionResponse.getValue().toBytes());
44-
} catch (IOException e) {
45-
e.printStackTrace();
46-
Assertions.assertTrue(false);
47-
}
36+
assertEquals(transactionResponseBodyJson.get(TRANSACTION_ID).asText(), transactionId);
4837

49-
Assertions.assertEquals(transactionResponseBodyJson.get("transactionId").asText(), transactionId);
50-
Assertions.assertTrue(200 == transactionResponse.getStatusCode() || 406 == transactionResponse.getStatusCode());
38+
int statusCode = transactionResponse.getStatusCode();
39+
assertTrue(200 == statusCode || 406 == statusCode, "Expected 200, or 206. Actual: " + statusCode);
5140

41+
// Act
5242
Response<BinaryData> currentResponse = confidentialLedgerClient.getCurrentLedgerEntryWithResponse(requestOptions);
5343

54-
JsonNode currentResponseBodyJson = null;
44+
// Assert
45+
JsonNode currentResponseBodyJson = OBJECT_MAPPER.readTree(currentResponse.getValue().toBytes());
5546

56-
try {
57-
58-
currentResponseBodyJson = objectMapper.readTree(currentResponse.getValue().toBytes());
59-
} catch (IOException e) {
60-
e.printStackTrace();
61-
Assertions.assertTrue(false);
62-
}
63-
64-
Assertions.assertTrue(200 == currentResponse.getStatusCode() || 406 == currentResponse.getStatusCode());
47+
assertTrue(200 == currentResponse.getStatusCode() || 406 == currentResponse.getStatusCode());
6548

6649
if (200 == currentResponse.getStatusCode()) {
67-
// we assume no one else is using this test ledger
68-
Assertions.assertTrue(currentResponseBodyJson.get("transactionId").asDouble() <= Double.parseDouble(transactionId));
50+
// we assume no one has created an entry since we created this entry.
51+
assertTrue(currentResponseBodyJson.get(TRANSACTION_ID).asDouble() <= Double.parseDouble(transactionId));
6952
}
7053
}
7154

7255
@Test
7356
public void testGetCollectionIdsTests() throws Exception {
57+
postLedgerEntry();
58+
7459
RequestOptions requestOptions = new RequestOptions();
7560
PagedIterable<BinaryData> pagedIterableResponse = confidentialLedgerClient.listCollections(requestOptions);
7661

7762
List<String> collectionKeys = new ArrayList<>();
7863

7964
pagedIterableResponse.streamByPage().forEach(resp -> {
80-
Assertions.assertEquals(200, resp.getStatusCode());
65+
assertEquals(200, resp.getStatusCode());
8166
resp.getValue().forEach(item -> {
8267
ObjectMapper objectMapper = new ObjectMapper();
8368
JsonNode responseBodyJson = null;
@@ -86,14 +71,15 @@ public void testGetCollectionIdsTests() throws Exception {
8671
responseBodyJson = objectMapper.readTree(item.toBytes());
8772
} catch (IOException e) {
8873
e.printStackTrace();
89-
Assertions.assertTrue(false);
74+
assertTrue(false);
9075
}
9176

92-
Assertions.assertNotNull(responseBodyJson.get("collectionId"));
93-
collectionKeys.add(responseBodyJson.get("collectionId").asText());
77+
Assertions.assertNotNull(responseBodyJson.get(COLLECTION_ID));
78+
collectionKeys.add(responseBodyJson.get(COLLECTION_ID).asText());
9479
});
9580

96-
collectionKeys.stream().anyMatch((item) -> item.contains("subledger:0"));
81+
boolean exists = collectionKeys.stream().anyMatch((item) -> item.contains("subledger:0"));
82+
assertTrue(exists, "Did not find matching collection.");
9783
});
9884
}
9985
}

sdk/confidentialledger/azure-security-confidentialledger/src/test/java/com/azure/security/confidentialledger/UserTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@
77
import com.azure.core.http.rest.RequestOptions;
88
import com.azure.core.http.rest.Response;
99
import com.azure.core.util.BinaryData;
10-
import com.azure.core.util.Configuration;
1110
import com.fasterxml.jackson.databind.JsonNode;
1211
import com.fasterxml.jackson.databind.ObjectMapper;
13-
14-
import java.io.IOException;
15-
1612
import org.junit.jupiter.api.Assertions;
1713
import org.junit.jupiter.api.Test;
1814

15+
import java.io.IOException;
16+
1917
public final class UserTests extends ConfidentialLedgerClientTestBase {
2018
@Test
2119
public void testGetUserTests() throws Exception {
22-
String userAad = Configuration.getGlobalConfiguration().get("USER_AAD", "d958292f-5b70-4b66-9502-562217cc7eaa");
20+
String userAad = ConfidentialLedgerEnvironment.getConfidentialLedgerAdminOid();
2321
RequestOptions requestOptions = new RequestOptions();
2422

2523
Response<BinaryData> response = confidentialLedgerClient.getUserWithResponse(userAad, requestOptions);

sdk/confidentialledger/azure-security-confidentialledger/src/test/java/com/azure/security/confidentialledger/certificate/generated/ConfidentialLedgerCertificateClientTestBase.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,29 @@
1010
import com.azure.core.http.policy.HttpLogOptions;
1111
import com.azure.core.test.TestBase;
1212
import com.azure.core.test.TestMode;
13-
import com.azure.core.util.Configuration;
1413
import com.azure.identity.DefaultAzureCredentialBuilder;
14+
import com.azure.security.confidentialledger.ConfidentialLedgerEnvironment;
1515
import com.azure.security.confidentialledger.certificate.ConfidentialLedgerCertificateClient;
1616
import com.azure.security.confidentialledger.certificate.ConfidentialLedgerCertificateClientBuilder;
17-
import java.time.OffsetDateTime;
17+
import com.fasterxml.jackson.databind.ObjectMapper;
1818
import reactor.core.publisher.Mono;
1919

20+
import java.time.OffsetDateTime;
21+
2022
class ConfidentialLedgerCertificateClientTestBase extends TestBase {
23+
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
24+
2125
protected ConfidentialLedgerCertificateClient confidentialLedgerCertificateClient;
2226

2327
@Override
2428
protected void beforeTest() {
2529
ConfidentialLedgerCertificateClientBuilder confidentialLedgerCertificateClientbuilder =
2630
new ConfidentialLedgerCertificateClientBuilder()
2731
.certificateEndpoint(
28-
Configuration.getGlobalConfiguration()
29-
.get("CERTIFICATEENDPOINT", "certificateendpoint"))
32+
ConfidentialLedgerEnvironment.getConfidentialLedgerIdentityUrl())
3033
.httpClient(HttpClient.createDefault())
3134
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC));
35+
3236
if (getTestMode() == TestMode.PLAYBACK) {
3337
confidentialLedgerCertificateClientbuilder
3438
.httpClient(interceptorManager.getPlaybackClient())

0 commit comments

Comments
 (0)