Skip to content

chore(modelarmor): Added floor settings tests #10106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions modelarmor/src/test/java/modelarmor/SnippetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.google.cloud.modelarmor.v1.FilterConfig;
import com.google.cloud.modelarmor.v1.FilterMatchState;
import com.google.cloud.modelarmor.v1.FilterResult;
import com.google.cloud.modelarmor.v1.FloorSetting;
import com.google.cloud.modelarmor.v1.FloorSettingName;
import com.google.cloud.modelarmor.v1.LocationName;
import com.google.cloud.modelarmor.v1.MaliciousUriFilterSettings;
import com.google.cloud.modelarmor.v1.MaliciousUriFilterSettings.MaliciousUriFilterEnforcement;
Expand All @@ -46,6 +48,7 @@
import com.google.cloud.modelarmor.v1.SdpFinding;
import com.google.cloud.modelarmor.v1.Template;
import com.google.cloud.modelarmor.v1.TemplateName;
import com.google.cloud.modelarmor.v1.UpdateFloorSettingRequest;
import com.google.privacy.dlp.v2.CreateDeidentifyTemplateRequest;
import com.google.privacy.dlp.v2.CreateInspectTemplateRequest;
import com.google.privacy.dlp.v2.DeidentifyConfig;
Expand All @@ -72,6 +75,7 @@
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -80,6 +84,10 @@
public class SnippetsIT {

private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String FOLDER_ID = System.getenv()
.getOrDefault("MA_FOLDER_ID", "global");
private static final String ORGANIZATION_ID = System.getenv()
.getOrDefault("MA_ORG_ID", "global");
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",
Expand All @@ -99,7 +107,11 @@ public class SnippetsIT {
private static String TEST_DEIDENTIFY_TEMPLATE_NAME;
private ByteArrayOutputStream stdOut;
private PrintStream originalOut;
private static String[] floorSettingNames;
private static String[] templateToDelete;
private static String projectFloorSettingName;
private static String folderFloorSettingName;
private static String organizationFloorSettingName;

// Check if the required environment variables are set.
private static void requireEnvVar(String varName) {
Expand All @@ -111,6 +123,14 @@ private static void requireEnvVar(String varName) {
@BeforeClass
public static void beforeAll() throws IOException {
requireEnvVar("GOOGLE_CLOUD_PROJECT");
requireEnvVar("MA_FOLDER_ID");
requireEnvVar("MA_ORG_ID");

projectFloorSettingName =
FloorSettingName.ofProjectLocationName(PROJECT_ID, "global").toString();
folderFloorSettingName = FloorSettingName.ofFolderLocationName(FOLDER_ID, "global").toString();
organizationFloorSettingName =
FloorSettingName.ofOrganizationLocationName(ORGANIZATION_ID, "global").toString();

TEST_TEMPLATE_ID = randomId();
TEST_RAI_TEMPLATE_ID = randomId();
Expand Down Expand Up @@ -147,6 +167,10 @@ private static String randomId() {
@AfterClass
public static void afterAll() throws IOException {
requireEnvVar("GOOGLE_CLOUD_PROJECT");
requireEnvVar("MA_FOLDER_ID");
requireEnvVar("MA_ORG_ID");

resetFloorSettings();

// Delete templates after running tests.
templateToDelete = new String[] {
Expand Down Expand Up @@ -380,6 +404,67 @@ private static void deleteTemplate(String templateId) throws IOException {
}
}

private static void resetFloorSettings() throws IOException {
floorSettingNames = new String[] {
projectFloorSettingName, folderFloorSettingName, organizationFloorSettingName
};


try (ModelArmorClient client = ModelArmorClient.create()) {
for (String name : floorSettingNames) {
FloorSetting floorSetting = FloorSetting.newBuilder()
.setName(name)
.setFilterConfig(FilterConfig.newBuilder().build())
.setEnableFloorSettingEnforcement(false)
.build();

UpdateFloorSettingRequest request = UpdateFloorSettingRequest.newBuilder()
.setFloorSetting(floorSetting)
.build();

client.updateFloorSetting(request);
}
}
}

// Tests for Folder setting snippets.
@Test
public void testGetOrganizationFloorSetting() throws IOException {
GetOrganizationFloorSetting.getOrganizationFloorSetting(ORGANIZATION_ID);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have assertions for the actual value that was fetched by the getOrganizationFloorSetting method? Similarly for other tests.

Copy link
Author

@mihirvala-crestdata mihirvala-crestdata Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We followed a similar pattern to other snippet tests (see Secret Manager tests). The goal of these tests is to verify that the snippets run successfully by asserting the presence of a success log.

assertThat(stdOut.toString()).contains("Fetched floor setting for organization:");
}

@Test
public void testGetFolderFloorSetting() throws IOException {
GetFolderFloorSetting.getFolderFloorSetting(FOLDER_ID);
assertThat(stdOut.toString()).contains("Fetched floor setting for folder:");
}

@Test
public void testGetProjectFloorSetting() throws IOException {
GetProjectFloorSetting.getProjectFloorSetting(PROJECT_ID);
assertThat(stdOut.toString()).contains("Fetched floor setting for project:");
}

@Test
public void testUpdateOrganizationFloorSetting() throws IOException {
UpdateOrganizationsFloorSetting.updateOrganizationFloorSetting(ORGANIZATION_ID);
assertThat(stdOut.toString()).contains("Updated floor setting for organization:");
}

@Test
public void testUpdateFolderFloorSetting() throws IOException {
UpdateFolderFloorSetting.updateFolderFloorSetting(FOLDER_ID);
assertThat(stdOut.toString()).contains("Updated floor setting for folder:");
}


@Test
public void testUpdateProjectFloorSetting() throws IOException {
UpdateProjectFloorSetting.updateProjectFloorSetting(PROJECT_ID);
assertThat(stdOut.toString()).contains("Updated floor setting for project:");
}

// Tests for Template CRUD snippets.
@Test
public void testUpdateModelArmorTemplate() throws IOException {
Expand Down