public static LocationName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "LocationName.parse: formattedString not in valid format"); return of(matchMap.get("project"), matchMap.get("location")); }
@Test @SuppressWarnings("all") public void listInstancesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockCloudRedis.addException(exception); try { LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); client.listInstances(parent); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void createInstanceExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockCloudRedis.addException(exception); try { LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); String instanceId = "test_instance"; Instance.Tier tier = Instance.Tier.BASIC; int memorySizeGb = 1; Instance instance = Instance.newBuilder().setTier(tier).setMemorySizeGb(memorySizeGb).build(); client.createInstanceAsync(parent, instanceId, instance).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } }
@Test @SuppressWarnings("all") public void listInstancesTest() { String nextPageToken = ""; Instance instancesElement = Instance.newBuilder().build(); List<Instance> instances = Arrays.asList(instancesElement); ListInstancesResponse expectedResponse = ListInstancesResponse.newBuilder() .setNextPageToken(nextPageToken) .addAllInstances(instances) .build(); mockCloudRedis.addResponse(expectedResponse); LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); ListInstancesPagedResponse pagedListResponse = client.listInstances(parent); List<Instance> resources = Lists.newArrayList(pagedListResponse.iterateAll()); Assert.assertEquals(1, resources.size()); Assert.assertEquals(expectedResponse.getInstancesList().get(0), resources.get(0)); List<GeneratedMessageV3> actualRequests = mockCloudRedis.getRequests(); Assert.assertEquals(1, actualRequests.size()); ListInstancesRequest actualRequest = (ListInstancesRequest) actualRequests.get(0); Assert.assertEquals(parent, LocationName.parse(actualRequest.getParent())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
mockCloudRedis.addResponse(resultOperation); LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); String instanceId = "test_instance"; Instance.Tier tier = Instance.Tier.BASIC;