From 458236c8fd363ea9aaa09174b174513244d7991c Mon Sep 17 00:00:00 2001 From: Charles Allen Date: Mon, 22 Dec 2014 21:53:58 -0800 Subject: [PATCH] Add simple unit tests for QueryResource --- .../io/druid/server/QueryResourceTest.java | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 server/src/test/java/io/druid/server/QueryResourceTest.java diff --git a/server/src/test/java/io/druid/server/QueryResourceTest.java b/server/src/test/java/io/druid/server/QueryResourceTest.java new file mode 100644 index 00000000000..a8b6d52335e --- /dev/null +++ b/server/src/test/java/io/druid/server/QueryResourceTest.java @@ -0,0 +1,153 @@ +/* + * Druid - a distributed column store. + * Copyright (C) 2012, 2013, 2014 Metamarkets Group Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package io.druid.server; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.metamx.common.guava.Sequence; +import com.metamx.common.guava.Sequences; +import com.metamx.emitter.service.ServiceEmitter; +import io.druid.jackson.DefaultObjectMapper; +import io.druid.query.Query; +import io.druid.query.QueryRunner; +import io.druid.query.QuerySegmentWalker; +import io.druid.query.SegmentDescriptor; +import io.druid.server.initialization.ServerConfig; +import io.druid.server.log.NoopRequestLogger; +import io.druid.server.metrics.NoopServiceEmitter; +import org.easymock.EasyMock; +import org.joda.time.Interval; +import org.joda.time.Period; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Map; + +/** + * + */ +public class QueryResourceTest +{ + private static final ObjectMapper jsonMapper = new DefaultObjectMapper(); + public static final ServerConfig serverConfig = new ServerConfig(){ + @Override + public int getNumThreads(){ + return 1; + } + @Override + public Period getMaxIdleTime(){ + return Period.seconds(1); + } + }; + private final HttpServletRequest testServletRequest = EasyMock.createMock(HttpServletRequest.class); + public static final QuerySegmentWalker testSegmentWalker = new QuerySegmentWalker() + { + @Override + public QueryRunner getQueryRunnerForIntervals( + Query query, Iterable intervals + ) + { + return new QueryRunner() + { + @Override + public Sequence run( + Query query, Map responseContext + ) + { + return Sequences.empty(); + } + }; + } + + @Override + public QueryRunner getQueryRunnerForSegments( + Query query, Iterable specs + ) + { + return getQueryRunnerForIntervals(null, null); + } + }; + + private static final ServiceEmitter noopServiceEmitter = new NoopServiceEmitter(); + @BeforeClass + public static void staticSetup(){ + com.metamx.emitter.EmittingLogger.registerEmitter(noopServiceEmitter); + } + @Before + public void setup() + { + EasyMock.expect(testServletRequest.getContentType()).andReturn(MediaType.APPLICATION_JSON); + EasyMock.expect(testServletRequest.getRemoteAddr()).andReturn("localhost").anyTimes(); + EasyMock.replay(testServletRequest); + } + private static final String simpleTimeSeriesQuery = "{\n" + + " \"queryType\": \"timeseries\",\n" + + " \"dataSource\": \"mmx_metrics\",\n" + + " \"granularity\": \"hour\",\n" + + " \"intervals\": [\n" + + " \"2014-12-17/2015-12-30\"\n" + + " ],\n" + + " \"aggregations\": [\n" + + " {\n" + + " \"type\": \"count\",\n" + + " \"name\": \"rows\"\n" + + " }\n" + + " ]\n" + + "}"; + @Test + public void testGoodQuery() throws IOException + { + QueryResource queryResource = new QueryResource( + serverConfig, + jsonMapper, + jsonMapper, + testSegmentWalker, + new NoopServiceEmitter(), + new NoopRequestLogger(), + new QueryManager() + ); + Response respone = queryResource.doPost(new ByteArrayInputStream(simpleTimeSeriesQuery.getBytes("UTF-8")), null /*pretty*/, testServletRequest); + Assert.assertNotNull(respone); + } + @Test + public void testBadQuery() throws IOException + { + + QueryResource queryResource = new QueryResource( + serverConfig, + jsonMapper, + jsonMapper, + testSegmentWalker, + new NoopServiceEmitter(), + new NoopRequestLogger(), + new QueryManager() + ); + Response respone = queryResource.doPost(new ByteArrayInputStream("Meka Leka Hi Meka Hiney Ho".getBytes("UTF-8")), null /*pretty*/, testServletRequest); + Assert.assertNotNull(respone); + Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), respone.getStatus()); + } +}