|
18 | 18 |
|
19 | 19 | import static org.junit.Assert.assertEquals;
|
20 | 20 | import static org.junit.Assert.assertFalse;
|
| 21 | +import static org.junit.Assert.assertTrue; |
21 | 22 |
|
22 | 23 | import com.fasterxml.jackson.core.JsonProcessingException;
|
23 | 24 | import com.fasterxml.jackson.core.type.TypeReference;
|
24 | 25 | import com.fasterxml.jackson.databind.json.JsonMapper;
|
25 | 26 | import java.util.ArrayList;
|
| 27 | +import java.util.Arrays; |
26 | 28 | import java.util.List;
|
27 | 29 | import java.util.Map;
|
28 | 30 | import org.junit.Test;
|
| 31 | +import software.amazon.cloudwatchlogs.emf.Constants; |
29 | 32 |
|
30 | 33 | public class MetricsContextTest {
|
31 | 34 |
|
@@ -75,6 +78,59 @@ public void testSerializeMoreThen100Metrics() throws JsonProcessingException {
|
75 | 78 | }
|
76 | 79 | }
|
77 | 80 |
|
| 81 | + @Test |
| 82 | + public void testSerializeAMetricWith101DataPoints() throws JsonProcessingException { |
| 83 | + MetricsContext mc = new MetricsContext(); |
| 84 | + int dataPointCount = 101; |
| 85 | + int expectedEventCount = 2; |
| 86 | + String metricName = "metric"; |
| 87 | + for (int i = 0; i < dataPointCount; i++) { |
| 88 | + mc.putMetric(metricName, i); |
| 89 | + } |
| 90 | + |
| 91 | + List<String> events = mc.serialize(); |
| 92 | + assertEquals(expectedEventCount, events.size()); |
| 93 | + List<MetricDefinition> allMetrics = new ArrayList<>(); |
| 94 | + for (String event : events) { |
| 95 | + allMetrics.addAll(parseMetrics(event)); |
| 96 | + } |
| 97 | + List<Double> expectedValues = new ArrayList<>(); |
| 98 | + for (int i = 0; i < Constants.MAX_DATAPOINTS_PER_METRIC; i++) { |
| 99 | + expectedValues.add((double) i); |
| 100 | + } |
| 101 | + assertEquals(expectedValues, allMetrics.get(0).getValues()); |
| 102 | + assertTrue(allMetrics.get(1).getValues().equals(Arrays.asList(100.0))); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testSerializeMetricsWith101DataPoints() throws JsonProcessingException { |
| 107 | + MetricsContext mc = new MetricsContext(); |
| 108 | + int dataPointCount = 101; |
| 109 | + int expectedEventCount = 2; |
| 110 | + String metricName = "metric1"; |
| 111 | + for (int i = 0; i < dataPointCount; i++) { |
| 112 | + mc.putMetric(metricName, i); |
| 113 | + } |
| 114 | + mc.putMetric("metric2", 2); |
| 115 | + |
| 116 | + List<String> events = mc.serialize(); |
| 117 | + assertEquals(expectedEventCount, events.size()); |
| 118 | + |
| 119 | + List<MetricDefinition> metricsFromEvent1 = parseMetrics(events.get(0)); |
| 120 | + List<MetricDefinition> metricsFromEvent2 = parseMetrics(events.get(1)); |
| 121 | + |
| 122 | + assertEquals(2, metricsFromEvent1.size()); |
| 123 | + List<Double> expectedValues = new ArrayList<>(); |
| 124 | + for (int i = 0; i < Constants.MAX_DATAPOINTS_PER_METRIC; i++) { |
| 125 | + expectedValues.add((double) i); |
| 126 | + } |
| 127 | + assertEquals(expectedValues, metricsFromEvent1.get(0).getValues()); |
| 128 | + assertEquals(Arrays.asList(2.0), metricsFromEvent1.get(1).getValues()); |
| 129 | + |
| 130 | + assertEquals(1, metricsFromEvent2.size()); |
| 131 | + assertEquals(Arrays.asList(100.0), metricsFromEvent2.get(0).getValues()); |
| 132 | + } |
| 133 | + |
78 | 134 | @Test
|
79 | 135 | public void testSerializeZeroMetric() throws JsonProcessingException {
|
80 | 136 | MetricsContext mc = new MetricsContext();
|
@@ -106,8 +162,12 @@ private ArrayList<MetricDefinition> parseMetrics(String event) throws JsonProces
|
106 | 162 | for (Map<String, String> metric : metrics) {
|
107 | 163 | String name = metric.get("Name");
|
108 | 164 | Unit unit = Unit.fromValue(metric.get("Unit"));
|
109 |
| - double value = (double) metadata_map.get(name); |
110 |
| - metricDefinitions.add(new MetricDefinition(name, unit, value)); |
| 165 | + Object value = metadata_map.get(name); |
| 166 | + if (value instanceof ArrayList) { |
| 167 | + metricDefinitions.add(new MetricDefinition(name, unit, (ArrayList) value)); |
| 168 | + } else { |
| 169 | + metricDefinitions.add(new MetricDefinition(name, unit, (double) value)); |
| 170 | + } |
111 | 171 | }
|
112 | 172 | return metricDefinitions;
|
113 | 173 | }
|
|
0 commit comments