public static NotificationChannelName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "NotificationChannelName.parse: formattedString not in valid format"); return of(matchMap.get("project"), matchMap.get("notification_channel")); }
@Test @SuppressWarnings("all") public void deleteNotificationChannelExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockNotificationChannelService.addException(exception); try { NotificationChannelName name = NotificationChannelName.of("[PROJECT]", "[NOTIFICATION_CHANNEL]"); boolean force = false; client.deleteNotificationChannel(name, force); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } } }
@Test @SuppressWarnings("all") public void getNotificationChannelExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockNotificationChannelService.addException(exception); try { NotificationChannelName name = NotificationChannelName.of("[PROJECT]", "[NOTIFICATION_CHANNEL]"); client.getNotificationChannel(name); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void deleteNotificationChannelTest() { Empty expectedResponse = Empty.newBuilder().build(); mockNotificationChannelService.addResponse(expectedResponse); NotificationChannelName name = NotificationChannelName.of("[PROJECT]", "[NOTIFICATION_CHANNEL]"); boolean force = false; client.deleteNotificationChannel(name, force); List<GeneratedMessageV3> actualRequests = mockNotificationChannelService.getRequests(); Assert.assertEquals(1, actualRequests.size()); DeleteNotificationChannelRequest actualRequest = (DeleteNotificationChannelRequest) actualRequests.get(0); Assert.assertEquals(name, NotificationChannelName.parse(actualRequest.getName())); Assert.assertEquals(force, actualRequest.getForce()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * Demonstrates deleting a notification channel by name. * @param channelName Name of the notification channel to delete. */ // [START monitoring_alert_delete_channel] static void deleteNotificationChannel(String channelName) throws IOException { String projectId = System.getProperty("projectId"); try (NotificationChannelServiceClient client = NotificationChannelServiceClient.create()) { NotificationChannelName name = NotificationChannelName.of(projectId, channelName); client.deleteNotificationChannel(channelName, false); System.out.println("Deleted notification channel " + channelName); } } // [END monitoring_alert_delete_channel]
@Test @SuppressWarnings("all") public void getNotificationChannelTest() { String type = "type3575610"; String name2 = "name2-1052831874"; String displayName = "displayName1615086568"; String description = "description-1724546052"; NotificationChannel expectedResponse = NotificationChannel.newBuilder() .setType(type) .setName(name2) .setDisplayName(displayName) .setDescription(description) .build(); mockNotificationChannelService.addResponse(expectedResponse); NotificationChannelName name = NotificationChannelName.of("[PROJECT]", "[NOTIFICATION_CHANNEL]"); NotificationChannel actualResponse = client.getNotificationChannel(name); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockNotificationChannelService.getRequests(); Assert.assertEquals(1, actualRequests.size()); GetNotificationChannelRequest actualRequest = (GetNotificationChannelRequest) actualRequests.get(0); Assert.assertEquals(name, NotificationChannelName.parse(actualRequest.getName())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
private static void replaceChannels(String projectId, String alertPolicyId, String[] channelIds) throws IOException { AlertPolicy.Builder policyBuilder = AlertPolicy .newBuilder() .setName(AlertPolicyName.of(projectId, alertPolicyId).toString()); for (String channelId : channelIds) { policyBuilder.addNotificationChannels( NotificationChannelName.of(projectId, channelId).toString()); } try (AlertPolicyServiceClient client = AlertPolicyServiceClient.create()) { AlertPolicy result = client.updateAlertPolicy( FieldMask.newBuilder().addPaths("notification_channels").build(), policyBuilder.build()); System.out.println(String.format("Updated %s", result.getName())); } } // [END monitoring_alert_replace_channels]