Repository: incubator-griffin
Updated Branches:
  refs/heads/master 2972aa2d0 -> 6b1a7000d


http://git-wip-us.apache.org/repos/asf/incubator-griffin/blob/6b1a7000/service/src/test/java/org/apache/griffin/core/metric/MetricControllerTest.java
----------------------------------------------------------------------
diff --git 
a/service/src/test/java/org/apache/griffin/core/metric/MetricControllerTest.java
 
b/service/src/test/java/org/apache/griffin/core/metric/MetricControllerTest.java
new file mode 100644
index 0000000..97ca3b6
--- /dev/null
+++ 
b/service/src/test/java/org/apache/griffin/core/metric/MetricControllerTest.java
@@ -0,0 +1,172 @@
+/*
+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.griffin.core.metric;
+
+import org.apache.griffin.core.exception.GriffinException;
+import org.apache.griffin.core.exception.GriffinExceptionHandler;
+import org.apache.griffin.core.metric.model.Metric;
+import org.apache.griffin.core.metric.model.MetricValue;
+import org.apache.griffin.core.util.JsonUtil;
+import org.apache.griffin.core.util.URLHelper;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+
+import static 
org.apache.griffin.core.exception.GriffinExceptionMessage.INVALID_METRIC_VALUE_FORMAT;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.BDDMockito.given;
+import static 
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static 
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static 
org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+@RunWith(SpringRunner.class)
+public class MetricControllerTest {
+
+    private MockMvc mvc;
+
+    @InjectMocks
+    private MetricController controller;
+
+    @Mock
+    private MetricServiceImpl service;
+
+
+    @Before
+    public void setup() {
+        mvc = MockMvcBuilders
+                .standaloneSetup(controller)
+                .setControllerAdvice(new GriffinExceptionHandler())
+                .build();
+    }
+
+    @Test
+    public void testGetAllMetricsSuccess() throws Exception {
+        Metric metric = new Metric("metricName", "owner", 
Collections.emptyList());
+        given(service.getAllMetrics()).willReturn(
+                Collections.singletonMap("measureName", 
Collections.singletonList(metric)));
+
+
+        mvc.perform(get(URLHelper.API_VERSION_PATH + "/metrics"))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.measureName", hasSize(1)));
+    }
+
+    @Test
+    public void testGetAllMetricsFailureWithException() throws Exception {
+        given(service.getAllMetrics())
+                .willThrow(new GriffinException.ServiceException("Failed to 
get metrics", new RuntimeException()));
+
+        mvc.perform(get(URLHelper.API_VERSION_PATH + "/metrics"))
+                .andExpect(status().isInternalServerError());
+    }
+
+    @Test
+    public void testGetMetricValuesSuccess() throws Exception {
+        MetricValue value = new MetricValue("jobName", 1L, new HashMap<>());
+        given(service.getMetricValues(Matchers.anyString(), Matchers.anyInt(), 
Matchers.anyInt()))
+                .willReturn(Collections.singletonList(value));
+
+        mvc.perform(get(URLHelper.API_VERSION_PATH + "/metrics/values")
+                .param("metricName", "jobName")
+                .param("size", "5"))
+                .andExpect(jsonPath("$.[0].name", is("jobName")));
+    }
+
+    @Test
+    public void testGetMetricValuesFailureWithException() throws Exception {
+        given(service.getMetricValues(Matchers.anyString(), Matchers.anyInt(), 
Matchers.anyInt()))
+                .willThrow(new GriffinException.ServiceException("Failed to 
get metric values", new IOException()));
+
+        mvc.perform(get(URLHelper.API_VERSION_PATH + "/metrics/values")
+                .param("metricName", "jobName")
+                .param("size", "5"))
+                .andExpect(status().isInternalServerError());
+    }
+
+    @Test
+    public void testAddMetricValuesSuccess() throws Exception {
+        List<MetricValue> values = Collections.singletonList(new 
MetricValue());
+        given(service.addMetricValues(Matchers.any()))
+                .willReturn(new ResponseEntity<>("{\"errors\": false, 
\"items\": []}", HttpStatus.OK));
+
+        mvc.perform(post(URLHelper.API_VERSION_PATH + "/metrics/values")
+                
.contentType(MediaType.APPLICATION_JSON).content(JsonUtil.toJson(values)))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.errors", is(false)));
+    }
+
+    @Test
+    public void testAddMetricValuesFailureWithException() throws Exception {
+        List<MetricValue> values = Collections.singletonList(new 
MetricValue());
+        given(service.addMetricValues(Matchers.any()))
+                .willThrow(new GriffinException.ServiceException("Failed to 
add metric values", new IOException()));
+        mvc.perform(post(URLHelper.API_VERSION_PATH + "/metrics/values")
+                
.contentType(MediaType.APPLICATION_JSON).content(JsonUtil.toJson(values)))
+                .andExpect(status().isInternalServerError());
+    }
+
+    @Test
+    public void testAddMetricValuesFailureWithInvalidFormat() throws Exception 
{
+        List<MetricValue> values = Collections.singletonList(new 
MetricValue());
+        given(service.addMetricValues(Matchers.any()))
+                .willThrow(new 
GriffinException.BadRequestException(INVALID_METRIC_VALUE_FORMAT));
+        mvc.perform(post(URLHelper.API_VERSION_PATH + "/metrics/values")
+                
.contentType(MediaType.APPLICATION_JSON).content(JsonUtil.toJson(values)))
+                .andExpect(status().isBadRequest());
+    }
+
+
+    @Test
+    public void testDeleteMetricValuesSuccess() throws Exception {
+        given(service.deleteMetricValues("metricName"))
+                .willReturn(new ResponseEntity<>("{\"failures\": []}", 
HttpStatus.OK));
+
+        mvc.perform(delete(URLHelper.API_VERSION_PATH + "/metrics/values")
+                .param("metricName", "metricName"))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.failures", hasSize(0)));
+    }
+
+    @Test
+    public void testDeleteMetricValuesFailureWithException() throws Exception {
+        given(service.deleteMetricValues("metricName"))
+                .willThrow(new GriffinException.ServiceException("Failed to 
delete metric values.", new IOException()));
+
+        mvc.perform(delete(URLHelper.API_VERSION_PATH + "/metrics/values")
+                .param("metricName", "metricName"))
+                .andExpect(status().isInternalServerError());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-griffin/blob/6b1a7000/service/src/test/java/org/apache/griffin/core/metric/MetricServiceImplTest.java
----------------------------------------------------------------------
diff --git 
a/service/src/test/java/org/apache/griffin/core/metric/MetricServiceImplTest.java
 
b/service/src/test/java/org/apache/griffin/core/metric/MetricServiceImplTest.java
new file mode 100644
index 0000000..a7084da
--- /dev/null
+++ 
b/service/src/test/java/org/apache/griffin/core/metric/MetricServiceImplTest.java
@@ -0,0 +1,172 @@
+/*
+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.griffin.core.metric;
+
+import org.apache.griffin.core.exception.GriffinException;
+import org.apache.griffin.core.job.entity.AbstractJob;
+import org.apache.griffin.core.job.repo.JobRepo;
+import org.apache.griffin.core.measure.entity.Measure;
+import org.apache.griffin.core.measure.repo.MeasureRepo;
+import org.apache.griffin.core.metric.model.Metric;
+import org.apache.griffin.core.metric.model.MetricValue;
+import org.apache.griffin.core.util.JsonUtil;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.griffin.core.util.EntityHelper.createGriffinJob;
+import static org.apache.griffin.core.util.EntityHelper.createGriffinMeasure;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.BDDMockito.given;
+
+@RunWith(SpringRunner.class)
+public class MetricServiceImplTest {
+
+    @InjectMocks
+    private MetricServiceImpl service;
+
+    @Mock
+    private MeasureRepo<Measure> measureRepo;
+    @Mock
+    private JobRepo<AbstractJob> jobRepo;
+    @Mock
+    private MetricStoreImpl metricStore;
+
+    @Before
+    public void setup() {
+    }
+
+    @Test
+    public void testGetAllMetricsSuccess() throws Exception {
+        Measure measure = createGriffinMeasure("measureName");
+        measure.setId(1L);
+        AbstractJob job = createGriffinJob();
+        MetricValue value = new MetricValue("jobName", 1L, new HashMap<>());
+        
given(jobRepo.findByDeleted(false)).willReturn(Collections.singletonList(job));
+        
given(measureRepo.findByDeleted(false)).willReturn(Collections.singletonList(measure));
+        given(metricStore.getMetricValues(Matchers.anyString(), 
Matchers.anyInt(), Matchers.anyInt()))
+                .willReturn(Collections.singletonList(value));
+
+        Map<String, List<Metric>> metricMap = service.getAllMetrics();
+        assertEquals(metricMap.get("measureName").get(0).getName(), "jobName");
+    }
+
+    @Test(expected = GriffinException.ServiceException.class)
+    public void testGetAllMetricsFailureWithException() throws Exception {
+        Measure measure = createGriffinMeasure("measureName");
+        measure.setId(1L);
+        AbstractJob job = createGriffinJob();
+        
given(jobRepo.findByDeleted(false)).willReturn(Collections.singletonList(job));
+        
given(measureRepo.findByDeleted(false)).willReturn(Collections.singletonList(measure));
+        given(metricStore.getMetricValues(Matchers.anyString(), 
Matchers.anyInt(), Matchers.anyInt()))
+                .willThrow(new IOException());
+
+        service.getAllMetrics();
+    }
+
+    @Test
+    public void testGetMetricValuesSuccess() throws IOException {
+        MetricValue value = new MetricValue("jobName", 1L, new HashMap<>());
+        given(metricStore.getMetricValues(Matchers.anyString(), 
Matchers.anyInt(), Matchers.anyInt()))
+                .willReturn(Collections.singletonList(value));
+
+        List<MetricValue> values = service.getMetricValues("jobName", 0, 300);
+        assertEquals(values.size(), 1);
+        assertEquals(values.get(0).getName(), "jobName");
+    }
+
+    @Test(expected = GriffinException.ServiceException.class)
+    public void testGetMetricValuesFailureWithException() throws IOException {
+        given(metricStore.getMetricValues(Matchers.anyString(), 
Matchers.anyInt(), Matchers.anyInt()))
+                .willThrow(new IOException());
+
+        service.getMetricValues("jobName", 0, 300);
+    }
+
+    @Test
+    public void testAddMetricValuesSuccess() throws IOException {
+        Map<String, Object> value = new HashMap<>();
+        value.put("total", 10000);
+        value.put("matched", 10000);
+        List<MetricValue> values = Collections.singletonList(
+                new MetricValue("jobName", 1L, value));
+        given(metricStore.addMetricValues(values))
+                .willReturn(new ResponseEntity<>("{\"errors\": false, 
\"items\": []}", HttpStatus.OK));
+
+        ResponseEntity response = service.addMetricValues(values);
+        Map body = JsonUtil.toEntity(response.getBody().toString(), Map.class);
+        assertEquals(response.getStatusCode(), HttpStatus.OK);
+        assertNotNull(body);
+        assertEquals(body.get("errors").toString(), "false");
+    }
+
+    @Test(expected = GriffinException.BadRequestException.class)
+    public void testAddMetricValuesFailureWithInvalidFormat() {
+        List<MetricValue> values = Collections.singletonList(new 
MetricValue());
+
+        service.addMetricValues(values);
+    }
+
+    @Test(expected = GriffinException.ServiceException.class)
+    public void testAddMetricValuesFailureWithException() throws IOException {
+        Map<String, Object> value = new HashMap<>();
+        value.put("total", 10000);
+        value.put("matched", 10000);
+        List<MetricValue> values = Collections.singletonList(new 
MetricValue("jobName", 1L, value));
+        given(metricStore.addMetricValues(values)).willThrow(new 
IOException());
+
+        service.addMetricValues(values);
+    }
+
+    @Test
+    public void testDeleteMetricValuesSuccess() throws IOException {
+
+        given(metricStore.deleteMetricValues("metricName"))
+                .willReturn(new ResponseEntity<>("{\"failures\": []}", 
HttpStatus.OK));
+
+        ResponseEntity response = service.deleteMetricValues("metricName");
+        Map body = JsonUtil.toEntity(response.getBody().toString(), Map.class);
+        assertEquals(response.getStatusCode(), HttpStatus.OK);
+        assertNotNull(body);
+        assertEquals(body.get("failures"), Collections.emptyList());
+    }
+
+    @Test(expected = GriffinException.ServiceException.class)
+    public void testDeleteMetricValuesFailureWithException() throws 
IOException {
+        given(metricStore.deleteMetricValues("metricName")).willThrow(new 
IOException());
+
+        service.deleteMetricValues("metricName");
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-griffin/blob/6b1a7000/service/src/test/java/org/apache/griffin/core/util/EntityHelper.java
----------------------------------------------------------------------
diff --git 
a/service/src/test/java/org/apache/griffin/core/util/EntityHelper.java 
b/service/src/test/java/org/apache/griffin/core/util/EntityHelper.java
index 1805608..ebb8980 100644
--- a/service/src/test/java/org/apache/griffin/core/util/EntityHelper.java
+++ b/service/src/test/java/org/apache/griffin/core/util/EntityHelper.java
@@ -169,4 +169,9 @@ public class EntityHelper {
         return trigger;
     }
 
+    public static GriffinJob createGriffinJob() {
+        return new GriffinJob(1L, 1L, "jobName",
+                "quartzJobName", "quartzGroupName", false);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-griffin/blob/6b1a7000/service/src/test/java/org/apache/griffin/core/util/TimeUtilTest.java
----------------------------------------------------------------------
diff --git 
a/service/src/test/java/org/apache/griffin/core/util/TimeUtilTest.java 
b/service/src/test/java/org/apache/griffin/core/util/TimeUtilTest.java
index b215f93..a4b722d 100644
--- a/service/src/test/java/org/apache/griffin/core/util/TimeUtilTest.java
+++ b/service/src/test/java/org/apache/griffin/core/util/TimeUtilTest.java
@@ -30,88 +30,89 @@ public class TimeUtilTest {
 
 
     @Test
-    public void testStr2LongWithPositive() throws Exception {
+    public void testStr2LongWithPositive() {
         String time = "2h3m4s";
         assertEquals(String.valueOf(TimeUtil.str2Long(time)), "7384000");
     }
 
     @Test
-    public void testStr2LongWithNegative() throws Exception {
+    public void testStr2LongWithNegative() {
         String time = "-2h3m4s";
         assertEquals(String.valueOf(TimeUtil.str2Long(time)), "-7384000");
     }
 
     @Test
-    public void testStr2LongWithNull() throws Exception {
+    public void testStr2LongWithNull() {
         String time = null;
         assertEquals(String.valueOf(TimeUtil.str2Long(time)), "0");
     }
 
     @Test
-    public void testStr2LongWithDay() throws Exception {
+    public void testStr2LongWithDay() {
         String time = "1d";
         System.out.println(TimeUtil.str2Long(time));
         assertEquals(String.valueOf(TimeUtil.str2Long(time)), "86400000");
     }
+
     @Test
-    public void testStr2LongWithHour() throws Exception {
+    public void testStr2LongWithHour() {
         String time = "1h";
         assertEquals(String.valueOf(TimeUtil.str2Long(time)), "3600000");
     }
 
     @Test
-    public void testStr2LongWithMinute() throws Exception {
+    public void testStr2LongWithMinute() {
         String time = "1m";
         assertEquals(String.valueOf(TimeUtil.str2Long(time)), "60000");
     }
 
     @Test
-    public void testStr2LongWithSecond() throws Exception {
+    public void testStr2LongWithSecond() {
         String time = "1s";
         assertEquals(String.valueOf(TimeUtil.str2Long(time)), "1000");
     }
 
     @Test
-    public void testStr2LongWithMillisecond() throws Exception {
+    public void testStr2LongWithMillisecond() {
         String time = "1ms";
         assertEquals(String.valueOf(TimeUtil.str2Long(time)), "1");
     }
 
     @Test
-    public void testStr2LongWithIllegalFormat() throws Exception {
+    public void testStr2LongWithIllegalFormat() {
         String time = "1y2m3s";
         assertEquals(String.valueOf(TimeUtil.str2Long(time)), "123000");
     }
 
     @Test
-    public void testFormat() throws Exception {
+    public void testFormat() {
         String format = "dt=#YYYYMMdd#";
         Long time = 1516186620155L;
         String timeZone = "GMT+8:00";
-        assertEquals(TimeUtil.format(format,time,timeZone),"dt=20180117");
+        assertEquals(TimeUtil.format(format, time, timeZone), "dt=20180117");
     }
 
     @Test
-    public void testFormatWithDiff() throws Exception {
+    public void testFormatWithDiff() {
         String format = "dt=#YYYYMMdd#/hour=#HH#";
         Long time = 1516186620155L;
         String timeZone = "GMT+8:00";
-        
assertEquals(TimeUtil.format(format,time,timeZone),"dt=20180117/hour=18");
+        assertEquals(TimeUtil.format(format, time, timeZone), 
"dt=20180117/hour=18");
     }
 
     @Test
-    public void testFormatWithIllegalException() throws Exception {
+    public void testFormatWithIllegalException() {
         String format = "\\#YYYYMMdd\\#";
         Long time = 1516186620155L;
         String timeZone = "GMT+8:00";
-        IllegalArgumentException exception = formatException(format, 
time,timeZone);
+        IllegalArgumentException exception = formatException(format, time, 
timeZone);
         assert exception != null;
     }
 
-    private IllegalArgumentException formatException(String format,Long 
time,String timeZone) {
+    private IllegalArgumentException formatException(String format, Long time, 
String timeZone) {
         IllegalArgumentException exception = null;
         try {
-            TimeUtil.format(format,time,timeZone);
+            TimeUtil.format(format, time, timeZone);
         } catch (IllegalArgumentException e) {
             exception = e;
         }

Reply via email to