public static NoteName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "NoteName.parse: formattedString not in valid format"); return of(matchMap.get("project"), matchMap.get("note")); }
@Test @SuppressWarnings("all") public void testIamPermissionsExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockContainerAnalysisV1Beta1.addException(exception); try { IamResourceName resource = NoteName.of("[PROJECT]", "[NOTE]"); List<String> permissions = new ArrayList<>(); client.testIamPermissions(resource, permissions); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void deleteNoteExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockGrafeasV1Beta1.addException(exception); try { NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); client.deleteNote(name); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void getIamPolicyExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockContainerAnalysisV1Beta1.addException(exception); try { IamResourceName resource = NoteName.of("[PROJECT]", "[NOTE]"); client.getIamPolicy(resource); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void getNoteExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockGrafeasV1Beta1.addException(exception); try { NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); client.getNote(name); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void listNoteOccurrencesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockGrafeasV1Beta1.addException(exception); try { NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); String filter = "filter-1274492040"; client.listNoteOccurrences(name, filter); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void setIamPolicyExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockContainerAnalysisV1Beta1.addException(exception); try { IamResourceName resource = NoteName.of("[PROJECT]", "[NOTE]"); Policy policy = Policy.newBuilder().build(); client.setIamPolicy(resource, policy); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void deleteNoteTest() { Empty expectedResponse = Empty.newBuilder().build(); mockGrafeasV1Beta1.addResponse(expectedResponse); NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); client.deleteNote(name); List<GeneratedMessageV3> actualRequests = mockGrafeasV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); DeleteNoteRequest actualRequest = (DeleteNoteRequest) actualRequests.get(0); Assert.assertEquals(name, NoteName.parse(actualRequest.getName())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * Deletes an existing Note from the server * @param client The Grafeas client used to perform the API requests. * @param noteId the identifier of the Note to delete * @param projectId the GCP project the Note belongs to */ public static void deleteNote(GrafeasV1Beta1Client client, String noteId, String projectId) { final NoteName noteName = NoteName.of(projectId, noteId); client.deleteNote(noteName); } // [END delete_note]
@Test @SuppressWarnings("all") public void listNoteOccurrencesTest() { String nextPageToken = ""; Occurrence occurrencesElement = Occurrence.newBuilder().build(); List<Occurrence> occurrences = Arrays.asList(occurrencesElement); ListNoteOccurrencesResponse expectedResponse = ListNoteOccurrencesResponse.newBuilder() .setNextPageToken(nextPageToken) .addAllOccurrences(occurrences) .build(); mockGrafeasV1Beta1.addResponse(expectedResponse); NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); String filter = "filter-1274492040"; ListNoteOccurrencesPagedResponse pagedListResponse = client.listNoteOccurrences(name, filter); List<Occurrence> resources = Lists.newArrayList(pagedListResponse.iterateAll()); Assert.assertEquals(1, resources.size()); Assert.assertEquals(expectedResponse.getOccurrencesList().get(0), resources.get(0)); List<GeneratedMessageV3> actualRequests = mockGrafeasV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); ListNoteOccurrencesRequest actualRequest = (ListNoteOccurrencesRequest) actualRequests.get(0); Assert.assertEquals(name, NoteName.parse(actualRequest.getName())); Assert.assertEquals(filter, actualRequest.getFilter()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void getIamPolicyTest() { int version = 351608024; ByteString etag = ByteString.copyFromUtf8("21"); Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); mockContainerAnalysisV1Beta1.addResponse(expectedResponse); IamResourceName resource = NoteName.of("[PROJECT]", "[NOTE]"); Policy actualResponse = client.getIamPolicy(resource); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockContainerAnalysisV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); GetIamPolicyRequest actualRequest = (GetIamPolicyRequest) actualRequests.get(0); Assert.assertEquals(resource, IamResourceNames.parse(actualRequest.getResource())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void testIamPermissionsTest() { TestIamPermissionsResponse expectedResponse = TestIamPermissionsResponse.newBuilder().build(); mockContainerAnalysisV1Beta1.addResponse(expectedResponse); IamResourceName resource = NoteName.of("[PROJECT]", "[NOTE]"); List<String> permissions = new ArrayList<>(); TestIamPermissionsResponse actualResponse = client.testIamPermissions(resource, permissions); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockContainerAnalysisV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); TestIamPermissionsRequest actualRequest = (TestIamPermissionsRequest) actualRequests.get(0); Assert.assertEquals(resource, IamResourceNames.parse(actualRequest.getResource())); Assert.assertEquals(permissions, actualRequest.getPermissionsList()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void updateNoteExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockGrafeasV1Beta1.addException(exception); try { NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); Note note = Note.newBuilder().build(); FieldMask updateMask = FieldMask.newBuilder().build(); client.updateNote(name, note, updateMask); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
/** * Retrieves and prints a specified Note from the server * @param client The Grafeas client used to perform the API requests. * @param noteId the Note's unique identifier * @param projectId the GCP project the Note belongs to * @return the requested Note object */ public static Note getNote(GrafeasV1Beta1Client client, String noteId, String projectId) { final NoteName noteName = NoteName.of(projectId, noteId); Note n = client.getNote(noteName); System.out.println(n); return n; } // [END get_note]
@Test @SuppressWarnings("all") public void setIamPolicyTest() { int version = 351608024; ByteString etag = ByteString.copyFromUtf8("21"); Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); mockContainerAnalysisV1Beta1.addResponse(expectedResponse); IamResourceName resource = NoteName.of("[PROJECT]", "[NOTE]"); Policy policy = Policy.newBuilder().build(); Policy actualResponse = client.setIamPolicy(resource, policy); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockContainerAnalysisV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); SetIamPolicyRequest actualRequest = (SetIamPolicyRequest) actualRequests.get(0); Assert.assertEquals(resource, IamResourceNames.parse(actualRequest.getResource())); Assert.assertEquals(policy, actualRequest.getPolicy()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void getNoteTest() { String name2 = "name2-1052831874"; String shortDescription = "shortDescription-235369287"; String longDescription = "longDescription-1747792199"; Note expectedResponse = Note.newBuilder() .setName(name2) .setShortDescription(shortDescription) .setLongDescription(longDescription) .build(); mockGrafeasV1Beta1.addResponse(expectedResponse); NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); Note actualResponse = client.getNote(name); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockGrafeasV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); GetNoteRequest actualRequest = (GetNoteRequest) actualRequests.get(0); Assert.assertEquals(name, NoteName.parse(actualRequest.getName())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void updateNoteTest() { String name2 = "name2-1052831874"; String shortDescription = "shortDescription-235369287"; String longDescription = "longDescription-1747792199"; Note expectedResponse = Note.newBuilder() .setName(name2) .setShortDescription(shortDescription) .setLongDescription(longDescription) .build(); mockGrafeasV1Beta1.addResponse(expectedResponse); NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); Note note = Note.newBuilder().build(); FieldMask updateMask = FieldMask.newBuilder().build(); Note actualResponse = client.updateNote(name, note, updateMask); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockGrafeasV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); UpdateNoteRequest actualRequest = (UpdateNoteRequest) actualRequests.get(0); Assert.assertEquals(name, NoteName.parse(actualRequest.getName())); Assert.assertEquals(note, actualRequest.getNote()); Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * Retrieves all the Occurrences associated with a specified Note * Here, all Occurrences are printed and counted * @param client The Grafeas client used to perform the API requests. * @param noteId the Note's unique identifier * @param projectId the GCP project the Note belongs to * @return number of Occurrences found */ public static int getOccurrencesForNote(GrafeasV1Beta1Client client, String noteId, String projectId) { final NoteName noteName = NoteName.of(projectId, noteId); int i = 0; ListNoteOccurrencesRequest request = ListNoteOccurrencesRequest.newBuilder() .setName(noteName.toString()) .build(); for (Occurrence o : client.listNoteOccurrences(request).iterateAll()) { // Write custom code to process each Occurrence here System.out.println(o.getName()); i = i + 1; } return i; } // [END occurrences_for_note]
/** * Pushes an update to a Note that already exists on the server * @param client The Grafeas client used to perform the API requests. * @param updated a Note object representing the desired updates to push * @param noteId the identifier of the existing Note * @param projectId the GCP project the Note belongs to */ public static Note updateNote(GrafeasV1Beta1Client client, Note updated, String noteId, String projectId) { final NoteName noteName = NoteName.of(projectId, noteId); UpdateNoteRequest request = UpdateNoteRequest.newBuilder() .setName(noteName.toString()) .setNote(updated) .build(); return client.updateNote(request); } // [END update_note]
/** * Creates and returns a new Occurrence of a previously created vulnerability Note * @param client The Grafeas client used to perform the API requests. * @param imageUrl the Container Registry URL associated with the image * example: "https://gcr.io/project/image@sha256:foo" * @param noteId the identifier of the Note associated with this Occurrence * @param occProjectId the GCP project the Occurrence will be created under * @param noteProjectId the GCP project the associated Note belongs to * @return the newly created Occurrence object */ public static Occurrence createOccurrence(GrafeasV1Beta1Client client, String imageUrl, String noteId, String occProjectId, String noteProjectId) { final NoteName noteName = NoteName.of(noteProjectId, noteId); final String occProjectName = ProjectName.format(occProjectId); Occurrence.Builder occBuilder = Occurrence.newBuilder(); occBuilder.setNoteName(noteName.toString()); Details.Builder detailsBuilder = Details.newBuilder(); // Details about the vulnerability instance can be added here occBuilder.setVulnerability(detailsBuilder); // Attach the occurrence to the associated image uri Resource.Builder resourceBuilder = Resource.newBuilder(); resourceBuilder.setUri(imageUrl); occBuilder.setResource(resourceBuilder); Occurrence newOcc = occBuilder.build(); return client.createOccurrence(occProjectName, newOcc); } // [END create_occurrence]