private void runWithMocks(Runnable toRun, Object... mocks) { EasyMock.replay(mocks); toRun.run(); EasyMock.verify(mocks); EasyMock.reset(mocks); }
private void givenPredicateWillReturnTrue() { EasyMock.reset(cliLinePredicate); EasyMock.expect(cliLinePredicate.test(anyString())).andReturn(true); EasyMock.replay(cliLinePredicate); } }
@Before public void setUp() { resourceFactory = (ResourceFactory<String, String>) EasyMock.createMock(ResourceFactory.class); EasyMock.replay(resourceFactory); pool = new ResourcePool<String, String>( resourceFactory, new ResourcePoolConfig(2, TimeUnit.MINUTES.toMillis(4)) ); EasyMock.verify(resourceFactory); EasyMock.reset(resourceFactory); }
private void setUpRequestExpectations(String producerId, String producerSequenceValue) { EasyMock.reset(req); EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED)) .andReturn(null) .anyTimes(); EasyMock.expect(req.getAttribute(AuthConfig.DRUID_ALLOW_UNSECURED_PATH)).andReturn(null).anyTimes(); EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)) .andReturn(AllowAllAuthenticator.ALLOW_ALL_RESULT) .anyTimes(); req.setAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED, true); EasyMock.expectLastCall().anyTimes(); EasyMock.expect(req.getContentType()).andReturn("application/json"); EasyMock.expect(req.getHeader("X-Firehose-Producer-Id")).andReturn(producerId); if (producerId != null) { EasyMock.expect(req.getHeader("X-Firehose-Producer-Seq")).andReturn(producerSequenceValue); } EasyMock.replay(req); } }
@Test public void shouldInvokeParserCorrectly() { // Given: reset(parser); expect(parser.parse("prop-1", "new-val")).andReturn("parsed-new-val"); replay(parser); // When: propsWithMockParser.set("prop-1", "new-val"); // Then: assertThat(propsWithMockParser.toMap().get("prop-1"), is("parsed-new-val")); verify(parser); }
@Test public void shouldFailIfIsNotAcceptingStatements() throws Exception { // Given: final String queryString = "SELECT * FROM test_stream;"; reset(mockKsqlEngine); expect(mockKsqlEngine.isAcceptingStatements()).andReturn(false); replay(mockKsqlEngine); // When: final Response response = testResource.streamQuery(new KsqlRequest(queryString, Collections.emptyMap(), null)); // Then: assertThat(response.getStatus(), equalTo(Status.INTERNAL_SERVER_ERROR.getStatusCode())); final KsqlErrorMessage errorMessage = (KsqlErrorMessage)response.getEntity(); assertThat(errorMessage.getErrorCode(), equalTo(Errors.ERROR_CODE_SERVER_ERROR)); assertThat(errorMessage.getMessage(), containsString("Cluster has been terminated.")); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowIfParserThrows() { // Given: reset(parser); expect(parser.parse("prop-1", "new-val")) .andThrow(new IllegalArgumentException("Boom")); replay(parser); // When: propsWithMockParser.set("prop-1", "new-val"); }
@Test public void testGoodQueryWithNullAcceptHeader() throws IOException { final String acceptHeader = null; final String contentTypeHeader = MediaType.APPLICATION_JSON; EasyMock.reset(testServletRequest); EasyMock.expect(testServletRequest.getAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED)) .andReturn(null) .anyTimes(); EasyMock.expect(testServletRequest.getAttribute(AuthConfig.DRUID_ALLOW_UNSECURED_PATH)).andReturn(null).anyTimes(); EasyMock.expect(testServletRequest.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)) .andReturn(authenticationResult) .anyTimes(); testServletRequest.setAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED, true); EasyMock.expect(testServletRequest.getHeader("Accept")).andReturn(acceptHeader).anyTimes(); EasyMock.expect(testServletRequest.getContentType()).andReturn(contentTypeHeader).anyTimes(); EasyMock.expect(testServletRequest.getHeader(QueryResource.HEADER_IF_NONE_MATCH)).andReturn(null).anyTimes(); EasyMock.expect(testServletRequest.getRemoteAddr()).andReturn("localhost").anyTimes(); EasyMock.replay(testServletRequest); Response response = queryResource.doPost( new ByteArrayInputStream(simpleTimeSeriesQuery.getBytes("UTF-8")), null /*pretty*/, testServletRequest ); Assert.assertEquals(HttpStatus.SC_OK, response.getStatus()); //since accept header is null, the response content type should be same as the value of 'Content-Type' header Assert.assertEquals(contentTypeHeader, (response.getMetadata().get("Content-Type").get(0)).toString()); Assert.assertNotNull(response); }
@Test public void testGoodQueryWithEmptyAcceptHeader() throws IOException { final String acceptHeader = ""; final String contentTypeHeader = MediaType.APPLICATION_JSON; EasyMock.reset(testServletRequest); EasyMock.expect(testServletRequest.getAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED)) .andReturn(null) .anyTimes(); EasyMock.expect(testServletRequest.getAttribute(AuthConfig.DRUID_ALLOW_UNSECURED_PATH)).andReturn(null).anyTimes(); EasyMock.expect(testServletRequest.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)) .andReturn(authenticationResult) .anyTimes(); testServletRequest.setAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED, true); EasyMock.expect(testServletRequest.getHeader("Accept")).andReturn(acceptHeader).anyTimes(); EasyMock.expect(testServletRequest.getContentType()).andReturn(contentTypeHeader).anyTimes(); EasyMock.expect(testServletRequest.getHeader(QueryResource.HEADER_IF_NONE_MATCH)).andReturn(null).anyTimes(); EasyMock.expect(testServletRequest.getRemoteAddr()).andReturn("localhost").anyTimes(); EasyMock.replay(testServletRequest); Response response = queryResource.doPost( new ByteArrayInputStream(simpleTimeSeriesQuery.getBytes("UTF-8")), null /*pretty*/, testServletRequest ); Assert.assertEquals(HttpStatus.SC_OK, response.getStatus()); //since accept header is empty, the response content type should be same as the value of 'Content-Type' header Assert.assertEquals(contentTypeHeader, (response.getMetadata().get("Content-Type").get(0)).toString()); Assert.assertNotNull(response); }
@Test public void testLookupDiscoverAll() { final Set<String> fakeChildren = ImmutableSet.of("tier1", "tier2"); EasyMock.reset(lookupNodeDiscovery); EasyMock.expect(lookupNodeDiscovery.getAllTiers()) .andReturn(fakeChildren) .once(); EasyMock.replay(lookupNodeDiscovery); final LookupCoordinatorManager manager = new LookupCoordinatorManager( druidNodeDiscoveryProvider, configManager, lookupCoordinatorManagerConfig, EasyMock.createMock(LookupCoordinatorManager.LookupsCommunicator.class), lookupNodeDiscovery ); manager.start(); Assert.assertEquals(fakeChildren, manager.discoverTiers()); EasyMock.verify(lookupNodeDiscovery); }
private void replayOnSubscribe() { subscription.request(1); EasyMock.expectLastCall().once(); EasyMock.replay(subscription); subscriber.onSubscribe(subscription); EasyMock.verify(subscription); EasyMock.reset(subscription); }
@Override @Before public void setUp() throws Exception { super.setUp(); EasyMock.reset(lookupReferencesManager); LookupExtractorFactory lookupExtractorFactory1 = new MapLookupExtractorFactory(ImmutableMap.of( "key", "value", "key2", "value2" ), false); EasyMock.expect(lookupReferencesManager.get("lookupId1")).andReturn( new LookupExtractorFactoryContainer( "v0", lookupExtractorFactory1 ) ).anyTimes(); EasyMock.replay(lookupReferencesManager); }
@Test public void testDiscoverNodesInTier() { EasyMock.reset(lookupNodeDiscovery); EasyMock.expect(lookupNodeDiscovery.getNodesInTier("tier")) .andReturn( ImmutableSet.of( HostAndPortWithScheme.fromParts("http", "h1", 8080), HostAndPortWithScheme.fromParts("http", "h2", 8080) ) ) .once(); EasyMock.replay(lookupNodeDiscovery); final LookupCoordinatorManager manager = new LookupCoordinatorManager( druidNodeDiscoveryProvider, configManager, lookupCoordinatorManagerConfig, EasyMock.createMock(LookupCoordinatorManager.LookupsCommunicator.class), lookupNodeDiscovery ); manager.start(); Assert.assertEquals( ImmutableSet.of( HostAndPort.fromParts("h1", 8080), HostAndPort.fromParts("h2", 8080) ), ImmutableSet.copyOf(manager.discoverNodesInTier("tier"))); EasyMock.verify(lookupNodeDiscovery); }
@Before public void setUp() throws IOException { SERVICE_EMITTER.flush(); EVENT_EMITS.set(0L); EasyMock.reset(lookupNodeDiscovery); EasyMock.reset(configManager); EasyMock.expect( configManager.watch( EasyMock.eq(LookupCoordinatorManager.LOOKUP_CONFIG_KEY), EasyMock.<TypeReference>anyObject(), EasyMock.<AtomicReference>isNull() ) ).andReturn( new AtomicReference<>(null) ).anyTimes(); EasyMock.expect( configManager.watch( EasyMock.eq(LookupCoordinatorManager.OLD_LOOKUP_CONFIG_KEY), EasyMock.<TypeReference>anyObject(), EasyMock.<AtomicReference>isNull() ) ).andReturn( new AtomicReference<>(null) ).anyTimes(); EasyMock.replay(configManager); }
@Test public void testFailedResource() { primePool(); EasyMock.expect(resourceFactory.isGood("billy1")).andReturn(false).times(1); resourceFactory.close("billy1"); EasyMock.expectLastCall(); EasyMock.expect(resourceFactory.generate("billy")).andReturn("billy2").times(1); EasyMock.replay(resourceFactory); ResourceContainer<String> billy = pool.take("billy"); Assert.assertEquals("billy2", billy.get()); billy.returnResource(); EasyMock.verify(resourceFactory); EasyMock.reset(resourceFactory); }
@Test public void testTimedOutResource() throws Exception { resourceFactory = (ResourceFactory<String, String>) EasyMock.createMock(ResourceFactory.class); pool = new ResourcePool<String, String>( resourceFactory, new ResourcePoolConfig(2, TimeUnit.MILLISECONDS.toMillis(10)) ); EasyMock.expect(resourceFactory.generate("billy")).andAnswer(new StringIncrementingAnswer("billy")).times(2); EasyMock.expect(resourceFactory.isGood("billy0")).andReturn(true).times(1); EasyMock.replay(resourceFactory); ResourceContainer<String> billyString = pool.take("billy"); Assert.assertEquals("billy0", billyString.get()); EasyMock.verify(resourceFactory); EasyMock.reset(resourceFactory); billyString.returnResource(); //make sure resources have been timed out. Thread.sleep(100); EasyMock.expect(resourceFactory.generate("billy")).andReturn("billy1").times(1); resourceFactory.close("billy1"); EasyMock.expect(resourceFactory.isGood("billy1")).andReturn(true).times(1); EasyMock.replay(resourceFactory); ResourceContainer<String> billy = pool.take("billy"); Assert.assertEquals("billy1", billy.get()); billy.returnResource(); EasyMock.verify(resourceFactory); EasyMock.reset(resourceFactory); }
private void primePool() { EasyMock.expect(resourceFactory.generate("billy")).andAnswer(new StringIncrementingAnswer("billy")).times(2); EasyMock.expect(resourceFactory.generate("sally")).andAnswer(new StringIncrementingAnswer("sally")).times(2); EasyMock.expect(resourceFactory.isGood("billy0")).andReturn(true).times(1); EasyMock.expect(resourceFactory.isGood("sally0")).andReturn(true).times(1); EasyMock.replay(resourceFactory); ResourceContainer<String> billyString = pool.take("billy"); ResourceContainer<String> sallyString = pool.take("sally"); Assert.assertEquals("billy0", billyString.get()); Assert.assertEquals("sally0", sallyString.get()); EasyMock.verify(resourceFactory); EasyMock.reset(resourceFactory); billyString.returnResource(); sallyString.returnResource(); }
@Test public void testUpdateLookupAdds() { final LookupCoordinatorManager manager = new LookupCoordinatorManager( client, druidNodeDiscoveryProvider, mapper, configManager, lookupCoordinatorManagerConfig ) { @Override public Map<String, Map<String, LookupExtractorFactoryMapContainer>> getKnownLookups() { return EMPTY_TIERED_LOOKUP; } }; manager.start(); final AuditInfo auditInfo = new AuditInfo("author", "comment", "localhost"); EasyMock.reset(configManager); EasyMock.expect(configManager.set( EasyMock.eq(LookupCoordinatorManager.LOOKUP_CONFIG_KEY), EasyMock.eq(TIERED_LOOKUP_MAP_V0), EasyMock.eq(auditInfo) )).andReturn(SetResult.ok()).once(); EasyMock.replay(configManager); manager.updateLookup(LOOKUP_TIER, SINGLE_LOOKUP_NAME, SINGLE_LOOKUP_SPEC_V0, auditInfo); EasyMock.verify(configManager); }
@Test public void testUpdateLookupsUpdates() { final LookupCoordinatorManager manager = new LookupCoordinatorManager( client, druidNodeDiscoveryProvider, mapper, configManager, lookupCoordinatorManagerConfig ) { @Override public Map<String, Map<String, LookupExtractorFactoryMapContainer>> getKnownLookups() { return TIERED_LOOKUP_MAP_V0; } }; manager.start(); final AuditInfo auditInfo = new AuditInfo("author", "comment", "localhost"); EasyMock.reset(configManager); EasyMock.expect(configManager.set( EasyMock.eq(LookupCoordinatorManager.LOOKUP_CONFIG_KEY), EasyMock.eq(TIERED_LOOKUP_MAP_V1), EasyMock.eq(auditInfo) )).andReturn(SetResult.ok()).once(); EasyMock.replay(configManager); manager.updateLookups(TIERED_LOOKUP_MAP_V1, auditInfo); EasyMock.verify(configManager); }
@Test public void testDeleteZoneOnSuccess() { EasyMock.reset(batchMock); Capture<RpcBatch.Callback<Void>> callback = Capture.newInstance(); batchMock.addDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock.capture(callback)); EasyMock.replay(batchMock); DnsBatchResult<Boolean> batchResult = dnsBatch.deleteZone(ZONE_NAME); assertNotNull(callback.getValue()); RpcBatch.Callback<Void> capturedCallback = callback.getValue(); Void result = null; capturedCallback.onSuccess(result); assertTrue(batchResult.get()); }