@Test public void testGetUnknownAccount() throws Exception { when(mockUserService.getUserWithAuthorities()).thenReturn(null); restUserMockMvc.perform(get("/api/account") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isInternalServerError()); }
@Test public void whenUpdateRetentionTimeAndDbFails() throws Exception { final EventType eventType = EventTypeTestBuilder.builder().build(); final EventTypeOptions eventTypeOptions = new EventTypeOptions(); eventTypeOptions.setRetentionTime(172800000L); eventType.setOptions(eventTypeOptions); doReturn(eventType).when(eventTypeRepository).findByName(eventType.getName()); doThrow(InternalNakadiException.class).when(eventTypeRepository).update(any()); when(timelineService.getActiveTimelinesOrdered(any())) .thenReturn(Collections.singletonList( Timeline.createTimeline(eventType.getName(), 0, null, "topic", new Date()))); final EventType eventType2 = EventTypeTestBuilder.builder().name(eventType.getName()).build(); final EventTypeOptions eventTypeOptions2 = new EventTypeOptions(); eventTypeOptions2.setRetentionTime(172800001L); eventType2.setOptions(eventTypeOptions2); putEventType(eventType2, eventType2.getName(), "nakadi") .andExpect(status().isInternalServerError()); verify(topicRepository, times(2)).setRetentionTime(anyString(), anyLong()); verify(eventTypeRepository).update(any()); }
@Test public void whenUpdateRetentionTimeAndKafkaFails() throws Exception { final EventType eventType = EventTypeTestBuilder.builder().build(); final EventTypeOptions eventTypeOptions = new EventTypeOptions(); eventTypeOptions.setRetentionTime(172800000L); eventType.setOptions(eventTypeOptions); doReturn(eventType).when(eventTypeRepository).findByName(eventType.getName()); doThrow(TopicConfigException.class).when(topicRepository).setRetentionTime(anyString(), anyLong()); when(timelineService.getActiveTimelinesOrdered(any())) .thenReturn(Collections.singletonList( Timeline.createTimeline(eventType.getName(), 0, null, "topic", new Date()))); final EventType eventType2 = EventTypeTestBuilder.builder().name(eventType.getName()).build(); final EventTypeOptions eventTypeOptions2 = new EventTypeOptions(); eventTypeOptions2.setRetentionTime(172800001L); eventType2.setOptions(eventTypeOptions2); putEventType(eventType2, eventType2.getName(), "nakadi") .andExpect(status().isInternalServerError()); verify(topicRepository, times(2)).setRetentionTime(anyString(), anyLong()); verify(eventTypeRepository, times(0)).update(any()); }
@Test public void shouldHandleExternalExceptions() throws Exception { Mockito.when(chatClient.getMessagesAfter(null)).thenThrow(new RuntimeException("Wrong cursor")); mockMvc.perform(get("/api/v1/messages") .accept(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().isInternalServerError()) .andExpect(jsonPath("$.message").value("Wrong cursor")); } }
@Test public void wheGenericExceptionIsThrown_returns500() throws Exception { when(credentialsHandler.getNCredentialVersions(eq("/foo"), any())).thenThrow(new RuntimeException()); final MockHttpServletRequestBuilder request = get("/api/v1/data?name=foo") .header("Authorization", "Bearer " + AuthConstants.ALL_PERMISSIONS_TOKEN) .accept(APPLICATION_JSON); final String expectedError = "An application error occurred. Please contact your CredHub administrator."; mockMvc.perform(request) .andExpect(status().isInternalServerError()) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .andExpect(jsonPath("$.error").value(expectedError)); } }
@Test public void whenPersistencyErrorThen500() throws Exception { final Problem expectedProblem = Problem.valueOf(INTERNAL_SERVER_ERROR); doThrow(InternalNakadiException.class).when(eventTypeRepository).saveEventType(any(EventType.class)); postEventType(buildDefaultEventType()).andExpect(status().isInternalServerError()) .andExpect(content().contentType("application/problem+json")).andExpect( content().string(matchesProblem(expectedProblem))); }
@Test public void whenDeleteEventTypeAndNakadiExceptionThen500() throws Exception { final String eventTypeName = randomValidEventTypeName(); final Problem expectedProblem = Problem.valueOf(INTERNAL_SERVER_ERROR, "Failed to delete event type " + eventTypeName); doThrow(new InternalNakadiException("dummy message")) .when(eventTypeRepository).removeEventType(eventTypeName); doReturn(Optional.of(EventTypeTestBuilder.builder().name(eventTypeName).build())) .when(eventTypeRepository).findByNameO(eventTypeName); deleteEventType(eventTypeName).andExpect(status().isInternalServerError()) .andExpect(content().contentType("application/problem+json")).andExpect(content() .string(matchesProblem(expectedProblem))); }
@Test public void should_send_internal_error_on_remote_api_error() throws Exception { //GIVEN that we have a map route in database (for started app) when(proxyMap.findOne(HOST_TEST_VALUE)).thenReturn(ProxyMapEntry.builder() .appId(APP_ID) .host(HOST_TEST_VALUE) .build()); when(cfApi.getApplicationState(APP_ID)).thenThrow(CloudFoundryException.class); this.mockMvc .perform(get("http://localhost/anything") .header(HEADER_HOST, HOST_TEST_VALUE) .header(HEADER_PROTOCOL, PROTOCOL_TEST_VALUE) .header(HEADER_FORWARDED, proxy.proxySignature)) .andExpect(status().isInternalServerError()); }
@Test public void gettingACredential_thatIsEncryptedWithAnUnknownKey_throwsAnException() throws Exception { final UUID uuid = UUID.randomUUID(); final ValueCredentialVersion valueCredential = new ValueCredentialVersion(CREDENTIAL_NAME); valueCredential.setEncryptor(encryptor); valueCredential.setUuid(uuid); valueCredential.setVersionCreatedAt(FROZEN_TIME); valueCredential.getCredential().setUuid(UUID.randomUUID()); doThrow(new KeyNotFoundException("error.missing_encryption_key")) .when(encryptor).decrypt(any()); doReturn(Collections.singletonList(valueCredential)).when(credentialVersionDataService) .findAllByName(CREDENTIAL_NAME); final MockHttpServletRequestBuilder get = get("/api/v1/data?name=" + CREDENTIAL_NAME) .header("Authorization", "Bearer " + AuthConstants.ALL_PERMISSIONS_TOKEN) .accept(APPLICATION_JSON); final String expectedError = "The credential could not be accessed with the provided encryption keys. You must update your deployment configuration to continue."; mockMvc.perform(get) .andExpect(status().isInternalServerError()) .andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .andExpect(jsonPath("$.error").value(expectedError)); }
@Test public void testSubscribeEmail_Database_Problem() throws Exception { when(mockWhitelistEmailService.saveWhiteListEmail(TEST_EMAIL)).thenThrow(new WhitelistEmailNotSavedException()); WhitelistEmailRequest request = new WhitelistEmailRequest(TEST_EMAIL); this.mockMvc.perform(post(WHITELIST_SUBSCRIBE_ENDPOINT) .contentType(APPLICATION_JSON) .content(jsonWhitelistEmailRequest.write(request).getJson())) .andExpect(status().isInternalServerError()) .andDo(print()); }
@Test public void should_send_internal_error_if_already_signed_by_proxy() throws Exception { //GIVEN //WHEN an incoming message contains the signature header with our own signature //THEN return 500 error (as this should not happen) this.mockMvc .perform(get("http://localhost/anything") .header(HEADER_HOST, HOST_TEST_VALUE) .header(HEADER_PROTOCOL, PROTOCOL_TEST_VALUE) .header(HEADER_FORWARDED, proxy.proxySignature)) .andExpect(status().isInternalServerError()); }
@Test public void deleteBindingFails() throws Exception { mockMvc.perform(delete(buildDeleteUrl())) .andExpect(request().asyncNotStarted()) .andExpect(status().isInternalServerError()) .andReturn(); }
break; case INTERNAL_SERVER_ERROR: statusResultMatcher = status().isInternalServerError(); break; default:
break; case INTERNAL_SERVER_ERROR: statusResultMatcher = status().isInternalServerError(); break; default:
break; case INTERNAL_SERVER_ERROR: statusResultMatcher = status().isInternalServerError(); break; default:
@Test public void createServiceInstanceWithExceptionFails() throws Exception { setupCatalogService(); setupServiceInstanceService(new NullPointerException("unknown error")); MvcResult mvcResult = mockMvc.perform(put(buildCreateUpdateUrl()) .content(createRequestBody) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()) .andReturn(); mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isInternalServerError()) .andExpect(jsonPath("$.description", endsWith("unknown error"))); }
@Test public void createBindingToAppFails() throws Exception { setupCatalogService(); MvcResult mvcResult = mockMvc.perform(put(buildCreateUrl()) .content(createRequestBody) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()) .andReturn(); mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isInternalServerError()); }
@Test public void regeneratingAPasswordWithParametersThatCannotBeDecrypted_returnsAnError() throws Exception { final EncryptionKeyCanary encryptionKeyCanary = new EncryptionKeyCanary(); canaryDataService.save(encryptionKeyCanary); final PasswordCredentialVersionData passwordCredentialData = new PasswordCredentialVersionData( "/my-password"); final PasswordCredentialVersion originalCredential = new PasswordCredentialVersion(passwordCredentialData); originalCredential.setEncryptor(encryptor); originalCredential .setPasswordAndGenerationParameters("abcde", new StringGenerationParameters()); passwordCredentialData.getEncryptedValueData().setEncryptionKeyUuid(encryptionKeyCanary.getUuid()); credentialVersionDataService.save(originalCredential); // language=JSON final String cannotRegenerate = "{\n" + " \"error\": \"The credential could not be accessed with the provided encryption keys. You must update your deployment configuration to continue" + ".\"\n" + "}"; final MockHttpServletRequestBuilder request = post("/api/v1/data") .header("Authorization", "Bearer " + ALL_PERMISSIONS_TOKEN) .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content("{\"regenerate\":true,\"name\":\"my-password\"}"); mockMvc.perform(request) .andDo(print()) .andExpect(status().isInternalServerError()) .andExpect(content().json(cannotRegenerate)); }