### Question:
EnvironmentAccessChecker { public EnvironmentAccessRights hasAccess(String memberCrn, Optional<String> requestId) { requireNonNull(memberCrn, "memberCrn is null"); requireNonNull(requestId, "requestId is null"); try { List<Boolean> hasRights = grpcUmsClient.hasRights(INTERNAL_ACTOR_CRN, memberCrn, rightChecks, requestId); return new EnvironmentAccessRights(hasRights.get(0), hasRights.get(1)); } catch (StatusRuntimeException e) { if (e.getStatus().getCode() == Code.NOT_FOUND) { LOGGER.warn("Member CRN {} not found in UMS. Treating as if member has no rights to environment {}: {}", memberCrn, environmentCrn, e.getLocalizedMessage()); return new EnvironmentAccessRights(false, false); } else { throw e; } } } EnvironmentAccessChecker(GrpcUmsClient grpcUmsClient, String environmentCrn, List<RightCheck> rightChecks); EnvironmentAccessRights hasAccess(String memberCrn, Optional<String> requestId);  }### Answer:
@Test void testEnvironmentAccessCheckerChecksRightRightChecks() { EnvironmentAccessChecker underTest = environmentAccessCheckerFactory.create(ENV_CRN); ArgumentCaptor<List<AuthorizationProto.RightCheck>> argumentCaptor = ArgumentCaptor.forClass((Class) List.class); when(grpcUmsClient.hasRights( anyString(), eq(MEMBER_CRN), anyList(), any(Optional.class))).thenReturn(List.of(true, true)); underTest.hasAccess(MEMBER_CRN, Optional.empty()); verify(grpcUmsClient).hasRights(eq(INTERNAL_ACTOR_CRN), eq(MEMBER_CRN), argumentCaptor.capture(), any()); List<AuthorizationProto.RightCheck> capturedRightChecks = argumentCaptor.getValue(); assertEquals(2, capturedRightChecks.size()); AuthorizationProto.RightCheck hasAccess = capturedRightChecks.get(0); Assertions.assertEquals(UserSyncConstants.ACCESS_ENVIRONMENT, hasAccess.getRight()); assertEquals(ENV_CRN, hasAccess.getResource()); AuthorizationProto.RightCheck isAdmin = capturedRightChecks.get(1); assertEquals(UserSyncConstants.ADMIN_FREEIPA, isAdmin.getRight()); }
@Test void testEnvironmentAccessCheckerCreatesRightEnvironmentAccessRights() { EnvironmentAccessChecker underTest = environmentAccessCheckerFactory.create(ENV_CRN); for (boolean hasAccess : new boolean[] { false, true}) { for (boolean ipaAdmin : new boolean[] { false, true}) { when(grpcUmsClient.hasRights(anyString(), eq(MEMBER_CRN), anyList(), any(Optional.class))).thenReturn(List.of(hasAccess, ipaAdmin)); EnvironmentAccessRights environmentAccessRights = underTest.hasAccess(MEMBER_CRN, Optional.empty()); assertEquals(hasAccess, environmentAccessRights.hasEnvironmentAccessRight()); assertEquals(ipaAdmin, environmentAccessRights.hasAdminFreeIpaRight()); } } }
@Test void testEnvironmentAccessCheckerNoAccessIfMemberNotFound() { EnvironmentAccessChecker underTest = environmentAccessCheckerFactory.create(ENV_CRN); Throwable ex = new StatusRuntimeException(Status.Code.NOT_FOUND.toStatus()); when(grpcUmsClient.hasRights(anyString(), eq(MEMBER_CRN), anyList(), any(Optional.class))).thenThrow(ex); EnvironmentAccessRights environmentAccessRights = underTest.hasAccess(MEMBER_CRN, Optional.empty()); assertFalse(environmentAccessRights.hasEnvironmentAccessRight()); assertFalse(environmentAccessRights.hasAdminFreeIpaRight()); }