private static ShardSpec shardSpec(String dimension, boolean contained) { ShardSpec shard = EasyMock.createMock(ShardSpec.class); EasyMock.expect(shard.getDomainDimensions()) .andReturn(ImmutableList.of(dimension)) .anyTimes(); EasyMock.expect(shard.possibleInDomain(EasyMock.anyObject())) .andReturn(contained) .anyTimes(); return shard; } }
/** * Prepares an executor service mock to expect the start of the timer. * * @param service the mock * @param future the future */ private void prepareStartTimer(final ScheduledExecutorService service, final ScheduledFuture<?> future) { service.scheduleAtFixedRate((Runnable) EasyMock.anyObject(), EasyMock .eq(PERIOD), EasyMock.eq(PERIOD), EasyMock.eq(UNIT)); EasyMock.expectLastCall().andReturn(future); }
@Test public void testHearDestroyable() throws Exception { TypeEncounter encounter = createMock(TypeEncounter.class); encounter.register(anyObject(DestroyableInjectionListener.class)); replay(encounter); LifecycleTypeListener underTest = new LifecycleTypeListener(null); underTest.hear(TypeLiteral.get(MyDestroyable.class), encounter); verify(encounter); }
@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); }
private ImmutableWorkerInfo createMockWorker(int currCapacityUsed, boolean canRunTask, boolean isValidVersion) { ImmutableWorkerInfo worker = EasyMock.createMock(ImmutableWorkerInfo.class); EasyMock.expect(worker.canRunTask(EasyMock.anyObject(Task.class))).andReturn(canRunTask).anyTimes(); EasyMock.expect(worker.getCurrCapacityUsed()).andReturn(currCapacityUsed).anyTimes(); EasyMock.expect(worker.isValidVersion(EasyMock.anyString())).andReturn(isValidVersion).anyTimes(); EasyMock.replay(worker); return worker; }
@Test public void testHearInitializable() throws Exception { TypeEncounter encounter = createMock(TypeEncounter.class); encounter.register(anyObject(InitializableInjectionListener.class)); replay(encounter); LifecycleTypeListener underTest = new LifecycleTypeListener(null); underTest.hear(TypeLiteral.get(MyInitializable.class), encounter); verify(encounter); }
@Test public void shouldWaitIfCommandSequenceNumberSpecified() throws Exception { // Given: commandQueue.ensureConsumedPast(eq(3L), anyObject()); expectLastCall(); replay(commandQueue); // When: testResource.streamQuery(new KsqlRequest(queryString, Collections.emptyMap(), 3L)); // Then: verify(commandQueue); }
@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 testQueryRegistration() { QueryRunner baseRunner = (queryPlus, responseContext) -> null; QueryWatcher mock = EasyMock.createMock(QueryWatcher.class); mock.registerQuery(EasyMock.eq(query), EasyMock.anyObject(ListenableFuture.class)); EasyMock.replay(mock); AsyncQueryRunner asyncRunner = new AsyncQueryRunner<>(baseRunner, executor, mock); asyncRunner.run(QueryPlus.wrap(query), Collections.EMPTY_MAP); EasyMock.verify(mock); } }
@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 testConstructor() { WebSecurityManager securityManager = createMock(WebSecurityManager.class); FilterChainResolver filterChainResolver = createMock(FilterChainResolver.class); ServletContext servletContext = createMock(ServletContext.class); Capture<WebGuiceEnvironment> capture = new Capture<WebGuiceEnvironment>(); servletContext.setAttribute(eq(EnvironmentLoaderListener.ENVIRONMENT_ATTRIBUTE_KEY), and(anyObject(WebGuiceEnvironment.class), capture(capture))); replay(servletContext, securityManager, filterChainResolver); WebGuiceEnvironment underTest = new WebGuiceEnvironment(filterChainResolver, servletContext, securityManager); assertSame(securityManager, underTest.getSecurityManager()); assertSame(filterChainResolver, underTest.getFilterChainResolver()); assertSame(securityManager, underTest.getWebSecurityManager()); assertSame(servletContext, underTest.getServletContext()); assertSame(underTest, capture.getValue()); verify(servletContext); } }
@Test public void shouldNotAttemptToCreateCommandTopicIfItExists() { EasyMock.resetToStrict(topicClient); EasyMock.expect(topicClient.isTopicExists(COMMAND_TOPIC)).andReturn(true); EasyMock.expect(topicClient.addTopicConfig(anyObject(), anyObject())).andReturn(false); EasyMock.replay(topicClient); KsqlRestApplication.ensureCommandTopic(restConfig, topicClient, COMMAND_TOPIC); EasyMock.verify(topicClient); }
@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); }
private StatusResource getTestStatusResource() { final StatementExecutor mockStatementExecutor = mock(StatementExecutor.class); expect(mockStatementExecutor.getStatuses()).andReturn(mockCommandStatuses); for (final Map.Entry<CommandId, CommandStatus> commandEntry : mockCommandStatuses.entrySet()) { expect(mockStatementExecutor.getStatus(commandEntry.getKey())).andReturn(Optional.of(commandEntry.getValue())); } expect(mockStatementExecutor.getStatus(anyObject(CommandId.class))).andReturn(Optional.empty()); replay(mockStatementExecutor); return new StatusResource(mockStatementExecutor); }
@Test public void testLog() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); DateTime dateTime = DateTimes.nowUtc(); File logDir = temporaryFolder.newFolder(); String nativeQueryLogString = dateTime + "\t" + HOST + "\t" + "native"; String sqlQueryLogString = dateTime + "\t" + HOST + "\t" + "sql"; FileRequestLogger fileRequestLogger = new FileRequestLogger(objectMapper, scheduler, logDir, "yyyy-MM-dd'.log'"); fileRequestLogger.start(); RequestLogLine nativeRequestLogLine = EasyMock.createMock(RequestLogLine.class); EasyMock.expect(nativeRequestLogLine.getNativeQueryLine(EasyMock.anyObject())).andReturn(nativeQueryLogString).anyTimes(); RequestLogLine sqlRequestLogLine = EasyMock.createMock(RequestLogLine.class); EasyMock.expect(sqlRequestLogLine.getSqlQueryLine(EasyMock.anyObject())).andReturn(sqlQueryLogString).anyTimes(); EasyMock.replay(nativeRequestLogLine, sqlRequestLogLine); fileRequestLogger.logNativeQuery(nativeRequestLogLine); fileRequestLogger.logSqlQuery(sqlRequestLogLine); File logFile = new File(logDir, dateTime.toString("yyyy-MM-dd'.log'")); String logString = CharStreams.toString(Files.newBufferedReader(logFile.toPath(), StandardCharsets.UTF_8)); Assert.assertTrue(logString.contains(nativeQueryLogString + "\n" + sqlQueryLogString + "\n")); fileRequestLogger.stop(); } }
@Before public void setUp() { rpcFactoryMock = EasyMock.createMock(BigQueryRpcFactory.class); bigqueryRpcMock = EasyMock.createMock(BigQueryRpc.class); EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(BigQueryOptions.class))) .andReturn(bigqueryRpcMock); EasyMock.replay(rpcFactoryMock); options = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); }
@Test public void testServiceAnnouncementPOSTExceptionInHandler() { final ListenerHandler handler = EasyMock.createStrictMock(ListenerHandler.class); EasyMock.expect(handler.handlePOST( EasyMock.anyObject(), EasyMock.anyObject(), 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.serviceAnnouncementPOST("id", EMPTY_JSON_MAP.get(), req).getStatus() ); EasyMock.verify(req, handler); }