diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 08490e87aaf..09b11f04a7c 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -37,6 +37,7 @@ /iam @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra /iap @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra /kms @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra +/modelarmor @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/cloud-modelarmor-team /parametermanager @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/cloud-secrets-team @GoogleCloudPlatform/cloud-parameters-team /privateca @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra /recaptcha_enterprise @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml index b46b2c816f3..06cb5cde1d0 100644 --- a/.github/blunderbuss.yml +++ b/.github/blunderbuss.yml @@ -96,6 +96,10 @@ assign_issues_by: - "api: bigquerydatatransfer" to: - GoogleCloudPlatform/bigquery-data-connectors +- labels: + - "api: modelarmor" + to: + - GoogleCloudPlatform/cloud-modelarmor-team assign_prs: - GoogleCloudPlatform/java-samples-reviewers @@ -173,3 +177,7 @@ assign_prs_by: - "api: appengine" to: - GoogleCloudPlatform/serverless-runtimes +- labels: + - "api: modelarmor" + to: + - GoogleCloudPlatform/cloud-modelarmor-team diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java new file mode 100644 index 00000000000..dea12f19a8b --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/CreateTemplate.java @@ -0,0 +1,110 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package modelarmor; + +// [START modelarmor_create_template] + +import com.google.cloud.modelarmor.v1.CreateTemplateRequest; +import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; +import com.google.cloud.modelarmor.v1.FilterConfig; +import com.google.cloud.modelarmor.v1.LocationName; +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.RaiFilterSettings; +import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter; +import com.google.cloud.modelarmor.v1.RaiFilterType; +import com.google.cloud.modelarmor.v1.Template; +import java.io.IOException; +import java.util.List; + +public class CreateTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + + // Specify the Google Project ID. + String projectId = "your-project-id"; + // Specify the location ID. For example, us-central1. + String locationId = "your-location-id"; + // Specify the template ID. + String templateId = "your-template-id"; + + createTemplate(projectId, locationId, templateId); + } + + public static Template createTemplate(String projectId, String locationId, String templateId) + throws IOException { + + // Construct the API endpoint URL. + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); + + // Initialize the client that will be used to send requests. This client + // only needs to be created once, and can be reused for multiple requests. + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + String parent = LocationName.of(projectId, locationId).toString(); + + // Build the Model Armor template with your preferred filters. + // For more details on filters, please refer to the following doc: + // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters + + // Configure Responsible AI filter with multiple categories and their confidence + // levels. + RaiFilterSettings raiFilterSettings = RaiFilterSettings.newBuilder() + .addAllRaiFilters( + List.of( + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) + .build(); + + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setRaiSettings(raiFilterSettings) + .build(); + + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .build(); + + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); + + Template createdTemplate = client.createTemplate(request); + System.out.println("Created template: " + createdTemplate.getName()); + + return createdTemplate; + } + } +} +// [END modelarmor_create_template] diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java new file mode 100644 index 00000000000..a88ab47b59a --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java @@ -0,0 +1,94 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package modelarmor; + +// [START modelarmor_create_template_with_basic_sdp] + +import com.google.cloud.modelarmor.v1.CreateTemplateRequest; +import com.google.cloud.modelarmor.v1.FilterConfig; +import com.google.cloud.modelarmor.v1.LocationName; +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.SdpBasicConfig; +import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement; +import com.google.cloud.modelarmor.v1.SdpFilterSettings; +import com.google.cloud.modelarmor.v1.Template; +import java.io.IOException; + +public class CreateTemplateWithBasicSdp { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + + // Specify the Google Project ID. + String projectId = "your-project-id"; + // Specify the location ID. For example, us-central1. + String locationId = "your-location-id"; + // Specify the template ID. + String templateId = "your-template-id"; + + createTemplateWithBasicSdp(projectId, locationId, templateId); + } + + public static Template createTemplateWithBasicSdp( + String projectId, String locationId, String templateId) throws IOException { + + // Construct the API endpoint URL. + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); + + // Initialize the client that will be used to send requests. This client + // only needs to be created once, and can be reused for multiple requests. + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + String parent = LocationName.of(projectId, locationId).toString(); + + // Build the Model Armor template with your preferred filters. + // For more details on filters, please refer to the following doc: + // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters + + // Configure Basic SDP Filter. + SdpBasicConfig basicSdpConfig = SdpBasicConfig.newBuilder() + .setFilterEnforcement(SdpBasicConfigEnforcement.ENABLED) + .build(); + + SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder() + .setBasicConfig(basicSdpConfig) + .build(); + + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setSdpSettings(sdpSettings) + .build(); + + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .build(); + + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); + + Template createdTemplate = client.createTemplate(request); + System.out.println("Created template with basic SDP filter: " + createdTemplate.getName()); + + return createdTemplate; + } + } +} +// [END modelarmor_create_template_with_basic_sdp] diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java new file mode 100644 index 00000000000..0faf2c7a42f --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java @@ -0,0 +1,119 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package modelarmor; + +// [START modelarmor_create_template_with_labels] + +import com.google.cloud.modelarmor.v1.CreateTemplateRequest; +import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; +import com.google.cloud.modelarmor.v1.FilterConfig; +import com.google.cloud.modelarmor.v1.LocationName; +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.RaiFilterSettings; +import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter; +import com.google.cloud.modelarmor.v1.RaiFilterType; +import com.google.cloud.modelarmor.v1.Template; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class CreateTemplateWithLabels { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + + // Specify the Google Project ID. + String projectId = "your-project-id"; + // Specify the location ID. For example, us-central1. + String locationId = "your-location-id"; + // Specify the template ID. + String templateId = "your-template-id"; + + createTemplateWithLabels(projectId, locationId, templateId); + } + + public static Template createTemplateWithLabels( + String projectId, String locationId, String templateId) throws IOException { + + // Construct the API endpoint URL. + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); + + // Initialize the client that will be used to send requests. This client + // only needs to be created once, and can be reused for multiple requests. + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + String parent = LocationName.of(projectId, locationId).toString(); + + // Build the Model Armor template with your preferred filters. + // For more details on filters, please refer to the following doc: + // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters + + // Configure Responsible AI filter with multiple categories and their confidence + // levels. + RaiFilterSettings raiFilterSettings = + RaiFilterSettings.newBuilder() + .addAllRaiFilters( + List.of( + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) + .build(); + + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setRaiSettings(raiFilterSettings) + .build(); + + // Create Labels. + Map labels = new HashMap<>(); + labels.put("key1", "value1"); + labels.put("key2", "value2"); + + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .putAllLabels(labels) + .build(); + + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); + + Template createdTemplate = client.createTemplate(request); + System.out.println("Created template with labels: " + createdTemplate.getName()); + + return createdTemplate; + } + } +} +// [END modelarmor_create_template_with_labels] diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java new file mode 100644 index 00000000000..5abf2bdb4cf --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java @@ -0,0 +1,121 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package modelarmor; + +// [START modelarmor_create_template_with_metadata] + +import com.google.cloud.modelarmor.v1.CreateTemplateRequest; +import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; +import com.google.cloud.modelarmor.v1.FilterConfig; +import com.google.cloud.modelarmor.v1.LocationName; +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.RaiFilterSettings; +import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter; +import com.google.cloud.modelarmor.v1.RaiFilterType; +import com.google.cloud.modelarmor.v1.Template; +import com.google.cloud.modelarmor.v1.Template.TemplateMetadata; +import java.io.IOException; +import java.util.List; + +public class CreateTemplateWithMetadata { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + + // Specify the Google Project ID. + String projectId = "your-project-id"; + // Specify the location ID. For example, us-central1. + String locationId = "your-location-id"; + // Specify the template ID. + String templateId = "your-template-id"; + + createTemplateWithMetadata(projectId, locationId, templateId); + } + + public static Template createTemplateWithMetadata( + String projectId, String locationId, String templateId) throws IOException { + + // Construct the API endpoint URL. + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); + + // Initialize the client that will be used to send requests. This client + // only needs to be created once, and can be reused for multiple requests. + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + String parent = LocationName.of(projectId, locationId).toString(); + + // Build the Model Armor template with your preferred filters. + // For more details on filters, please refer to the following doc: + // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters + + // Configure Responsible AI filter with multiple categories and their confidence + // levels. + RaiFilterSettings raiFilterSettings = + RaiFilterSettings.newBuilder() + .addAllRaiFilters( + List.of( + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) + .build(); + + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setRaiSettings(raiFilterSettings) + .build(); + + // For more details about metadata, refer to the following documentation: + // https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#templatemetadata + TemplateMetadata templateMetadata = TemplateMetadata.newBuilder() + .setIgnorePartialInvocationFailures(true) + .setLogSanitizeOperations(true) + .setCustomPromptSafetyErrorCode(500) + .build(); + + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .setTemplateMetadata(templateMetadata) + .build(); + + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); + + Template createdTemplate = client.createTemplate(request); + System.out.println("Created template with metadata: " + createdTemplate.getName()); + + return createdTemplate; + } + } +} +// [END modelarmor_create_template_with_metadata] diff --git a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java new file mode 100644 index 00000000000..20cdb670cb0 --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java @@ -0,0 +1,60 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package modelarmor; + +// [START modelarmor_delete_template] + +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.TemplateName; +import java.io.IOException; + +public class DeleteTemplate { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + + // Specify the Google Project ID. + String projectId = "your-project-id"; + // Specify the location ID. For example, us-central1. + String locationId = "your-location-id"; + // Specify the template ID. + String templateId = "your-template-id"; + + deleteTemplate(projectId, locationId, templateId); + } + + public static void deleteTemplate(String projectId, String locationId, String templateId) + throws IOException { + + // Construct the API endpoint URL. + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); + + // Initialize the client that will be used to send requests. This client + // only needs to be created once, and can be reused for multiple requests. + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + String name = TemplateName.of(projectId, locationId, templateId).toString(); + + // Note: Ensure that the template you are deleting isn't used by any models. + client.deleteTemplate(name); + System.out.println("Deleted template: " + name); + } + } +} +// [END modelarmor_delete_template] diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index 2fe39b406b8..3ba8ca22fe4 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -16,6 +16,7 @@ package modelarmor; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -24,6 +25,7 @@ import com.google.cloud.modelarmor.v1.ModelArmorClient; import com.google.cloud.modelarmor.v1.ModelArmorSettings; import com.google.cloud.modelarmor.v1.SdpAdvancedConfig; +import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement; import com.google.cloud.modelarmor.v1.Template; import com.google.cloud.modelarmor.v1.TemplateName; import com.google.privacy.dlp.v2.CreateDeidentifyTemplateRequest; @@ -62,8 +64,8 @@ public class SnippetsIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String LOCATION_ID = System.getenv() .getOrDefault("GOOGLE_CLOUD_PROJECT_LOCATION", "us-central1"); - private static final String MA_ENDPOINT = - String.format("modelarmor.%s.rep.googleapis.com:443", LOCATION_ID); + private static final String MA_ENDPOINT = String.format("modelarmor.%s.rep.googleapis.com:443", + LOCATION_ID); private static String TEST_TEMPLATE_ID; private static String TEST_INSPECT_TEMPLATE_ID; private static String TEST_DEIDENTIFY_TEMPLATE_ID; @@ -75,7 +77,8 @@ public class SnippetsIT { // Check if the required environment variables are set. private static String requireEnvVar(String varName) { String value = System.getenv(varName); - assertNotNull("Environment variable " + varName + " is required to run these tests.", + assertNotNull( + "Environment variable " + varName + " is required to run these tests.", System.getenv(varName)); return value; } @@ -83,7 +86,7 @@ private static String requireEnvVar(String varName) { @BeforeClass public static void beforeAll() throws IOException { requireEnvVar("GOOGLE_CLOUD_PROJECT"); - + TEST_TEMPLATE_ID = randomId(); TEST_INSPECT_TEMPLATE_ID = randomId(); TEST_DEIDENTIFY_TEMPLATE_ID = randomId(); @@ -91,8 +94,10 @@ public static void beforeAll() throws IOException { TEST_INSPECT_TEMPLATE_NAME = InspectTemplateName .ofProjectLocationInspectTemplateName(PROJECT_ID, LOCATION_ID, TEST_INSPECT_TEMPLATE_ID) .toString(); - TEST_DEIDENTIFY_TEMPLATE_NAME = DeidentifyTemplateName.ofProjectLocationDeidentifyTemplateName( - PROJECT_ID, LOCATION_ID, TEST_DEIDENTIFY_TEMPLATE_ID).toString(); + TEST_DEIDENTIFY_TEMPLATE_NAME = DeidentifyTemplateName + .ofProjectLocationDeidentifyTemplateName( + PROJECT_ID, LOCATION_ID, TEST_DEIDENTIFY_TEMPLATE_ID) + .toString(); createInspectTemplate(TEST_INSPECT_TEMPLATE_ID); createDeidentifyTemplate(TEST_DEIDENTIFY_TEMPLATE_ID); @@ -102,11 +107,6 @@ public static void beforeAll() throws IOException { public static void afterAll() throws IOException { requireEnvVar("GOOGLE_CLOUD_PROJECT"); - try { - deleteModelArmorTemplate(TEST_TEMPLATE_ID); - } catch (NotFoundException e) { - // Ignore not found error - template already deleted. - } deleteSdpTemplates(); } @@ -118,6 +118,12 @@ public void beforeEach() { @After public void afterEach() throws IOException { + try { + deleteModelArmorTemplate(TEST_TEMPLATE_ID); + } catch (NotFoundException e) { + // Ignore not found error - template already deleted. + } + stdOut = null; System.setOut(null); } @@ -127,6 +133,51 @@ private static String randomId() { return "java-ma-" + random.nextLong(); } + @Test + public void testCreateModelArmorTemplate() throws IOException { + Template createdTemplate = CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, + TEST_TEMPLATE_ID); + + assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); + } + + @Test + public void testCreateModelArmorTemplateWithBasicSDP() throws IOException { + Template createdTemplate = CreateTemplateWithBasicSdp.createTemplateWithBasicSdp(PROJECT_ID, + LOCATION_ID, TEST_TEMPLATE_ID); + + assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); + assertEquals(SdpBasicConfigEnforcement.ENABLED, + createdTemplate.getFilterConfig().getSdpSettings().getBasicConfig().getFilterEnforcement()); + } + + @Test + public void testCreateModelArmorTemplateWithLabels() throws IOException { + Template createdTemplate = CreateTemplateWithLabels.createTemplateWithLabels(PROJECT_ID, + LOCATION_ID, TEST_TEMPLATE_ID); + + assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); + } + + @Test + public void testCreateModelArmorTemplateWithMetadata() throws IOException { + Template createdTemplate = CreateTemplateWithMetadata.createTemplateWithMetadata(PROJECT_ID, + LOCATION_ID, TEST_TEMPLATE_ID); + + assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); + assertEquals(true, createdTemplate.getTemplateMetadata().getIgnorePartialInvocationFailures()); + assertEquals(true, createdTemplate.getTemplateMetadata().getLogSanitizeOperations()); + assertEquals(500, createdTemplate.getTemplateMetadata().getCustomPromptSafetyErrorCode()); + } + + @Test + public void testDeleteModelArmorTemplate() throws IOException { + CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + + assertThat(stdOut.toString()).contains("Deleted template:"); + } + private static void deleteModelArmorTemplate(String templateId) throws IOException { ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(MA_ENDPOINT) .build(); @@ -146,7 +197,8 @@ private static void deleteSdpTemplates() throws IOException { private static InspectTemplate createInspectTemplate(String templateId) throws IOException { try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { - // Info Types: https://cloud.google.com/sensitive-data-protection/docs/infotypes-reference + // Info Types: + // https://cloud.google.com/sensitive-data-protection/docs/infotypes-reference List infoTypes = Stream .of("PHONE_NUMBER", "EMAIL_ADDRESS", "US_INDIVIDUAL_TAXPAYER_IDENTIFICATION_NUMBER") .map(it -> InfoType.newBuilder().setName(it).build()) @@ -160,12 +212,12 @@ private static InspectTemplate createInspectTemplate(String templateId) throws I .setInspectConfig(inspectConfig) .build(); - CreateInspectTemplateRequest createInspectTemplateRequest = - CreateInspectTemplateRequest.newBuilder() - .setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString()) - .setTemplateId(templateId) - .setInspectTemplate(inspectTemplate) - .build(); + CreateInspectTemplateRequest createInspectTemplateRequest = CreateInspectTemplateRequest + .newBuilder() + .setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString()) + .setTemplateId(templateId) + .setInspectTemplate(inspectTemplate) + .build(); return dlpServiceClient.createInspectTemplate(createInspectTemplateRequest); } @@ -188,18 +240,19 @@ private static DeidentifyTemplate createDeidentifyTemplate(String templateId) th .setPrimitiveTransformation(primitiveTransformation) .build(); - // Construct the configuration for the Redact request and list all desired transformations. + // Construct the configuration for the Redact request and list all desired + // transformations. DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder() .setInfoTypeTransformations( - InfoTypeTransformations.newBuilder() - .addTransformations(transformation)) + InfoTypeTransformations.newBuilder() + .addTransformations(transformation)) .build(); DeidentifyTemplate deidentifyTemplate = DeidentifyTemplate.newBuilder() .setDeidentifyConfig(redactConfig) .build(); - CreateDeidentifyTemplateRequest createDeidentifyTemplateRequest = + CreateDeidentifyTemplateRequest createDeidentifyTemplateRequest = CreateDeidentifyTemplateRequest.newBuilder() .setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString()) .setTemplateId(templateId)