62358826766e68823ab80aa3f66864213301e8a8
[policy/apex-pdp.git] / examples / examples-adaptive / src / test / java / org / onap / policy / apex / examples / adaptive / AnomalyDetectionConceptTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.examples.adaptive;
22
23 import static org.junit.Assert.*;
24
25 import java.util.LinkedList;
26 import java.util.List;
27
28 import org.junit.Test;
29 import org.onap.policy.apex.examples.adaptive.concepts.AnomalyDetection;
30
31 public class AnomalyDetectionConceptTest {
32
33     @Test
34     public void testToString(){
35         AnomalyDetection anomalyDetection = new AnomalyDetection();
36         List<Double> newAnomalyScores = new LinkedList<>();
37         newAnomalyScores.add((double) 55);
38         anomalyDetection.setAnomalyScores(newAnomalyScores);
39         anomalyDetection.setFrequency(55);
40         assertEquals(newAnomalyScores, anomalyDetection.getAnomalyScores());
41         assertTrue(anomalyDetection.checkSetAnomalyScores());
42         assertEquals(55,anomalyDetection.getFrequency());
43         assertEquals(true,anomalyDetection.getFirstRound());
44         assertEquals("AnomalyDetection [firstRound=true, frequency=55, anomalyScores=[55.0], frequencyForecasted=null]", anomalyDetection.toString());
45     }
46
47     @Test
48     public void testHashCode(){
49         AnomalyDetection detection = new AnomalyDetection();
50         AnomalyDetection compareDetection = new AnomalyDetection();
51         assertEquals(detection.hashCode(), compareDetection.hashCode());
52         detection.init(1);
53         assertTrue(detection.isInitialized());
54         assertFalse(compareDetection.isInitialized());
55         compareDetection.setAnomalyScores(null);
56         compareDetection.setFirstRound(false);
57         compareDetection.setFrequencyForecasted(new LinkedList<>());
58         assertNotEquals(detection.hashCode(), compareDetection.hashCode());
59     }
60
61     @Test
62     public void testEquals() {
63         AnomalyDetection anomalyDetection = new AnomalyDetection();
64         AnomalyDetection comparisonDetection = new AnomalyDetection();
65         assertTrue(anomalyDetection.equals(comparisonDetection));
66         //Compare object to itself
67         assertTrue(anomalyDetection.equals(anomalyDetection));
68         //Compare object to null
69         assertFalse(anomalyDetection.equals(null));
70         //compare object to string
71         assertFalse(anomalyDetection.equals("test"));
72         // Anomaly Scores comparison
73         anomalyDetection.setAnomalyScores(null);
74         assertFalse(anomalyDetection.equals(comparisonDetection));
75         comparisonDetection.setAnomalyScores(null);
76         assertTrue(anomalyDetection.equals(comparisonDetection));
77         List<Double> anomalyScores = new LinkedList<>();
78         anomalyScores.add((double) 20);
79         anomalyDetection.setAnomalyScores(anomalyScores);
80         assertFalse(anomalyDetection.equals(comparisonDetection));
81         comparisonDetection.setAnomalyScores(anomalyScores);
82         assertTrue(anomalyDetection.checkSetAnomalyScores());
83         //First Round Checks
84         anomalyDetection.setFirstRound(false);
85         assertFalse(anomalyDetection.equals(comparisonDetection));
86         anomalyDetection.setFirstRound(true);
87         //Frequency Checks
88         anomalyDetection.setFrequency(55);
89         assertFalse(anomalyDetection.equals(comparisonDetection));
90         anomalyDetection.setFrequency(0);
91         //FrequencyForecasted Checks
92         List<Double> comparisonFrequency = new LinkedList<>();
93         comparisonDetection.setFrequencyForecasted(comparisonFrequency);
94         assertFalse(anomalyDetection.equals(comparisonDetection));
95         anomalyDetection.setFrequencyForecasted(anomalyScores);
96         assertFalse(anomalyDetection.equals(comparisonDetection));
97         anomalyDetection.setFrequencyForecasted(comparisonFrequency);
98         assertTrue(anomalyDetection.equals(comparisonDetection));
99     }
100
101     @Test
102     public void testCheckSets(){
103         AnomalyDetection anomalyDetection = new AnomalyDetection();
104         assertFalse(anomalyDetection.checkSetAnomalyScores());
105         List<Double> anomalyScores = new LinkedList<>();
106         anomalyDetection.setAnomalyScores(anomalyScores);
107         assertFalse(anomalyDetection.checkSetAnomalyScores());
108         anomalyScores.add((double)2);
109         anomalyDetection.setAnomalyScores(anomalyScores);
110         assertTrue(anomalyDetection.checkSetAnomalyScores());
111         anomalyDetection.unsetAnomalyScores();
112         assertFalse(anomalyDetection.checkSetAnomalyScores());
113         assertEquals(null, anomalyDetection.getFrequencyForecasted());
114         assertFalse(anomalyDetection.checkSetFrequencyForecasted());
115         List<Double> frequencyForecasted = new LinkedList<>();
116         anomalyDetection.setFrequencyForecasted(frequencyForecasted);
117         assertFalse(anomalyDetection.checkSetFrequencyForecasted());
118         frequencyForecasted.add((double)2);
119         anomalyDetection.setFrequencyForecasted(frequencyForecasted);
120         assertTrue(anomalyDetection.checkSetFrequencyForecasted());
121         anomalyDetection.unsetFrequencyForecasted();
122         assertFalse(anomalyDetection.checkSetFrequencyForecasted());
123     }
124 }