Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.ui.chart; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 20 | import static org.junit.Assert.assertEquals; |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Unit tests for {@link ChartModel}. |
| 24 | */ |
| 25 | public class ChartModelTest { |
| 26 | |
| 27 | private static final String FOO = "foo"; |
| 28 | private static final String BAR = "bar"; |
| 29 | private static final String ZOO = "zoo"; |
| 30 | |
| 31 | private static final Double[] VALUES1 = {0D, 1D, 2D}; |
| 32 | private static final Double[] VALUES2 = {3D, 4D, 5D}; |
| 33 | private static final Double[] VALUES3 = {6D, 7D, 8D}; |
| 34 | |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 35 | private ChartModel cm; |
| 36 | |
| 37 | @Test(expected = NullPointerException.class) |
| 38 | public void guardAgainstNullSeries() { |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 39 | cm = new ChartModel(null); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | @Test |
| 43 | public void testSeriesCount() { |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 44 | cm = new ChartModel(FOO, BAR, ZOO); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 45 | assertEquals("Wrong series count", 3, cm.seriesCount()); |
| 46 | } |
| 47 | |
| 48 | @Test |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 49 | public void emptyLabel() { |
| 50 | cm = new ChartModel(FOO, BAR, ZOO); |
| 51 | cm.addDataPoint(System.currentTimeMillis()); |
| 52 | |
| 53 | assertEquals("bad data point count", 1, cm.dataPointCount()); |
| 54 | } |
| 55 | |
| 56 | @Test(expected = IllegalArgumentException.class) |
| 57 | public void dataPointBandSeries() { |
| 58 | cm = new ChartModel(FOO, BAR); |
| 59 | |
| 60 | cm.addDataPoint(System.currentTimeMillis()) |
| 61 | .data(ZOO, VALUES3[0]); |
| 62 | } |
| 63 | |
| 64 | @Test |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 65 | public void testAddDataPoint() { |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 66 | cm = new ChartModel(FOO, BAR, ZOO); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 67 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 68 | long time = System.currentTimeMillis(); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 69 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 70 | cm.addDataPoint(time) |
| 71 | .data(FOO, VALUES1[0]) |
| 72 | .data(BAR, VALUES2[0]) |
| 73 | .data(ZOO, VALUES3[0]); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 74 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 75 | cm.addDataPoint(time + 1) |
| 76 | .data(FOO, VALUES1[1]) |
| 77 | .data(BAR, VALUES2[1]) |
| 78 | .data(ZOO, VALUES3[1]); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 79 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 80 | cm.addDataPoint(time + 2) |
| 81 | .data(FOO, VALUES1[2]) |
| 82 | .data(BAR, VALUES2[2]) |
| 83 | .data(ZOO, VALUES3[2]); |
| 84 | |
| 85 | assertEquals("Wrong result", 3, cm.getDataPoints()[0].size()); |
| 86 | assertEquals("Wrong result", 3, cm.getDataPoints()[1].size()); |
| 87 | assertEquals("Wrong result", 3, cm.getDataPoints()[2].size()); |
| 88 | assertEquals("Wrong result", 3, cm.getDataPoints().length); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Test |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 92 | public void testGetDataPoint() { |
| 93 | cm = new ChartModel(FOO, BAR); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 94 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 95 | long time = System.currentTimeMillis(); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 96 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 97 | cm.addDataPoint(time) |
| 98 | .data(FOO, VALUES1[0]) |
| 99 | .data(BAR, VALUES2[0]); |
| 100 | |
| 101 | cm.addDataPoint(time + 1) |
| 102 | .data(FOO, VALUES1[1]) |
| 103 | .data(BAR, VALUES2[1]); |
| 104 | |
| 105 | assertEquals("Wrong result", (Double) 0D, cm.getDataPoints()[0].get(FOO)); |
| 106 | assertEquals("Wrong result", (Double) 1D, cm.getDataPoints()[1].get(FOO)); |
| 107 | assertEquals("Wrong result", (Double) 3D, cm.getDataPoints()[0].get(BAR)); |
| 108 | assertEquals("Wrong result", (Double) 4D, cm.getDataPoints()[1].get(BAR)); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Test |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 112 | public void testGetLastDataPoint() { |
| 113 | cm = new ChartModel(FOO, BAR); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 114 | |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 115 | long time = System.currentTimeMillis(); |
| 116 | |
| 117 | cm.addDataPoint(time) |
| 118 | .data(FOO, VALUES1[0]) |
| 119 | .data(BAR, VALUES2[0]); |
| 120 | |
| 121 | cm.addDataPoint(time + 1) |
| 122 | .data(FOO, VALUES1[1]) |
| 123 | .data(BAR, VALUES2[1]); |
| 124 | |
| 125 | assertEquals("Wrong result", VALUES1[1], cm.getLastDataPoint().get(FOO)); |
| 126 | assertEquals("Wrong result", VALUES2[1], cm.getLastDataPoint().get(BAR)); |
Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 127 | } |
| 128 | } |