ServiceAccountKeyManager { public boolean keyExists(String keyName) throws IOException { try { iam.projects().serviceAccounts().keys() .get(keyName) .execute(); return true; } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == 403 || e.getStatusCode() == 404) { return false; } throw e; } } ServiceAccountKeyManager(Iam iam); ServiceAccountKey createJsonKey(String serviceAccount); ServiceAccountKey createP12Key(String serviceAccount); boolean serviceAccountExists(String serviceAccount); boolean keyExists(String keyName); void tryDeleteKey(String keyName); void deleteKey(String keyName);  }
@Test public void keyExistsTreatsPermissionDeniedAsNotFound() throws Exception { when(get.execute()).thenThrow(PERMISSION_DENIED); assertThat(sakm.keyExists("foo"), is(false)); verify(iam.projects().serviceAccounts().keys()).get("foo"); }
@Test public void keyExistsReturnsFalseForNotFound() throws Exception { when(get.execute()).thenThrow(NOT_FOUND); assertThat(sakm.keyExists("foo"), is(false)); verify(iam.projects().serviceAccounts().keys()).get("foo"); }
@Test(expected = GoogleJsonResponseException.class) public void keyExistsShouldThrowUnknownExceptions() throws Exception { when(get.execute()).thenThrow(INTERNAL_SERVER_ERROR); sakm.keyExists("foo"); verify(iam.projects().serviceAccounts().keys()).get("foo"); }