protected DatastoreBatchWriter() { super("test"); datastore = EasyMock.createMock(Datastore.class); IncompleteKey[] expected = {INCOMPLETE_KEY, INCOMPLETE_KEY}; List<Key> result = ImmutableList.of(KEY2, KEY3); expect(datastore.allocateId(expected)).andReturn(result).times(0, 1); replay(datastore); }
@Test public void testChunking() { Query query = queryBuilder.intervals("2015-01-01T00:00:00.000/2015-01-11T00:00:00.000").context(ImmutableMap.of("chunkPeriod", "P1D")).build(); executors.execute(EasyMock.anyObject(Runnable.class)); EasyMock.expectLastCall().times(10); EasyMock.replay(executors); EasyMock.replay(toolChest); QueryRunner runner = decorator.decorate(baseRunner, toolChest); runner.run(QueryPlus.wrap(query), Collections.EMPTY_MAP); EasyMock.verify(executors); }
@Test public void cleanup() { PooledTopNAlgorithm pooledTopNAlgorithm = new PooledTopNAlgorithm(EasyMock.mock(StorageAdapter.class), null, null); PooledTopNAlgorithm.PooledTopNParams params = EasyMock.createMock(PooledTopNAlgorithm.PooledTopNParams.class); ResourceHolder<ByteBuffer> resourceHolder = EasyMock.createMock(ResourceHolder.class); EasyMock.expect(params.getResultsBufHolder()).andReturn(resourceHolder).times(1); EasyMock.expect(resourceHolder.get()).andReturn(ByteBuffer.allocate(1)).times(1); resourceHolder.close(); EasyMock.expectLastCall().once(); EasyMock.replay(params); EasyMock.replay(resourceHolder); pooledTopNAlgorithm.cleanup(params); EasyMock.verify(params); EasyMock.verify(resourceHolder); } }
@Test public void testChunkingOnMonths() { Query query = queryBuilder.intervals("2015-01-01T00:00:00.000/2015-02-11T00:00:00.000").context(ImmutableMap.of("chunkPeriod", "P1M")).build(); executors.execute(EasyMock.anyObject(Runnable.class)); EasyMock.expectLastCall().times(2); EasyMock.replay(executors); EasyMock.replay(toolChest); QueryRunner runner = decorator.decorate(baseRunner, toolChest); runner.run(QueryPlus.wrap(query), Collections.EMPTY_MAP); EasyMock.verify(executors); } }
@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 testComparatorsWithFinalizing() EasyMock.expect(aggFactory.finalizeComputation("test_val1")) .andReturn(new Long(10L)) .times(1); EasyMock.expect(aggFactory.finalizeComputation("test_val2")) .andReturn(new Long(21)) .times(1); EasyMock.expect(aggFactory.finalizeComputation("test_val3")) .andReturn(new Long(3)) .times(1); EasyMock.expect(aggFactory.finalizeComputation("test_val4")) .andReturn(null) .times(1); EasyMock.expect(aggFactory.getComparator()) .andReturn(Ordering.natural().<Long>nullsLast()) .times(1); EasyMock.replay(aggFactory);
@Test public void testComparatorsWithFinalizingAndComparatorNull() { String aggName = "billy"; AggregatorFactory aggFactory = EasyMock.createMock(AggregatorFactory.class); EasyMock.expect(aggFactory.getComparator()) .andReturn(null) .times(1); EasyMock.replay(aggFactory); FinalizingFieldAccessPostAggregator postAgg = buildDecorated( "final_billy", "joe", ImmutableMap.of(aggName, aggFactory)); List<Object> computedValues = new ArrayList<>(); Map<String, Object> forNull = new HashMap<>(); forNull.put("joe", null); // guava does not allow the value to be null. computedValues.add(postAgg.compute(ImmutableMap.of("joe", "test_val1"))); computedValues.add(postAgg.compute(ImmutableMap.of("joe", "test_val2"))); computedValues.add(postAgg.compute(forNull)); computedValues.add(postAgg.compute(ImmutableMap.of("joe", "test_val4"))); Collections.sort(computedValues, postAgg.getComparator()); Assert.assertArrayEquals( new Object[]{null, "test_val1", "test_val2", "test_val4"}, computedValues.toArray(new Object[0]) ); EasyMock.verify(); }
@Test public void testHandle() throws IOException { final HttpResponse response = mock(HttpResponse.class); final StatusLine statusLine = mock(StatusLine.class); final HttpEntity entity = mock(HttpEntity.class); final Logger log = mock(Logger.class); final Header header = mock(Header.class); expect(response.getStatusLine()).andReturn(statusLine).once(); expect(statusLine.getStatusCode()).andReturn(HttpStatus.SC_OK).once(); expect(response.getEntity()).andReturn(entity).times(2); final ByteArrayInputStream bais = new ByteArrayInputStream("yolo".getBytes(StandardCharsets.UTF_8)); expect(entity.getContent()).andReturn(bais).times(2); expect(entity.getContentType()).andReturn(header).times(1); expect(header.getElements()).andReturn(new HeaderElement[]{}); expect(entity.getContentLength()).andReturn(4L).times(2); log.warn("yolo"); expectLastCall().once(); replay(response, statusLine, entity, header, log); final KsqlVersionCheckerResponseHandler kvcr = new KsqlVersionCheckerResponseHandler(log); kvcr.handle(response); verify(response, statusLine, entity, header, log); } }
@Test public void testFullGetSpecificIntervals() { EasyMock.expect(inventoryView.getInventory()).andReturn( ImmutableList.of(server) ).atLeastOnce(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_ALLOW_UNSECURED_PATH)).andReturn(null).once(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED)).andReturn(null).once(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)).andReturn( new AuthenticationResult("druid", "druid", null, null) ).once(); request.setAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED, true); EasyMock.expectLastCall().times(1); EasyMock.replay(inventoryView, request); List<Interval> expectedIntervals = new ArrayList<>(); expectedIntervals.add(Intervals.of("2010-01-01T00:00:00.000Z/2010-01-02T00:00:00.000Z")); IntervalsResource intervalsResource = new IntervalsResource( inventoryView, new AuthConfig(), AuthTestUtils.TEST_AUTHORIZER_MAPPER ); Response response = intervalsResource.getSpecificIntervals("2010-01-01T00:00:00.000Z/P1D", null, "full", request); TreeMap<Interval, Map<String, Map<String, Object>>> actualIntervals = (TreeMap) response.getEntity(); Assert.assertEquals(1, actualIntervals.size()); Assert.assertEquals(expectedIntervals.get(0), actualIntervals.firstKey()); Assert.assertEquals(20L, actualIntervals.get(expectedIntervals.get(0)).get("datasource1").get("size")); Assert.assertEquals(1, actualIntervals.get(expectedIntervals.get(0)).get("datasource1").get("count")); Assert.assertEquals(5L, actualIntervals.get(expectedIntervals.get(0)).get("datasource2").get("size")); Assert.assertEquals(1, actualIntervals.get(expectedIntervals.get(0)).get("datasource2").get("count")); }
@Test public void testNullWorkerConfig() { workerConfig.set(null); EasyMock.replay(autoScaler); RemoteTaskRunner runner = EasyMock.createMock(RemoteTaskRunner.class); EasyMock.expect(runner.getPendingTaskPayloads()).andReturn( Collections.singletonList( NoopTask.create() ) ).times(1); EasyMock.expect(runner.getWorkers()).andReturn( Collections.singletonList( new TestZkWorker(null).toImmutable() ) ).times(2); EasyMock.replay(runner); Provisioner provisioner = strategy.makeProvisioner(runner); boolean terminatedSomething = provisioner.doTerminate(); boolean provisionedSomething = provisioner.doProvision(); Assert.assertFalse(terminatedSomething); Assert.assertFalse(provisionedSomething); EasyMock.verify(autoScaler); EasyMock.verify(runner); }
@Test public void testSimpleGetSpecificIntervals() { EasyMock.expect(inventoryView.getInventory()).andReturn( ImmutableList.of(server) ).atLeastOnce(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_ALLOW_UNSECURED_PATH)).andReturn(null).once(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED)).andReturn(null).once(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)).andReturn( new AuthenticationResult("druid", "druid", null, null) ).once(); request.setAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED, true); EasyMock.expectLastCall().times(1); EasyMock.replay(inventoryView, request); List<Interval> expectedIntervals = new ArrayList<>(); expectedIntervals.add(Intervals.of("2010-01-01T00:00:00.000Z/2010-01-02T00:00:00.000Z")); IntervalsResource intervalsResource = new IntervalsResource( inventoryView, new AuthConfig(), AuthTestUtils.TEST_AUTHORIZER_MAPPER ); Response response = intervalsResource.getSpecificIntervals("2010-01-01T00:00:00.000Z/P1D", "simple", null, request); Map<Interval, Map<String, Object>> actualIntervals = (Map) response.getEntity(); Assert.assertEquals(1, actualIntervals.size()); Assert.assertTrue(actualIntervals.containsKey(expectedIntervals.get(0))); Assert.assertEquals(25L, actualIntervals.get(expectedIntervals.get(0)).get("size")); Assert.assertEquals(2, actualIntervals.get(expectedIntervals.get(0)).get("count")); }
@Test public void testTestPermissions() { List<Boolean> response = ImmutableList.of(true, true); String getPermission = "resourcemanager.projects.get"; String deletePermission = "resourcemanager.projects.delete"; expect(resourceManager.getOptions()).andReturn(mockOptions).times(1); expect( resourceManager.testPermissions( PROJECT_ID, ImmutableList.of(getPermission, deletePermission))) .andReturn(response); replay(resourceManager); initializeProject(); assertEquals( response, project.testPermissions(ImmutableList.of(getPermission, deletePermission))); }
@Test public void testGetIntervals() EasyMock.expect(inventoryView.getInventory()).andReturn( ImmutableList.of(server) ).atLeastOnce(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_ALLOW_UNSECURED_PATH)).andReturn(null).once(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED)).andReturn(null).once(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)).andReturn( new AuthenticationResult("druid", "druid", null, null) ).once(); request.setAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED, true); EasyMock.expectLastCall().times(1); EasyMock.replay(inventoryView, request);
@Test public void testGetSpecificIntervals() { EasyMock.expect(inventoryView.getInventory()).andReturn( ImmutableList.of(server) ).atLeastOnce(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_ALLOW_UNSECURED_PATH)).andReturn(null).once(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED)).andReturn(null).once(); EasyMock.expect(request.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT)).andReturn( new AuthenticationResult("druid", "druid", null, null) ).once(); request.setAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED, true); EasyMock.expectLastCall().times(1); EasyMock.replay(inventoryView, request); IntervalsResource intervalsResource = new IntervalsResource( inventoryView, new AuthConfig(), AuthTestUtils.TEST_AUTHORIZER_MAPPER ); Response response = intervalsResource.getSpecificIntervals("2010-01-01T00:00:00.000Z/P1D", null, null, request); Map<String, Object> actualIntervals = (Map) response.getEntity(); Assert.assertEquals(2, actualIntervals.size()); Assert.assertEquals(25L, actualIntervals.get("size")); Assert.assertEquals(2, actualIntervals.get("count")); }
@Test public void testNext() { boolean expected = true; EasyMock.expect(peekIterator.hasNext()).andReturn(expected).times(4); String defaultString = "S1"; String resString = "S2"; EasyMock.expect(peekIterator.next()).andReturn(defaultString); EasyMock.expect(binaryFn.apply(EasyMock.eq(defaultString), EasyMock.isNull())) .andReturn(resString); EasyMock.expect(peekIterator.next()).andReturn(defaultString); EasyMock.expect(comparator.compare(EasyMock.eq(resString), EasyMock.eq(defaultString))) .andReturn(0); EasyMock.expect(peekIterator.next()).andReturn(defaultString); EasyMock.expect(binaryFn.apply(EasyMock.eq(resString), EasyMock.eq(defaultString))) .andReturn(resString); EasyMock.expect(comparator.compare(EasyMock.eq(resString), EasyMock.eq(defaultString))) .andReturn(1); EasyMock.replay(peekIterator); EasyMock.replay(binaryFn); EasyMock.replay(comparator); String actual = testingIterator.next(); Assert.assertEquals(resString, actual); EasyMock.verify(peekIterator); EasyMock.verify(comparator); EasyMock.verify(binaryFn); }
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 reloadByNameAndNotFound() { expect(dns.getZone(ZONE_NAME)).andReturn(null).times(2); // again for options expect(dns.getZone(ZONE_NAME, ZONE_FIELD_OPTIONS)).andReturn(null).times(2); replay(dns); Zone result = zoneNoId.reload(); assertNull(result); result = zone.reload(); assertNull(result); zoneNoId.reload(ZONE_FIELD_OPTIONS); // for options zone.reload(ZONE_FIELD_OPTIONS); // for options }