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