Repository: incubator-eagle Updated Branches: refs/heads/master 021c2bddd -> 286034ff3
[EAGLE-775] add unit test for eagle-service-base https://issues.apache.org/jira/browse/EAGLE-775 Author: koone <luokun1...@126.com> Closes #689 from koone/EAGLE-775. Project: http://git-wip-us.apache.org/repos/asf/incubator-eagle/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-eagle/commit/286034ff Tree: http://git-wip-us.apache.org/repos/asf/incubator-eagle/tree/286034ff Diff: http://git-wip-us.apache.org/repos/asf/incubator-eagle/diff/286034ff Branch: refs/heads/master Commit: 286034ff3b68eb8ba1650c6fd013dd5d6feb68f5 Parents: 021c2bd Author: koone <luokun1...@126.com> Authored: Tue Nov 29 15:49:53 2016 +0800 Committer: Hao Chen <h...@apache.org> Committed: Tue Nov 29 15:49:53 2016 +0800 ---------------------------------------------------------------------- .../eagle-query/eagle-service-base/pom.xml | 12 + .../TestGenericEntityServiceResource.java | 236 +++++++++++++++++++ .../service/generic/TestResourceUnmarshal.java | 125 ++++++++++ .../service/metric/EagleMetricResourceApp.java | 41 ++++ .../service/metric/TestEagleMetricResource.java | 74 ++++++ 5 files changed, 488 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/286034ff/eagle-core/eagle-query/eagle-service-base/pom.xml ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-service-base/pom.xml b/eagle-core/eagle-query/eagle-service-base/pom.xml index a657581..74f635e 100755 --- a/eagle-core/eagle-query/eagle-service-base/pom.xml +++ b/eagle-core/eagle-query/eagle-service-base/pom.xml @@ -139,6 +139,18 @@ <version>${org.eclipse.jetty.orbit.javax.servlet.version}</version> <scope>provided</scope> </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-module-junit4</artifactId> + <version>${powermock.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-api-mockito</artifactId> + <version>${powermock.version}</version> + <scope>test</scope> + </dependency> <!--<dependency>--> <!--<groupId>org.codehaus.jackson</groupId>--> <!--<artifactId>jackson-mapper-asl</artifactId>--> http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/286034ff/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/generic/TestGenericEntityServiceResource.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/generic/TestGenericEntityServiceResource.java b/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/generic/TestGenericEntityServiceResource.java new file mode 100644 index 0000000..862f09b --- /dev/null +++ b/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/generic/TestGenericEntityServiceResource.java @@ -0,0 +1,236 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eagle.service.generic; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.dropwizard.testing.junit.ResourceTestRule; +import org.apache.eagle.log.base.taggedlog.EntityJsonModule; +import org.apache.eagle.log.base.taggedlog.TaggedLogAPIEntity; +import org.apache.eagle.log.entity.GenericServiceAPIResponseEntity; +import org.apache.eagle.log.entity.meta.EntityDefinition; +import org.apache.eagle.log.entity.meta.EntityDefinitionManager; +import org.apache.eagle.log.entity.test.TestTimeSeriesAPIEntity; +import org.apache.eagle.storage.DataStorage; +import org.apache.eagle.storage.DataStorageManager; +import org.apache.eagle.storage.exception.IllegalDataStorageTypeException; +import org.apache.eagle.storage.hbase.HBaseStorage; +import org.apache.eagle.storage.operation.UpdateStatement; +import org.apache.eagle.storage.result.ModifyResult; +import org.apache.eagle.storage.result.QueryResult; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.junit.*; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({DataStorageManager.class, HBaseConfiguration.class}) +public class TestGenericEntityServiceResource { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + private DataStorage dataStorage; + private GenericEntityServiceResource genericEntityServiceResource = new GenericEntityServiceResource(); + + @Rule + public ResourceTestRule resources = ResourceTestRule.builder() + .addResource(genericEntityServiceResource).setMapper(MAPPER) + .build(); + + @BeforeClass + public static void setupMapper() throws IllegalDataStorageTypeException, IOException { + MAPPER.setFilters(TaggedLogAPIEntity.getFilterProvider()); + MAPPER.registerModule(new EntityJsonModule()); + } + + @Before + public void setup() throws IllegalDataStorageTypeException { + dataStorage = mock(HBaseStorage.class); + mockStatic(DataStorageManager.class); + mockStatic(HBaseConfiguration.class); + when(DataStorageManager.getDataStorageByEagleConfig()).thenReturn(dataStorage); + when(HBaseConfiguration.create()).thenReturn(new Configuration()); + } + + @Test + public void testGenericEntityServiceResourceupdateDatabaseNullDataStorage() throws IllegalDataStorageTypeException { + when(DataStorageManager.getDataStorageByEagleConfig()).thenReturn(null); + GenericServiceAPIResponseEntity responseEntity = genericEntityServiceResource.updateDatabase(null); + Assert.assertFalse(responseEntity.isSuccess()); + Assert.assertEquals(null, responseEntity.getMeta()); + Assert.assertEquals(null, responseEntity.getType()); + Assert.assertEquals(null, responseEntity.getObj()); + Assert.assertTrue(responseEntity.getException().startsWith("org.apache.eagle.storage.exception.IllegalDataStorageException: Data storage is null")); + } + + @Test + public void testGenericEntityServiceResourceupdateDatabaseFalse() throws IllegalDataStorageTypeException, IOException, IllegalAccessException, InstantiationException { + TestTimeSeriesAPIEntity e = new TestTimeSeriesAPIEntity(); + List<TestTimeSeriesAPIEntity> entities = new ArrayList<>(); + entities.add(e); + EntityDefinition ed = EntityDefinitionManager.getEntityByServiceName("TestTimeSeriesAPIEntity"); + + ModifyResult<String> modifyResult = new ModifyResult<>(); + modifyResult.setSuccess(false); + when(dataStorage.update(entities, ed)).thenReturn(modifyResult); + GenericServiceAPIResponseEntity responseEntity = genericEntityServiceResource.updateDatabase(new UpdateStatement(entities, "TestTimeSeriesAPIEntity")); + Assert.assertFalse(responseEntity.isSuccess()); + Assert.assertEquals(null, responseEntity.getMeta()); + Assert.assertEquals(null, responseEntity.getType()); + Assert.assertEquals(null, responseEntity.getObj()); + Assert.assertEquals(null, responseEntity.getException()); + verify(dataStorage).update(entities, ed); + } + + @Test + public void testGenericEntityServiceResourceupdateDatabase() throws IllegalDataStorageTypeException, IOException, IllegalAccessException, InstantiationException { + TestTimeSeriesAPIEntity e = new TestTimeSeriesAPIEntity(); + List<TestTimeSeriesAPIEntity> entities = new ArrayList<>(); + entities.add(e); + EntityDefinition ed = EntityDefinitionManager.getEntityByServiceName("TestTimeSeriesAPIEntity"); + + ModifyResult<String> modifyResult = new ModifyResult<>(); + List<String> identifiers = new ArrayList<>(); + identifiers.add("test"); + modifyResult.setSuccess(true); + modifyResult.setIdentifiers(identifiers); + when(dataStorage.update(entities, ed)).thenReturn(modifyResult); + GenericServiceAPIResponseEntity responseEntity = genericEntityServiceResource.updateDatabase(new UpdateStatement(entities, "TestTimeSeriesAPIEntity")); + Assert.assertTrue(responseEntity.isSuccess()); + Assert.assertTrue(responseEntity.getMeta().toString().startsWith("{totalResults=1")); + Assert.assertEquals(String.class, responseEntity.getType()); + Assert.assertEquals("test", responseEntity.getObj().get(0)); + Assert.assertEquals(null, responseEntity.getException()); + verify(dataStorage).update(entities, ed); + } + + @Test + public void testGenericEntityServiceResourceupdateDatabase1() throws IllegalDataStorageTypeException, IOException, IllegalAccessException, InstantiationException { + TestTimeSeriesAPIEntity e = new TestTimeSeriesAPIEntity(); + List<TestTimeSeriesAPIEntity> entities = new ArrayList<>(); + entities.add(e); + EntityDefinition ed = EntityDefinitionManager.getEntityByServiceName("TestTimeSeriesAPIEntity"); + + ModifyResult<String> modifyResult = new ModifyResult<>(); + List<String> identifiers = null; + modifyResult.setSuccess(true); + modifyResult.setIdentifiers(identifiers); + when(dataStorage.update(entities, ed)).thenReturn(modifyResult); + GenericServiceAPIResponseEntity responseEntity = genericEntityServiceResource.updateDatabase(new UpdateStatement(entities, "TestTimeSeriesAPIEntity")); + Assert.assertTrue(responseEntity.isSuccess()); + Assert.assertTrue(responseEntity.getMeta().toString().startsWith("{totalResults=0")); + Assert.assertEquals(null, responseEntity.getType()); + Assert.assertEquals(null, responseEntity.getObj()); + Assert.assertEquals(null, responseEntity.getException()); + verify(dataStorage).update(entities, ed); + } + + + @Test + public void testGenericEntityServiceResourceSearchFalse() throws IllegalAccessException, InstantiationException, IOException { + QueryResult<TestTimeSeriesAPIEntity> queryResult = new QueryResult<>(); + queryResult.setSuccess(false); + List<String> rowkeys = new ArrayList<>(); + rowkeys.add("test"); + EntityDefinition ed = EntityDefinitionManager.getEntityByServiceName("TestTimeSeriesAPIEntity"); + when(dataStorage.queryById(rowkeys, ed)).thenReturn(queryResult); + GenericServiceAPIResponseEntity responseEntity = resources.client().resource("/entities/rowkey").queryParam("value", "test").queryParam("serviceName", "TestTimeSeriesAPIEntity").get(GenericServiceAPIResponseEntity.class); + + Assert.assertFalse(responseEntity.isSuccess()); + Assert.assertEquals(null, responseEntity.getMeta()); + Assert.assertEquals(null, responseEntity.getType()); + Assert.assertEquals(null, responseEntity.getObj()); + Assert.assertEquals(null, responseEntity.getException()); + verify(dataStorage).queryById(rowkeys, ed); + } + + @Test + public void testGenericEntityServiceResourceSearch() throws IllegalAccessException, InstantiationException, IOException { + List<TestTimeSeriesAPIEntity> result = new ArrayList<>(); + TestTimeSeriesAPIEntity e = new TestTimeSeriesAPIEntity(); + long timestamp = System.currentTimeMillis(); + e.setTimestamp(timestamp); + e.setField1(1); + e.setField2(2); + e.setField3(3); + e.setField4(4L); + e.setField5(5.0); + e.setField6(5.0); + e.setField7("7"); + e.setTags(new HashMap<>()); + e.getTags().put("cluster", "test4UT"); + e.getTags().put("datacenter", "dc1"); + e.getTags().put("index", "" + 1); + e.getTags().put("jobId", "job_2"); + result.add(e); + QueryResult<TestTimeSeriesAPIEntity> queryResult = new QueryResult<>(); + + queryResult.setData(result); + queryResult.setSuccess(true); + queryResult.setSize(result.size()); + + + EntityDefinition ed = EntityDefinitionManager.getEntityByServiceName("TestTimeSeriesAPIEntity"); + List<String> rowkeys = new ArrayList<>(); + rowkeys.add("test"); + when(dataStorage.queryById(rowkeys, ed)).thenReturn(queryResult); + GenericServiceAPIResponseEntity responseEntity = resources.client().resource("/entities/rowkey").queryParam("value", "test").queryParam("serviceName", "TestTimeSeriesAPIEntity").get(GenericServiceAPIResponseEntity.class); + + Assert.assertTrue(responseEntity.isSuccess()); + Assert.assertTrue(responseEntity.getMeta().toString().startsWith("{firstTimestamp=null, totalResults=1, lastTimestamp=null, elapsedms=")); + Assert.assertEquals(null, responseEntity.getType()); + Assert.assertEquals("{prefix=null, timestamp=" + timestamp + ", tags={cluster=test4UT, jobId=job_2, index=1, datacenter=dc1}, exp=null, encodedRowkey=null, serializeAlias=null, serializeVerbose=true, field1=1, field2=2, field3=3, field4=4, field5=5.0, field6=5.0, field7=7}", responseEntity.getObj().get(0).toString()); + Assert.assertEquals(null, responseEntity.getException()); + verify(dataStorage).queryById(rowkeys, ed); + } + + @Test + public void testGenericEntityServiceResourceSearchNullServiceName() throws IllegalAccessException, InstantiationException, IOException { + GenericServiceAPIResponseEntity responseEntity = resources.client().resource("/entities/rowkey").queryParam("value", "test").get(GenericServiceAPIResponseEntity.class); + Assert.assertFalse(responseEntity.isSuccess()); + Assert.assertEquals(null, responseEntity.getMeta()); + Assert.assertEquals(null, responseEntity.getType()); + Assert.assertEquals(null, responseEntity.getObj()); + Assert.assertTrue(responseEntity.getException().startsWith("java.lang.Exception: java.lang.IllegalArgumentException: serviceName is null")); + verify(dataStorage, never()).queryById(any(), any()); + } + + @Test + public void testGenericEntityServiceResourceSearchNullDataStorage() throws IllegalAccessException, InstantiationException, IOException, IllegalDataStorageTypeException { + when(DataStorageManager.getDataStorageByEagleConfig()).thenReturn(null); + GenericServiceAPIResponseEntity responseEntity = resources.client().resource("/entities/rowkey").queryParam("value", "test").queryParam("serviceName", "TestTimeSeriesAPIEntity").get(GenericServiceAPIResponseEntity.class); + Assert.assertFalse(responseEntity.isSuccess()); + Assert.assertEquals(null, responseEntity.getMeta()); + Assert.assertEquals(null, responseEntity.getType()); + Assert.assertEquals(null, responseEntity.getObj()); + Assert.assertTrue(responseEntity.getException().startsWith("java.lang.Exception: org.apache.eagle.storage.exception.IllegalDataStorageException: data storage is null")); + verify(dataStorage, never()).queryById(any(), any()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/286034ff/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/generic/TestResourceUnmarshal.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/generic/TestResourceUnmarshal.java b/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/generic/TestResourceUnmarshal.java new file mode 100644 index 0000000..4e21063 --- /dev/null +++ b/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/generic/TestResourceUnmarshal.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eagle.service.generic; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.eagle.log.base.taggedlog.EntityJsonModule; +import org.apache.eagle.log.base.taggedlog.TaggedLogAPIEntity; +import org.apache.eagle.log.entity.meta.EntityDefinition; +import org.apache.eagle.log.entity.meta.EntityDefinitionManager; +import org.apache.eagle.log.entity.test.TestTimeSeriesAPIEntity; +import org.apache.eagle.storage.exception.IllegalDataStorageTypeException; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class TestResourceUnmarshal { + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @BeforeClass + public static void setup() throws IllegalDataStorageTypeException, IOException { + MAPPER.setFilters(TaggedLogAPIEntity.getFilterProvider()); + MAPPER.registerModule(new EntityJsonModule()); + } + @Test + public void testUnmarshalEntitiesByServie() throws NoSuchMethodException, InvocationTargetException, JsonProcessingException, IllegalAccessException, InstantiationException { + + TestTimeSeriesAPIEntity timeSeriesAPIEntity = new TestTimeSeriesAPIEntity(); + timeSeriesAPIEntity.setTimestamp(System.currentTimeMillis()); + timeSeriesAPIEntity.setField1(1); + timeSeriesAPIEntity.setField2(2); + timeSeriesAPIEntity.setField3(3); + timeSeriesAPIEntity.setField4(4L); + timeSeriesAPIEntity.setField5(5.0); + timeSeriesAPIEntity.setField6(5.0); + timeSeriesAPIEntity.setField7("7"); + timeSeriesAPIEntity.setTags(new HashMap<>()); + timeSeriesAPIEntity.getTags().put("cluster", "test4UT"); + timeSeriesAPIEntity.getTags().put("datacenter", "dc1"); + timeSeriesAPIEntity.getTags().put("index", "" + 1); + timeSeriesAPIEntity.getTags().put("jobId", "job_" + timeSeriesAPIEntity.getTimestamp()); + + List<TestTimeSeriesAPIEntity> timeSeriesAPIEntityList = new ArrayList<>(); + timeSeriesAPIEntityList.add(timeSeriesAPIEntity); + + GenericEntityServiceResource genericEntityServiceResource = new GenericEntityServiceResource(); + Method unmarshalEntitiesByServie = genericEntityServiceResource.getClass().getDeclaredMethod("unmarshalEntitiesByServie", InputStream.class, EntityDefinition.class); + unmarshalEntitiesByServie.setAccessible(true); + InputStream stream = new ByteArrayInputStream(MAPPER.writeValueAsString(timeSeriesAPIEntityList).getBytes(StandardCharsets.UTF_8)); + EntityDefinition ed = EntityDefinitionManager.getEntityDefinitionByEntityClass(TestTimeSeriesAPIEntity.class); + List<TestTimeSeriesAPIEntity> result = (List<TestTimeSeriesAPIEntity>) unmarshalEntitiesByServie.invoke(genericEntityServiceResource, stream, ed); + + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getTimestamp(), result.get(0).getTimestamp()); + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getField1(), result.get(0).getField1()); + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getField2(), result.get(0).getField2()); + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getField3(), result.get(0).getField3()); + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getField4(), result.get(0).getField4()); + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getField5(), result.get(0).getField5(), 0.1); + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getField6(), result.get(0).getField6()); + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getField7(), result.get(0).getField7()); + + + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getTags().get("cluster"), result.get(0).getTags().get("cluster")); + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getTags().get("datacenter"), result.get(0).getTags().get("datacenter")); + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getTags().get("index"), result.get(0).getTags().get("index")); + Assert.assertEquals(timeSeriesAPIEntityList.get(0).getTags().get("jobId"), result.get(0).getTags().get("jobId")); + + } + + @Test + public void testUnmarshalAsStringlist() throws NoSuchMethodException, JsonProcessingException, InvocationTargetException, IllegalAccessException { + + TestTimeSeriesAPIEntity timeSeriesAPIEntity = new TestTimeSeriesAPIEntity(); + timeSeriesAPIEntity.setTimestamp(1l); + timeSeriesAPIEntity.setField1(1); + timeSeriesAPIEntity.setField2(2); + timeSeriesAPIEntity.setField3(3); + timeSeriesAPIEntity.setField4(4L); + timeSeriesAPIEntity.setField5(5.0); + timeSeriesAPIEntity.setField6(5.0); + timeSeriesAPIEntity.setField7("7"); + timeSeriesAPIEntity.setTags(new HashMap<>()); + timeSeriesAPIEntity.getTags().put("cluster", "test4UT"); + timeSeriesAPIEntity.getTags().put("datacenter", "dc1"); + timeSeriesAPIEntity.getTags().put("index", "" + 1); + timeSeriesAPIEntity.getTags().put("jobId", "job_" + timeSeriesAPIEntity.getTimestamp()); + + List<TestTimeSeriesAPIEntity> timeSeriesAPIEntityList = new ArrayList<>(); + timeSeriesAPIEntityList.add(timeSeriesAPIEntity); + + GenericEntityServiceResource genericEntityServiceResource = new GenericEntityServiceResource(); + Method unmarshalAsStringlist = genericEntityServiceResource.getClass().getDeclaredMethod("unmarshalAsStringlist", InputStream.class); + unmarshalAsStringlist.setAccessible(true); + + InputStream stream = new ByteArrayInputStream(MAPPER.writeValueAsString(timeSeriesAPIEntityList).getBytes(StandardCharsets.UTF_8)); + List<String> result = (List<String>) unmarshalAsStringlist.invoke(genericEntityServiceResource, stream); + + Assert.assertEquals("[{, prefix, null, timestamp, 1, tags, {, cluster, test4UT, jobId, job_1, index, 1, datacenter, dc1, }, exp, null, encodedRowkey, null, serializeAlias, null, serializeVerbose, true, field1, 1, field2, 2, field3, 3, field4, 4, field5, 5.0, field6, 5.0, field7, 7, }]", result.toString()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/286034ff/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/metric/EagleMetricResourceApp.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/metric/EagleMetricResourceApp.java b/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/metric/EagleMetricResourceApp.java new file mode 100644 index 0000000..dcb00bd --- /dev/null +++ b/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/metric/EagleMetricResourceApp.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.eagle.service.metric; + +import io.dropwizard.Application; +import io.dropwizard.Configuration; +import io.dropwizard.jetty.HttpConnectorFactory; +import io.dropwizard.server.DefaultServerFactory; +import io.dropwizard.setup.Bootstrap; +import io.dropwizard.setup.Environment; +import org.apache.eagle.service.generic.MetadataResource; + + +public class EagleMetricResourceApp extends Application<Configuration> { + + @Override + public void initialize(Bootstrap<Configuration> bootstrap) { + } + + @Override + public void run(Configuration configuration, Environment environment) throws Exception { + ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()).getApplicationConnectors().get(0)).setPort(0); + ((HttpConnectorFactory) ((DefaultServerFactory) configuration.getServerFactory()).getAdminConnectors().get(0)).setPort(0); + environment.jersey().register(EagleMetricResource.class); + } +} http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/286034ff/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/metric/TestEagleMetricResource.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/metric/TestEagleMetricResource.java b/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/metric/TestEagleMetricResource.java new file mode 100644 index 0000000..d67ed75 --- /dev/null +++ b/eagle-core/eagle-query/eagle-service-base/src/test/java/org/apache/eagle/service/metric/TestEagleMetricResource.java @@ -0,0 +1,74 @@ +package org.apache.eagle.service.metric; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.sun.jersey.api.client.Client; +import io.dropwizard.Configuration; +import io.dropwizard.jackson.Jackson; +import io.dropwizard.testing.junit.DropwizardAppRule; +import org.apache.eagle.log.base.taggedlog.TaggedLogAPIEntity; +import org.apache.eagle.log.entity.GenericCreateAPIResponseEntity; +import org.apache.eagle.log.entity.GenericMetricEntity; +import org.apache.eagle.service.generic.MetadataResource; +import org.apache.eagle.service.generic.MetadataResourceApp; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; + +import javax.ws.rs.core.MediaType; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static javax.ws.rs.core.HttpHeaders.ACCEPT; +import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE; +import static org.junit.Assert.*; + +/** + * Created by luokun on 11/25/16. + */ +public class TestEagleMetricResource { + + private String restURL; + private String requestJson; + + @ClassRule + public static final DropwizardAppRule<Configuration> RULE = + new DropwizardAppRule<>(EagleMetricResourceApp.class, null); + + + private void getMetadataResource( ){ + new Client().resource(restURL ) + .header(ACCEPT, MediaType.APPLICATION_JSON) + .header(CONTENT_TYPE, MediaType.APPLICATION_JSON) + .post(GenericCreateAPIResponseEntity.class, requestJson); + } + + @Before + public void setUp() throws JsonProcessingException { + restURL = "http://localhost:" + RULE.getLocalPort() + EagleMetricResource.METRIC_URL_PATH; + List<GenericMetricEntity> entities = new ArrayList<GenericMetricEntity>(); + Map<String,String> tags = new HashMap<String, String>() {{ + put("cluster", "cluster4ut"); + put("datacenter", "datacenter4ut"); + }}; + for(int i=0;i<100;i++){ + GenericMetricEntity entity = new GenericMetricEntity(); + entity.setTimestamp(System.currentTimeMillis()); + entity.setTags(tags); + entity.setValue(new double[]{1.234}); + entity.setPrefix("unit.test.metrics"); + entities.add(entity); + } + requestJson = TaggedLogAPIEntity.buildObjectMapper().writeValueAsString(entities); + } + + @Test + public void testCreateGenericMetricEntity() throws Exception { + // getMetadataResource(); + } + +} \ No newline at end of file