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