Removing deprecated DMAAP library
[policy/drools-pdp.git] / policy-utils / src / test / java / org / onap / policy / drools / utils / ReferenceTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.utils;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import org.junit.jupiter.api.Test;
30
31 class ReferenceTest {
32
33     @Test
34     void testReference() {
35         Reference<Integer> val = new Reference<>(null);
36         assertNull(val.get());
37
38         val = new Reference<>(10);
39         assertEquals(10, val.get().intValue());
40     }
41
42     @Test
43     void testGet_testSet() {
44         Reference<Integer> val = new Reference<>(null);
45         assertNull(val.get());
46
47         val.set(20);
48         assertEquals(20, val.get().intValue());
49
50         val.set(30);
51         assertEquals(30, val.get().intValue());
52     }
53
54     @Test
55     void testCompareAndSet() {
56         Reference<Integer> val = new Reference<>(null);
57
58         Integer valCompare = 100;
59
60         // try an incorrect value - should fail and leave value unchanged
61         assertFalse(val.compareAndSet(500, valCompare));
62         assertNull(val.get());
63
64         assertTrue(val.compareAndSet(null, valCompare));
65         assertEquals(valCompare, val.get());
66
67         // try an incorrect value - should fail and leave value unchanged
68         Integer v2 = 200;
69         assertFalse(val.compareAndSet(600, v2));
70         assertEquals(valCompare, val.get());
71
72         // now try again, this time with the correct value
73         assertTrue(val.compareAndSet(valCompare, v2));
74         assertEquals(v2, val.get());
75
76         Integer v3 = 300;
77         assertTrue(val.compareAndSet(v2, v3));
78         assertEquals(v3, val.get());
79
80         // try setting it back to null
81         assertTrue(val.compareAndSet(v3, null));
82         assertNull(val.get());
83     }
84
85 }