From 433599c4620ebcb19439a14f9281e5ffda7bbdc0 Mon Sep 17 00:00:00 2001 From: Rafael Rodrigues Date: Thu, 6 Jun 2024 12:56:51 +0000 Subject: [PATCH] change snippet response and mock static --- .../vtwo/bigquery/CreateBigQueryExport.java | 3 +- .../java/vtwo/bigquery/GetBigQueryExport.java | 3 +- .../vtwo/bigquery/ListBigQueryExports.java | 5 +- .../vtwo/bigquery/UpdateBigQueryExport.java | 10 +- .../src/test/java/vtwo/BigQueryExportIT.java | 295 +++++++++--------- 5 files changed, 155 insertions(+), 161 deletions(-) diff --git a/security-command-center/snippets/src/main/java/vtwo/bigquery/CreateBigQueryExport.java b/security-command-center/snippets/src/main/java/vtwo/bigquery/CreateBigQueryExport.java index b065ecd6add..ae4a9d26a81 100644 --- a/security-command-center/snippets/src/main/java/vtwo/bigquery/CreateBigQueryExport.java +++ b/security-command-center/snippets/src/main/java/vtwo/bigquery/CreateBigQueryExport.java @@ -55,7 +55,7 @@ public static void main(String[] args) throws IOException { // Create export configuration to export findings from a project to a BigQuery dataset. // Optionally specify filter to export certain findings only. - public static void createBigQueryExport(String organizationId, String location, + public static BigQueryExport createBigQueryExport(String organizationId, String location, String projectId, String filter, String bigQueryDatasetId, String bigQueryExportId) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created @@ -84,6 +84,7 @@ public static void createBigQueryExport(String organizationId, String location, BigQueryExport response = client.createBigQueryExport(bigQueryExportRequest); System.out.printf("BigQuery export request created successfully: %s\n", response.getName()); + return response; } } } diff --git a/security-command-center/snippets/src/main/java/vtwo/bigquery/GetBigQueryExport.java b/security-command-center/snippets/src/main/java/vtwo/bigquery/GetBigQueryExport.java index f419d250f72..d58a52ff351 100644 --- a/security-command-center/snippets/src/main/java/vtwo/bigquery/GetBigQueryExport.java +++ b/security-command-center/snippets/src/main/java/vtwo/bigquery/GetBigQueryExport.java @@ -41,7 +41,7 @@ public static void main(String[] args) throws IOException { } // Retrieve an existing BigQuery export. - public static void getBigQueryExport(String organizationId, String location, + public static BigQueryExport getBigQueryExport(String organizationId, String location, String bigQueryExportId) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. @@ -57,6 +57,7 @@ public static void getBigQueryExport(String organizationId, String location, BigQueryExport response = client.getBigQueryExport(bigQueryExportRequest); System.out.printf("Retrieved the BigQuery export: %s", response.getName()); + return response; } } } diff --git a/security-command-center/snippets/src/main/java/vtwo/bigquery/ListBigQueryExports.java b/security-command-center/snippets/src/main/java/vtwo/bigquery/ListBigQueryExports.java index 3e4ad0f196f..432864f4b1d 100644 --- a/security-command-center/snippets/src/main/java/vtwo/bigquery/ListBigQueryExports.java +++ b/security-command-center/snippets/src/main/java/vtwo/bigquery/ListBigQueryExports.java @@ -39,8 +39,8 @@ public static void main(String[] args) throws IOException { } // List BigQuery exports in the given parent. - public static void listBigQueryExports(String organizationId, String location) - throws IOException { + public static ListBigQueryExportsPagedResponse listBigQueryExports(String organizationId, + String location) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. try (SecurityCenterClient client = SecurityCenterClient.create()) { @@ -57,6 +57,7 @@ public static void listBigQueryExports(String organizationId, String location) for (BigQueryExport bigQueryExport : response.iterateAll()) { System.out.println(bigQueryExport.getName()); } + return response; } } } diff --git a/security-command-center/snippets/src/main/java/vtwo/bigquery/UpdateBigQueryExport.java b/security-command-center/snippets/src/main/java/vtwo/bigquery/UpdateBigQueryExport.java index 181b4fc4501..8c8261884cf 100644 --- a/security-command-center/snippets/src/main/java/vtwo/bigquery/UpdateBigQueryExport.java +++ b/security-command-center/snippets/src/main/java/vtwo/bigquery/UpdateBigQueryExport.java @@ -50,9 +50,8 @@ public static void main(String[] args) throws IOException { } // Updates an existing BigQuery export. - public static void updateBigQueryExport(String organizationId, String location, String filter, - String bigQueryExportId) - throws IOException { + public static BigQueryExport updateBigQueryExport(String organizationId, String location, + String filter, String bigQueryExportId) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. try (SecurityCenterClient client = SecurityCenterClient.create()) { @@ -83,11 +82,8 @@ public static void updateBigQueryExport(String organizationId, String location, .build(); BigQueryExport response = client.updateBigQueryExport(request); - if (!response.getFilter().equalsIgnoreCase(filter)) { - System.out.println("Failed to update BigQueryExport!"); - return; - } System.out.println("BigQueryExport updated successfully!"); + return response; } } } diff --git a/security-command-center/snippets/src/test/java/vtwo/BigQueryExportIT.java b/security-command-center/snippets/src/test/java/vtwo/BigQueryExportIT.java index f31017056ba..90406dc1159 100644 --- a/security-command-center/snippets/src/test/java/vtwo/BigQueryExportIT.java +++ b/security-command-center/snippets/src/test/java/vtwo/BigQueryExportIT.java @@ -16,6 +16,7 @@ package vtwo; +import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; @@ -26,6 +27,7 @@ import com.google.cloud.securitycenter.v2.BigQueryExportName; import com.google.cloud.securitycenter.v2.CreateBigQueryExportRequest; import com.google.cloud.securitycenter.v2.DeleteBigQueryExportRequest; +import com.google.cloud.securitycenter.v2.Finding.State; import com.google.cloud.securitycenter.v2.GetBigQueryExportRequest; import com.google.cloud.securitycenter.v2.ListBigQueryExportsRequest; import com.google.cloud.securitycenter.v2.OrganizationLocationName; @@ -48,177 +50,170 @@ public class BigQueryExportIT { - public static final String ORGANIZATION_ID = "test-organization-id"; - public static final String PROJECT_ID = "test-project-id"; + private static final String ORGANIZATION_ID = "test-organization-id"; + private static final String PROJECT_ID = "test-project-id"; private static final String LOCATION = "global"; private static final String BQ_DATASET_NAME = "test-dataset-id"; private static final String BQ_EXPORT_ID = "test-export-id"; + private static MockedStatic clientMock; + private static SecurityCenterClient client; + + @BeforeClass + public static void setUp() { + client = mock(SecurityCenterClient.class); + clientMock = Mockito.mockStatic(SecurityCenterClient.class); + clientMock.when(SecurityCenterClient::create).thenReturn(client); + } + + @AfterClass + public static void cleanUp() { + clientMock.reset(); + } @Test public void testCreateBigQueryExport() throws IOException { - // Mock SecurityCenterClient. - SecurityCenterClient client = mock(SecurityCenterClient.class); - try (MockedStatic clientMock = Mockito.mockStatic( - SecurityCenterClient.class)) { - clientMock.when(SecurityCenterClient::create).thenReturn(client); - // Define test data. - String filter = "test-filter"; - // Build the parent of the request. - OrganizationLocationName organizationName = OrganizationLocationName.of(ORGANIZATION_ID, - LOCATION); - // Build the BigQueryExport response. - BigQueryExport expectedExport = BigQueryExport.newBuilder() - .setDescription( - "Export low and medium findings if the compute resource has an IAM anomalous grant") - .setFilter(filter) - .setDataset(String.format("projects/%s/datasets/%s", PROJECT_ID, BQ_DATASET_NAME)) - .build(); - // Build the CreateBigQueryExportRequest request. - CreateBigQueryExportRequest expectedRequest = CreateBigQueryExportRequest.newBuilder() - .setParent(organizationName.toString()) - .setBigQueryExport(expectedExport) - .setBigQueryExportId(BQ_EXPORT_ID) - .build(); - // Mock the createBigQueryExport method to return the expected response. - when(client.createBigQueryExport(any())).thenReturn(expectedExport); - - // Call the createBigQueryExport method. - CreateBigQueryExport.createBigQueryExport(ORGANIZATION_ID, LOCATION, PROJECT_ID, filter, - BQ_DATASET_NAME, BQ_EXPORT_ID); - - // Verify that the createBigQueryExport method was called with the expected request. - verify(client).createBigQueryExport(expectedRequest); - } + // Mocking and test data setup. + String filter = "test-filter"; + OrganizationLocationName organizationName = OrganizationLocationName.of(ORGANIZATION_ID, + LOCATION); + // Building expected BigQueryExport. + BigQueryExport expectedExport = BigQueryExport.newBuilder() + .setDescription( + "Export low and medium findings if the compute resource has an IAM anomalous grant") + .setFilter(filter) + .setDataset(String.format("projects/%s/datasets/%s", PROJECT_ID, BQ_DATASET_NAME)) + .build(); + // Building CreateBigQueryExportRequest. + CreateBigQueryExportRequest request = CreateBigQueryExportRequest.newBuilder() + .setParent(organizationName.toString()) + .setBigQueryExport(expectedExport) + .setBigQueryExportId(BQ_EXPORT_ID) + .build(); + // Mocking createBigQueryExport. + when(client.createBigQueryExport(request)).thenReturn(expectedExport); + + // Calling createBigQueryExport. + BigQueryExport response = CreateBigQueryExport.createBigQueryExport(ORGANIZATION_ID, LOCATION, + PROJECT_ID, filter, + BQ_DATASET_NAME, BQ_EXPORT_ID); + // Verifying createBigQueryExport was called. + verify(client).createBigQueryExport(request); + + // Asserts the created BigQueryExport matches the expected request. + assertThat(response).isEqualTo(expectedExport); } @Test public void testDeleteBigQueryExport() throws IOException { - // Mock SecurityCenterClient. - SecurityCenterClient client = mock(SecurityCenterClient.class); - try (MockedStatic clientMock = Mockito.mockStatic( - SecurityCenterClient.class)) { - clientMock.when(SecurityCenterClient::create).thenReturn(client); - // Build the BigQuery export name. - BigQueryExportName bigQueryExportName = BigQueryExportName.of(ORGANIZATION_ID, LOCATION, - BQ_EXPORT_ID); - // Build the delete request. - DeleteBigQueryExportRequest bigQueryExportRequest = DeleteBigQueryExportRequest.newBuilder() - .setName(bigQueryExportName.toString()) - .build(); - // Mock the deleteBigQueryExport method to return successfully. - doNothing().when(client).deleteBigQueryExport(bigQueryExportRequest); - - // Call the deleteBigQueryExport method. - DeleteBigQueryExport.deleteBigQueryExport(ORGANIZATION_ID, LOCATION, BQ_EXPORT_ID); - - // Verify that the deleteBigQueryExport method was called with the expected request. - verify(client).deleteBigQueryExport(bigQueryExportRequest); - } + // Building BigQueryExportName. + BigQueryExportName bigQueryExportName = BigQueryExportName.of(ORGANIZATION_ID, LOCATION, + BQ_EXPORT_ID); + // Building DeleteBigQueryExportRequest. + DeleteBigQueryExportRequest request = DeleteBigQueryExportRequest.newBuilder() + .setName(bigQueryExportName.toString()) + .build(); + // Mocking deleteBigQueryExport. + doNothing().when(client).deleteBigQueryExport(request); + + // Calling deleteBigQueryExport. + DeleteBigQueryExport.deleteBigQueryExport(ORGANIZATION_ID, LOCATION, BQ_EXPORT_ID); + + // Verifying deleteBigQueryExport was called. + verify(client).deleteBigQueryExport(request); } @Test public void testGetBigQueryExport() throws IOException { - // Mock SecurityCenterClient. - SecurityCenterClient client = mock(SecurityCenterClient.class); - try (MockedStatic clientMock = Mockito.mockStatic( - SecurityCenterClient.class)) { - clientMock.when(SecurityCenterClient::create).thenReturn(client); - // Build BigQueryExport response. - BigQueryExport expectedExport = BigQueryExport.newBuilder() - .setName( - String.format("organizations/%s/locations/%s/bigQueryExports/%s", ORGANIZATION_ID, - LOCATION, BQ_EXPORT_ID)) - .build(); - // Build the BigQueryExportName and request. - BigQueryExportName bigQueryExportName = BigQueryExportName.of(ORGANIZATION_ID, LOCATION, - BQ_EXPORT_ID); - // Build the GetBigQueryExportRequest request. - GetBigQueryExportRequest request = GetBigQueryExportRequest.newBuilder() - .setName(bigQueryExportName.toString()) - .build(); - // Mock the getBigQueryExport method to return the expected response. - when(client.getBigQueryExport(request)).thenReturn(expectedExport); - - // Call the getBigQueryExport method. - GetBigQueryExport.getBigQueryExport(ORGANIZATION_ID, LOCATION, BQ_EXPORT_ID); - - // Verify that the getBigQueryExport method was called with the expected request. - verify(client).getBigQueryExport(request); - } + // Building Expected BigQueryExport. + BigQueryExport expectedExport = BigQueryExport.newBuilder() + .setName( + String.format("organizations/%s/locations/%s/bigQueryExports/%s", ORGANIZATION_ID, + LOCATION, BQ_EXPORT_ID)) + .build(); + // Build the BigQueryExportName and request. + BigQueryExportName bigQueryExportName = BigQueryExportName.of(ORGANIZATION_ID, LOCATION, + BQ_EXPORT_ID); + GetBigQueryExportRequest request = GetBigQueryExportRequest.newBuilder() + .setName(bigQueryExportName.toString()) + .build(); + // Mocking getBigQueryExport. + when(client.getBigQueryExport(request)).thenReturn(expectedExport); + + // Calling getBigQueryExport. + BigQueryExport response = GetBigQueryExport.getBigQueryExport(ORGANIZATION_ID, LOCATION, + BQ_EXPORT_ID); + // Verifying getBigQueryExport was called. + verify(client).getBigQueryExport(request); + + // Verifies the retrieved BigQueryExport matches the expected export. + assertThat(response).isEqualTo(expectedExport); } @Test public void testListBigQueryExports() throws IOException { - // Mock SecurityCenterClient. - SecurityCenterClient client = mock(SecurityCenterClient.class); - try (MockedStatic clientMock = Mockito.mockStatic( - SecurityCenterClient.class)) { - clientMock.when(SecurityCenterClient::create).thenReturn(client); - // Define test data. - String exportId1 = "export-1"; - String exportId2 = "export-2"; - // Build BigQueryExport objects for the response. - BigQueryExport export1 = BigQueryExport.newBuilder() - .setName( - String.format("organizations/%s/locations/%s/bigQueryExports/%s", ORGANIZATION_ID, - LOCATION, exportId1)) - .build(); - BigQueryExport export2 = BigQueryExport.newBuilder() - .setName( - String.format("organizations/%s/locations/%s/bigQueryExports/%s", ORGANIZATION_ID, - LOCATION, exportId2)) - .build(); - // Mock the ListBigQueryExportsPagedResponse. - ListBigQueryExportsPagedResponse pagedResponse = mock(ListBigQueryExportsPagedResponse.class); - when(pagedResponse.iterateAll()).thenReturn(ImmutableList.of(export1, export2)); - // Mock the client.listBigQueryExports method to return the paged response. - when(client.listBigQueryExports(any(ListBigQueryExportsRequest.class))) - .thenReturn(pagedResponse); - - // Call the listBigQueryExports method. - ListBigQueryExports.listBigQueryExports(ORGANIZATION_ID, LOCATION); - - // Verify that the client.listBigQueryExports method was called with the expected request. - verify(client).listBigQueryExports(ListBigQueryExportsRequest.newBuilder() - .setParent(OrganizationLocationName.of(ORGANIZATION_ID, LOCATION).toString()) - .build()); - } + String exportId1 = "export-1"; + String exportId2 = "export-2"; + // Building Expected BigQueryExports. + BigQueryExport export1 = BigQueryExport.newBuilder() + .setName( + String.format("organizations/%s/locations/%s/bigQueryExports/%s", ORGANIZATION_ID, + LOCATION, exportId1)) + .build(); + BigQueryExport export2 = BigQueryExport.newBuilder() + .setName( + String.format("organizations/%s/locations/%s/bigQueryExports/%s", ORGANIZATION_ID, + LOCATION, exportId2)) + .build(); + // Mocking ListBigQueryExportsPagedResponse. + ListBigQueryExportsPagedResponse pagedResponse = mock(ListBigQueryExportsPagedResponse.class); + when(pagedResponse.iterateAll()).thenReturn(ImmutableList.of(export1, export2)); + // Building ListBigQueryExportsRequest. + ListBigQueryExportsRequest request = ListBigQueryExportsRequest.newBuilder() + .setParent(OrganizationLocationName.of(ORGANIZATION_ID, LOCATION).toString()) + .build(); + // Mock the client.listBigQueryExports method to return the paged response. + when(client.listBigQueryExports(request)).thenReturn(pagedResponse); + + // Calling listBigQueryExports. + ListBigQueryExportsPagedResponse response = ListBigQueryExports.listBigQueryExports( + ORGANIZATION_ID, LOCATION); + // Verifying client.listBigQueryExports was called. + verify(client).listBigQueryExports(request); + + // Ensures the response from listBigQueryExports matches the mocked paged response. + assertThat(response).isEqualTo(pagedResponse); } @Test public void testUpdateBigQueryExport() throws IOException { - // Mock SecurityCenterClient. - SecurityCenterClient client = mock(SecurityCenterClient.class); - try (MockedStatic clientMock = Mockito.mockStatic( - SecurityCenterClient.class)) { - clientMock.when(SecurityCenterClient::create).thenReturn(client); - // Define test data. - String filter = "updated filter"; - // Build the expected BigQueryExport response. - BigQueryExport expectedExport = BigQueryExport.newBuilder() - .setName( - String.format("organizations/%s/locations/%s/bigQueryExports/%s", ORGANIZATION_ID, - LOCATION, BQ_EXPORT_ID)) - .setFilter(filter) - .setDescription("Updated description.") - .build(); - // Build BigQueryExportName, UpdateMask, and request. - FieldMask updateMask = FieldMask.newBuilder().addPaths("filter").addPaths("description") - .build(); - // Build the UpdateBigQueryExportRequest. - UpdateBigQueryExportRequest request = UpdateBigQueryExportRequest.newBuilder() - .setBigQueryExport(expectedExport) - .setUpdateMask(updateMask) - .build(); - // Mock the updateBigQueryExport method to return the expected response. - when(client.updateBigQueryExport(request)).thenReturn(expectedExport); - - // Call the updateBigQueryExport method. - UpdateBigQueryExport.updateBigQueryExport(ORGANIZATION_ID, LOCATION, filter, BQ_EXPORT_ID); - - // Verify that the updateBigQueryExport method was called with the expected request. - verify(client).updateBigQueryExport(request); - } + String filter = "updated filter"; + String name = String.format("organizations/%s/locations/%s/bigQueryExports/%s", + ORGANIZATION_ID, + LOCATION, BQ_EXPORT_ID); + // Building expected BigQueryExport. + BigQueryExport expectedExport = BigQueryExport.newBuilder() + .setName(name) + .setFilter(filter) + .setDescription("Updated description.") + .build(); + // Building Update Parameters. + FieldMask updateMask = FieldMask.newBuilder().addPaths("filter").addPaths("description") + .build(); + UpdateBigQueryExportRequest request = UpdateBigQueryExportRequest.newBuilder() + .setBigQueryExport(expectedExport) + .setUpdateMask(updateMask) + .build(); + // Mocking updateBigQueryExport. + when(client.updateBigQueryExport(request)).thenReturn(expectedExport); + + // Calling updateBigQueryExport. + BigQueryExport response = UpdateBigQueryExport.updateBigQueryExport(ORGANIZATION_ID, LOCATION, + filter, BQ_EXPORT_ID); + // Verifying updateBigQueryExport was called. + verify(client).updateBigQueryExport(request); + + // Ensures the updated BigQuery Export name matches the expected name. + assertThat(response.getName()).isEqualTo(name); } -} \ No newline at end of file +} \ No newline at end of file