public static CryptoKeyPathName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "CryptoKeyPathName.parse: formattedString not in valid format"); return of( matchMap.get("project"), matchMap.get("location"), matchMap.get("key_ring"), matchMap.get("crypto_key_path")); }
@Test @SuppressWarnings("all") public void encryptExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockKeyManagementService.addException(exception); try { CryptoKeyPathName name = CryptoKeyPathName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY_PATH]"); ByteString plaintext = ByteString.copyFromUtf8("-9"); client.encrypt(name, plaintext); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void encryptTest() { String name2 = "name2-1052831874"; ByteString ciphertext = ByteString.copyFromUtf8("-72"); EncryptResponse expectedResponse = EncryptResponse.newBuilder().setName(name2).setCiphertext(ciphertext).build(); mockKeyManagementService.addResponse(expectedResponse); CryptoKeyPathName name = CryptoKeyPathName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY_PATH]"); ByteString plaintext = ByteString.copyFromUtf8("-9"); EncryptResponse actualResponse = client.encrypt(name, plaintext); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockKeyManagementService.getRequests(); Assert.assertEquals(1, actualRequests.size()); EncryptRequest actualRequest = (EncryptRequest) actualRequests.get(0); Assert.assertEquals(name, CryptoKeyPathName.parse(actualRequest.getName())); Assert.assertEquals(plaintext, actualRequest.getPlaintext()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
public static CryptoKeyPathName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "CryptoKeyPathName.parse: formattedString not in valid format"); return of( matchMap.get("project"), matchMap.get("location"), matchMap.get("key_ring"), matchMap.get("crypto_key_path")); }