@Test public void testResourceManagerException() { ResourceManagerException exception = new ResourceManagerException(500, "message"); assertEquals(500, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertTrue(exception.isRetryable()); exception = new ResourceManagerException(503, "message"); assertEquals(503, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertTrue(exception.isRetryable()); exception = new ResourceManagerException(429, "message"); assertEquals(429, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertTrue(exception.isRetryable()); exception = new ResourceManagerException(403, "message"); assertEquals(403, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertFalse(exception.isRetryable()); exception = new ResourceManagerException(cause); assertNull(exception.getReason()); assertNull(exception.getMessage()); assertTrue(exception.isRetryable());
@Override public List<Boolean> testPermissions(final String projectId, final List<String> permissions) { try { return runWithRetries( new Callable<List<Boolean>>() { @Override public List<Boolean> call() { return resourceManagerRpc.testPermissions(projectId, permissions); } }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); } }
private void expectInvalidArgumentException( com.google.api.services.cloudresourcemanager.model.Project project, String errorMessageSubstring) { try { rpc.create(project); fail("Should fail because of an invalid argument."); } catch (ResourceManagerException e) { assertEquals(400, e.getCode()); assertTrue(e.getMessage().contains(errorMessageSubstring)); } }
private static ResourceManagerException translate(IOException exception) { return new ResourceManagerException(exception); }
@Test public void testTranslateAndThrow() throws Exception { Exception cause = new ResourceManagerException(503, "message"); RetryHelperException exceptionMock = createMock(RetryHelperException.class); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { ResourceManagerException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(503, ex.getCode()); replay(exceptionMock); try { ResourceManagerException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(ResourceManagerException.UNKNOWN_CODE, ex.getCode());
@Override public Project get(String projectId, Map<Option, ?> options) { try { return resourceManager .projects() .get(projectId) .setFields(Option.FIELDS.getString(options)) .execute(); } catch (IOException ex) { ResourceManagerException translated = translate(ex); if (translated.getCode() == HTTP_FORBIDDEN || translated.getCode() == HTTP_NOT_FOUND) { // Service can return either 403 or 404 to signify that the project doesn't exist. return null; } else { throw translated; } } }
@Override public boolean shouldRetry(Throwable prevThrowable, Operation prevOp) { if (prevThrowable == null) { return prevOp.getDone() == null || !prevOp.getDone(); } return prevThrowable instanceof ResourceManagerException && ((ResourceManagerException) prevThrowable).isRetryable(); } };
@Override public Project get(String projectId, Map<Option, ?> options) { try { return resourceManager.projects() .get(projectId) .setFields(FIELDS.getString(options)) .execute(); } catch (IOException ex) { ResourceManagerException translated = translate(ex); if (translated.code() == HTTP_FORBIDDEN || translated.code() == HTTP_NOT_FOUND) { // Service can return either 403 or 404 to signify that the project doesn't exist. return null; } else { throw translated; } } }
@Test public void testInvalidListPaging() { Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>(); rpcOptions.put(ResourceManagerRpc.Option.PAGE_SIZE, -1); try { rpc.list(rpcOptions); } catch (ResourceManagerException e) { assertEquals("Page size must be greater than 0.", e.getMessage()); } }
@Test public void testUndeleteWhenActive() { rpc.create(COMPLETE_PROJECT); try { rpc.undelete(COMPLETE_PROJECT.getProjectId()); fail("Should fail because the project is not deleted."); } catch (ResourceManagerException e) { assertEquals(400, e.getCode()); assertTrue(e.getMessage().contains("lifecycle state was not DELETE_REQUESTED")); } }
private static ResourceManagerException translate(Status status) { Integer code = RPC_TO_HTTP_CODES.get(status.getCode()); if (code == null) { code = BaseHttpServiceException.UNKNOWN_CODE; } return new ResourceManagerException(code, status.getMessage()); }
@Override public Policy getPolicy(String projectId) throws ResourceManagerException { try { return resourceManager .projects() .getIamPolicy(projectId, new GetIamPolicyRequest()) .execute(); } catch (IOException ex) { ResourceManagerException translated = translate(ex); if (translated.getCode() == HTTP_FORBIDDEN) { // Service returns permission denied if policy doesn't exist. return null; } else { throw translated; } } }
@Override public Policy getPolicy(String projectId) throws ResourceManagerException { try { return resourceManager.projects() .getIamPolicy(projectId, new GetIamPolicyRequest()) .execute(); } catch (IOException ex) { ResourceManagerException translated = translate(ex); if (translated.code() == HTTP_FORBIDDEN) { // Service returns permission denied if policy doesn't exist. return null; } else { throw translated; } } }
@Test public void testTestPermissions() { List<String> permissions = ImmutableList.of("resourcemanager.projects.get"); try { rpc.testPermissions("nonexistent-project", permissions); fail("Nonexistent project."); } catch (ResourceManagerException e) { assertEquals(403, e.getCode()); assertEquals("Project nonexistent-project not found.", e.getMessage()); } rpc.create(PARTIAL_PROJECT); assertEquals( ImmutableList.of(true), rpc.testPermissions(PARTIAL_PROJECT.getProjectId(), permissions)); }
@Override public void delete(final String projectId) { try { runWithRetries( new Callable<Void>() { @Override public Void call() { resourceManagerRpc.delete(projectId); return null; } }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); } }
/** * Translate RetryHelperException to the ResourceManagerException that caused the error. This * method will always throw an exception. * * @throws ResourceManagerException when {@code ex} was caused by a {@code * ResourceManagerException} */ static ResourceManagerException translateAndThrow(RetryHelperException ex) { BaseServiceException.translate(ex); throw new ResourceManagerException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } }
@Test public void testTestPermissions() { List<String> permissions = ImmutableList.of("resourcemanager.projects.get"); try { RESOURCE_MANAGER.testPermissions("nonexistent-project", permissions); fail("Nonexistent project"); } catch (ResourceManagerException e) { assertEquals(403, e.getCode()); assertEquals("Project nonexistent-project not found.", e.getMessage()); } RESOURCE_MANAGER.create(PARTIAL_PROJECT); assertEquals( ImmutableList.of(true), RESOURCE_MANAGER.testPermissions(PARTIAL_PROJECT.getProjectId(), permissions)); }
@Override public void undelete(final String projectId) { try { runWithRetries( new Callable<Void>() { @Override public Void call() { resourceManagerRpc.undelete(projectId); return null; } }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); } }
@Test public void testRetryableException() { ResourceManagerRpcFactory rpcFactoryMock = EasyMock.createMock(ResourceManagerRpcFactory.class); ResourceManagerRpc resourceManagerRpcMock = EasyMock.createMock(ResourceManagerRpc.class); EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(ResourceManagerOptions.class))) .andReturn(resourceManagerRpcMock); EasyMock.replay(rpcFactoryMock); ResourceManager resourceManagerMock = ResourceManagerOptions.newBuilder() .setServiceRpcFactory(rpcFactoryMock) .build() .getService(); EasyMock.expect(resourceManagerRpcMock.get(PARTIAL_PROJECT.getProjectId(), EMPTY_RPC_OPTIONS)) .andThrow(new ResourceManagerException(500, "Internal Error")) .andReturn(PARTIAL_PROJECT.toPb()); EasyMock.replay(resourceManagerRpcMock); Project returnedProject = resourceManagerMock.get(PARTIAL_PROJECT.getProjectId()); assertEquals( new Project(resourceManagerMock, new ProjectInfo.BuilderImpl(PARTIAL_PROJECT)), returnedProject); }
@Test public void testDeleteWhenDeleteInProgress() { rpc.create(COMPLETE_PROJECT); RESOURCE_MANAGER_HELPER.changeLifecycleState( COMPLETE_PROJECT.getProjectId(), "DELETE_IN_PROGRESS"); try { rpc.delete(COMPLETE_PROJECT.getProjectId()); fail("Should fail because the project is not ACTIVE."); } catch (ResourceManagerException e) { assertEquals(400, e.getCode()); assertTrue(e.getMessage().contains("the lifecycle state was not ACTIVE")); } }