@Test(expected = Error.class) public void testWhenComputableThrowsError() throws Exception { final Integer input = 1; final Memoizer<Integer, Integer> memoizer = new Memoizer<>(computable); final Error error = new Error(); expect(computable.compute(input)).andThrow(error); replay(computable); memoizer.compute(input); } }
private Producer mockProducerSendThrowsException() { Producer mockProducer = EasyMock.createMock(Producer.class); mockProducer.send((KeyedMessage) EasyMock.anyObject()); EasyMock.expectLastCall().andThrow(new RuntimeException("dummyException")).anyTimes(); mockProducer.close(); EasyMock.expectLastCall().anyTimes(); EasyMock.replay(mockProducer); return mockProducer; }
private SimpleConsumer mockConsumerThrowsExceptionForOffsetRangeCall(TopicMetadataResponse metadataResponse) { SimpleConsumer simpleConsumer = EasyMock.createMock(SimpleConsumer.class); mocks.add(simpleConsumer); EasyMock.expect(simpleConsumer.send((TopicMetadataRequest)EasyMock.anyObject())) .andReturn(metadataResponse).times(1); EasyMock.expect(simpleConsumer.getOffsetsBefore((OffsetRequest) EasyMock.anyObject())) .andThrow(new RuntimeException()).times(3); EasyMock.expect(simpleConsumer.clientId()).andReturn(KAFKA_CLIENT_ID).times(1); simpleConsumer.close(); EasyMock.expectLastCall().andVoid().times(2); EasyMock.expect(simpleConsumer.host()).andReturn("dummyHost").times(4); EasyMock.expect(simpleConsumer.port()).andReturn(8888).times(4); return simpleConsumer; }
@Test(expected = RuntimeException.class) public void testWhenComputableThrowsRuntimeException() throws Exception { final Integer input = 1; final Memoizer<Integer, Integer> memoizer = new Memoizer<>(computable); final RuntimeException runtimeException = new RuntimeException("Some runtime exception"); expect(computable.compute(input)).andThrow(runtimeException); replay(computable); memoizer.compute(input); }
private Producer mockProducerThirdSendSucceed() { Producer mockProducer = EasyMock.createMock(Producer.class); mockProducer.send((KeyedMessage) EasyMock.anyObject()); EasyMock.expectLastCall().andThrow(new RuntimeException("dummyException")).times(2); mockProducer.send((KeyedMessage) EasyMock.anyObject()); EasyMock.expectLastCall().times(1); mockProducer.close(); EasyMock.expectLastCall().anyTimes(); EasyMock.replay(mockProducer); return mockProducer; }
@Test public void testBadInnerInput() throws Exception { final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{}); final ObjectMapper mapper = EasyMock.createStrictMock(ObjectMapper.class); EasyMock.expect(mapper.readValue(EasyMock.<InputStream>anyObject(), EasyMock.<TypeReference<Object>>anyObject())) .andThrow(new IOException()); EasyMock.replay(mapper); final Response response = abstractListenerHandler.handlePOSTAll(bais, mapper); Assert.assertEquals(400, response.getStatus()); EasyMock.verify(mapper); }
@Test public void testServiceAnnouncementGETAllExceptionInHandler() { final ListenerHandler handler = EasyMock.createStrictMock(ListenerHandler.class); EasyMock.expect(handler.handleGETAll()).andThrow(new RuntimeException("test")); final ListenerResource resource = new ListenerResource( mapper, mapper, handler ) { }; EasyMock.replay(handler); Assert.assertEquals( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resource.getAll().getStatus() ); EasyMock.verify(handler); }
@Test public void shouldPrintErrorWhenCantConnectToNewAddress() { expect(restClient.makeRootRequest()).andThrow( new KsqlRestClientException("Failed to connect", new ProcessingException("Boom"))); replay(restClient); command.execute(ImmutableList.of(VALID_SERVER_ADDRESS), terminal); assertThat(out.toString(), containsString("Boom")); assertThat(out.toString(), containsString("Failed to connect")); }
@Test public void shouldPrintServerAddressWhenEmptyStringArg() throws Exception { expect(restClient.makeRootRequest()).andReturn(RestResponse.successful(SERVER_INFO)); expect(restClient.getServerAddress()).andReturn(new URI(INITIAL_SERVER_ADDRESS)); restClient.setServerAddress(anyString()); expectLastCall().andThrow(new AssertionError("should not set address")); replay(restClient); command.execute(ImmutableList.of(), terminal); assertThat(out.toString(), equalTo(INITIAL_SERVER_ADDRESS + "\n")); }
@Test public void testServiceAnnouncementGETExceptionInHandler() { final ListenerHandler handler = EasyMock.createStrictMock(ListenerHandler.class); EasyMock.expect(handler.handleGET(EasyMock.anyString())).andThrow(new RuntimeException("test")); final ListenerResource resource = new ListenerResource( mapper, mapper, handler ) { }; EasyMock.replay(handler); Assert.assertEquals( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resource.serviceAnnouncementGET("id").getStatus() ); EasyMock.verify(handler); }
@Test public void testServiceAnnouncementDELETEExceptionInHandler() { final ListenerHandler handler = EasyMock.createStrictMock(ListenerHandler.class); EasyMock.expect(handler.handleDELETE(EasyMock.anyString())).andThrow(new RuntimeException("test")); final ListenerResource resource = new ListenerResource( mapper, mapper, handler ) { }; EasyMock.replay(handler); Assert.assertEquals( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resource.serviceAnnouncementDELETE("id").getStatus() ); EasyMock.verify(handler); }
@Test(expected = IllegalStateException.class) public void testDefaultBehaviourNotToRecalculateExecutionExceptions() throws Exception { final Integer input = 1; final Memoizer<Integer, Integer> memoizer = new Memoizer<>(computable); final InterruptedException interruptedException = new InterruptedException(); expect(computable.compute(input)).andThrow(interruptedException); replay(computable); try { memoizer.compute(input); fail("Expected Throwable to be thrown!"); } catch (final Throwable expected) { // Should always be thrown the first time } memoizer.compute(input); }
@Test(expected = IllegalStateException.class) public void testDoesNotRecalculateWhenSetToFalse() throws Exception { final Integer input = 1; final Memoizer<Integer, Integer> memoizer = new Memoizer<>(computable, false); final InterruptedException interruptedException = new InterruptedException(); expect(computable.compute(input)).andThrow(interruptedException); replay(computable); try { memoizer.compute(input); fail("Expected Throwable to be thrown!"); } catch (final Throwable expected) { // Should always be thrown the first time } memoizer.compute(input); }
@Test public void shouldNotFailIfTopicExistsOnCreation() { topicClient.createTopic(COMMAND_TOPIC, 1, (short) 1, commandTopicConfig); EasyMock.expectLastCall().andThrow(new KafkaTopicException("blah")); EasyMock.replay(topicClient); KsqlRestApplication.ensureCommandTopic(restConfig, topicClient, COMMAND_TOPIC); EasyMock.verify(topicClient); }
@Test public void testExceptionalGet() { final String errMsg = "some error"; final LookupCoordinatorManager lookupCoordinatorManager = EasyMock.createStrictMock( LookupCoordinatorManager.class); EasyMock.expect(lookupCoordinatorManager.getKnownLookups()).andThrow(new RuntimeException(errMsg)).once(); EasyMock.replay(lookupCoordinatorManager); final LookupCoordinatorResource lookupCoordinatorResource = new LookupCoordinatorResource( lookupCoordinatorManager, mapper, mapper ); final Response response = lookupCoordinatorResource.getTiers(false); Assert.assertEquals(500, response.getStatus()); Assert.assertEquals(ImmutableMap.of("error", errMsg), response.getEntity()); EasyMock.verify(lookupCoordinatorManager); }
@Test public void testServiceAnnouncementPOSTAllExceptionInHandler() { final ListenerHandler handler = EasyMock.createStrictMock(ListenerHandler.class); EasyMock.expect(handler.handlePOSTAll(EasyMock.anyObject(), EasyMock.anyObject())) .andThrow(new RuntimeException("test")); final ListenerResource resource = new ListenerResource( mapper, mapper, handler ) { }; EasyMock.replay(handler); Assert.assertEquals( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resource.serviceAnnouncementPOSTAll(EMPTY_JSON_MAP.get(), req).getStatus() ); EasyMock.verify(req, handler); }
@Test public void testDoesRecalculateWhenSetToTrue() throws Exception { final Integer input = 1; final Integer answer = 3; final Memoizer<Integer, Integer> memoizer = new Memoizer<>(computable, true); final InterruptedException interruptedException = new InterruptedException(); expect(computable.compute(input)).andThrow(interruptedException).andReturn(answer); replay(computable); try { memoizer.compute(input); fail("Expected Throwable to be thrown!"); } catch (final Throwable expected) { // Should always be thrown the first time } assertEquals(answer, memoizer.compute(input)); }