5b9856f41fab2591175b7e8bf22ef093ec330e4a
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertTrue;
27
28 import org.junit.Before;
29 import org.junit.Test;
30
31 public class DelayedIdentStringTest {
32
33     private int countToStringCalls;
34     private Object object;
35     private DelayedIdentString delay;
36
37     /**
38      * Initializes fields, including {@link #delay}.
39      */
40     @Before
41     public void setUp() {
42         countToStringCalls = 0;
43
44         object = new Object() {
45             @Override
46             public String toString() {
47                 ++countToStringCalls;
48                 return super.toString();
49             }
50         };
51
52         delay = new DelayedIdentString(object);
53     }
54
55     @Test
56     public void testToString() {
57         String delayed = delay.toString();
58         assertEquals(1, countToStringCalls);
59
60         String real = object.toString();
61         assertNotEquals(real, delayed);
62
63         assertThat(delayed).startsWith("@");
64         assertTrue(delayed.length() > 1);
65
66         // test case where the object is null
67         assertEquals(DelayedIdentString.NULL_STRING, new DelayedIdentString(null).toString());
68
69         // test case where the object returns null from toString()
70         object = new Object() {
71             @Override
72             public String toString() {
73                 return null;
74             }
75         };
76         assertEquals(DelayedIdentString.NULL_STRING, new DelayedIdentString(object).toString());
77
78         // test case where the object's toString() does not include "@"
79         object = new Object() {
80             @Override
81             public String toString() {
82                 return "some text";
83             }
84         };
85         assertEquals(object.toString(), new DelayedIdentString(object).toString());
86     }
87
88     @Test
89     public void testDelayedIdentString() {
90         // should not have called the object's toString() method yet
91         assertEquals(0, countToStringCalls);
92     }
93 }