1237537fd801175b19305c65719f6e5858fd7dfc
[policy/apex-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.examples.adaptive;
24
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;
30
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;
35
36 public class AnomalyDetectionConceptTest {
37
38     @Test
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());
51     }
52
53     @Test
54     public void testHashCode() {
55         AnomalyDetection detection = new AnomalyDetection();
56         AnomalyDetection compareDetection = new AnomalyDetection();
57         assertEquals(detection.hashCode(), compareDetection.hashCode());
58         detection.init(1);
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());
65     }
66
67     @Test
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());
89         //First Round Checks
90         anomalyDetection.setFirstRound(false);
91         assertNotEquals(anomalyDetection, comparisonDetection);
92         anomalyDetection.setFirstRound(true);
93         //Frequency Checks
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);
105     }
106
107     @Test
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());
129     }
130 }