2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.examples.adaptive;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertTrue;
28 import java.util.LinkedList;
29 import java.util.List;
30 import org.junit.Test;
31 import org.onap.policy.apex.examples.adaptive.concepts.AnomalyDetection;
33 public class AnomalyDetectionConceptTest {
36 public void testToString() {
37 AnomalyDetection anomalyDetection = new AnomalyDetection();
38 List<Double> newAnomalyScores = new LinkedList<>();
39 newAnomalyScores.add((double) 55);
40 anomalyDetection.setAnomalyScores(newAnomalyScores);
41 anomalyDetection.setFrequency(55);
42 assertEquals(newAnomalyScores, anomalyDetection.getAnomalyScores());
43 assertTrue(anomalyDetection.checkSetAnomalyScores());
44 assertEquals(55, anomalyDetection.getFrequency());
45 assertEquals(true, anomalyDetection.getFirstRound());
46 assertEquals("AnomalyDetection [firstRound=true, frequency=55, anomalyScores=[55.0], frequencyForecasted=null]",
47 anomalyDetection.toString());
51 public void testHashCode() {
52 AnomalyDetection detection = new AnomalyDetection();
53 AnomalyDetection compareDetection = new AnomalyDetection();
54 assertEquals(detection.hashCode(), compareDetection.hashCode());
56 assertTrue(detection.isInitialized());
57 assertFalse(compareDetection.isInitialized());
58 compareDetection.setAnomalyScores(null);
59 compareDetection.setFirstRound(false);
60 compareDetection.setFrequencyForecasted(new LinkedList<>());
61 assertNotEquals(detection.hashCode(), compareDetection.hashCode());
65 public void testEquals() {
66 AnomalyDetection anomalyDetection = new AnomalyDetection();
67 AnomalyDetection comparisonDetection = new AnomalyDetection();
68 assertTrue(anomalyDetection.equals(comparisonDetection));
69 //Compare object to itself
70 assertTrue(anomalyDetection.equals(anomalyDetection));
71 //Compare object to null
72 assertFalse(anomalyDetection.equals(null));
73 //compare object to string
74 assertFalse(anomalyDetection.equals("test"));
75 // Anomaly Scores comparison
76 anomalyDetection.setAnomalyScores(null);
77 assertFalse(anomalyDetection.equals(comparisonDetection));
78 comparisonDetection.setAnomalyScores(null);
79 assertTrue(anomalyDetection.equals(comparisonDetection));
80 List<Double> anomalyScores = new LinkedList<>();
81 anomalyScores.add((double) 20);
82 anomalyDetection.setAnomalyScores(anomalyScores);
83 assertFalse(anomalyDetection.equals(comparisonDetection));
84 comparisonDetection.setAnomalyScores(anomalyScores);
85 assertTrue(anomalyDetection.checkSetAnomalyScores());
87 anomalyDetection.setFirstRound(false);
88 assertFalse(anomalyDetection.equals(comparisonDetection));
89 anomalyDetection.setFirstRound(true);
91 anomalyDetection.setFrequency(55);
92 assertFalse(anomalyDetection.equals(comparisonDetection));
93 anomalyDetection.setFrequency(0);
94 //FrequencyForecasted Checks
95 List<Double> comparisonFrequency = new LinkedList<>();
96 comparisonDetection.setFrequencyForecasted(comparisonFrequency);
97 assertFalse(anomalyDetection.equals(comparisonDetection));
98 anomalyDetection.setFrequencyForecasted(anomalyScores);
99 assertFalse(anomalyDetection.equals(comparisonDetection));
100 anomalyDetection.setFrequencyForecasted(comparisonFrequency);
101 assertTrue(anomalyDetection.equals(comparisonDetection));
105 public void testCheckSets() {
106 AnomalyDetection anomalyDetection = new AnomalyDetection();
107 assertFalse(anomalyDetection.checkSetAnomalyScores());
108 List<Double> anomalyScores = new LinkedList<>();
109 anomalyDetection.setAnomalyScores(anomalyScores);
110 assertFalse(anomalyDetection.checkSetAnomalyScores());
111 anomalyScores.add((double) 2);
112 anomalyDetection.setAnomalyScores(anomalyScores);
113 assertTrue(anomalyDetection.checkSetAnomalyScores());
114 anomalyDetection.unsetAnomalyScores();
115 assertFalse(anomalyDetection.checkSetAnomalyScores());
116 assertEquals(null, anomalyDetection.getFrequencyForecasted());
117 assertFalse(anomalyDetection.checkSetFrequencyForecasted());
118 List<Double> frequencyForecasted = new LinkedList<>();
119 anomalyDetection.setFrequencyForecasted(frequencyForecasted);
120 assertFalse(anomalyDetection.checkSetFrequencyForecasted());
121 frequencyForecasted.add((double) 2);
122 anomalyDetection.setFrequencyForecasted(frequencyForecasted);
123 assertTrue(anomalyDetection.checkSetFrequencyForecasted());
124 anomalyDetection.unsetFrequencyForecasted();
125 assertFalse(anomalyDetection.checkSetFrequencyForecasted());