b2dcfb52d30ddd57ffef992fe961c6a413576782
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-session-persistence
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.drools.persistence;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Date;
28
29 import org.junit.Test;
30 import org.onap.policy.drools.persistence.DroolsSessionEntity;
31
32 public class DroolsSessionEntityTest {
33
34     @Test
35     public void testHashCode() {
36         DroolsSessionEntity entity = makeEnt("mynameA", 1);
37
38         DroolsSessionEntity e2 = makeEnt("mynameA", 2);
39
40         // session id is not part of hash code
41         assertTrue(entity.hashCode() == e2.hashCode());
42
43         // diff sess name
44         e2 = makeEnt("mynameB", 1);
45         assertTrue(entity.hashCode() != e2.hashCode());
46     }
47
48     /** Ensures that hashCode() functions as expected when the getXxx methods are overridden. */
49     @Test
50     public void testHashCode_Subclass() {
51         DroolsSessionEntity entity = makeEnt2("mynameA", 1);
52
53         DroolsSessionEntity e2 = makeEnt("mynameA", 2);
54
55         // session id is not part of hash code
56         assertTrue(entity.hashCode() == e2.hashCode());
57
58         // diff sess name
59         e2 = makeEnt("mynameB", 1);
60         assertTrue(entity.hashCode() != e2.hashCode());
61     }
62
63     @Test
64     public void testGetSessionName_testSetSessionName() {
65         DroolsSessionEntity entity = makeEnt("mynameZ", 1);
66
67         assertEquals("mynameZ", entity.getSessionName());
68
69         entity.setSessionName("another");
70         assertEquals("another", entity.getSessionName());
71
72         // others unchanged
73         assertEquals(1, entity.getSessionId());
74     }
75
76     @Test
77     public void testGetSessionId_testSetSessionId() {
78         DroolsSessionEntity entity = makeEnt("mynameA", 1);
79
80         assertEquals(1, entity.getSessionId());
81
82         entity.setSessionId(20);
83         assertEquals(20, entity.getSessionId());
84
85         // others unchanged
86         assertEquals("mynameA", entity.getSessionName());
87     }
88
89     @Test
90     public void testGetCreatedDate_testSetCreatedDate_testGetUpdatedDate_testSetUpdatedDate() {
91         DroolsSessionEntity entity = new DroolsSessionEntity();
92
93         Date crtdt = new Date(System.currentTimeMillis() - 100);
94         entity.setCreatedDate(crtdt);
95
96         Date updt = new Date(System.currentTimeMillis() - 200);
97         entity.setUpdatedDate(updt);
98
99         assertEquals(crtdt, entity.getCreatedDate());
100         assertEquals(updt, entity.getUpdatedDate());
101     }
102
103     @Test
104     public void testEqualsObject() {
105         DroolsSessionEntity entity = makeEnt("mynameA", 1);
106
107         // reflexive
108         assertTrue(entity.equals(entity));
109
110         DroolsSessionEntity e2 = makeEnt("mynameA", 2);
111
112         // session id is not part of hash code
113         assertTrue(entity.equals(e2));
114         assertTrue(entity.equals(e2));
115
116         // diff sess name
117         e2 = makeEnt("mynameB", 1);
118         assertFalse(entity.equals(e2));
119         assertFalse(entity.equals(e2));
120     }
121
122     /** Ensures that equals() functions as expected when the getXxx methods are overridden. */
123     @Test
124     public void testEqualsObject_Subclass() {
125         DroolsSessionEntity entity = makeEnt2("mynameA", 1);
126
127         // reflexive
128         assertTrue(entity.equals(entity));
129
130         DroolsSessionEntity e2 = makeEnt("mynameA", 2);
131
132         // session id is not part of hash code
133         assertTrue(entity.equals(e2));
134         assertTrue(entity.equals(e2));
135
136         // diff sess name
137         e2 = makeEnt("mynameB", 1);
138         assertFalse(entity.equals(e2));
139         assertFalse(entity.equals(e2));
140     }
141
142     @Test
143     public void testToString() {
144         DroolsSessionEntity entity = makeEnt("mynameA", 23);
145
146         assertEquals("{name=mynameA, id=23}", entity.toString());
147     }
148
149     /**
150      * Makes a session Entity. The parameters are stored into the Entity object via the setXxx
151      * methods.
152      *
153      * @param sessnm session name
154      * @param sessid session id
155      * @return a new session Entity
156      */
157     private DroolsSessionEntity makeEnt(String sessnm, long sessid) {
158
159         DroolsSessionEntity entity = new DroolsSessionEntity();
160
161         entity.setSessionName(sessnm);
162         entity.setSessionId(sessid);
163
164         return entity;
165     }
166
167     /**
168      * Makes a session Entity that overrides the getXxx methods. The parameters that are provided are
169      * returned by the overridden methods, but they are <i>not</i> stored into the Entity object via
170      * the setXxx methods.
171      *
172      * @param sessnm session name
173      * @param sessid session id
174      * @return a new session Entity
175      */
176     @SuppressWarnings("serial")
177     private DroolsSessionEntity makeEnt2(String sessnm, long sessid) {
178
179         return new DroolsSessionEntity() {
180
181             @Override
182             public String getSessionName() {
183                 return sessnm;
184             }
185
186             @Override
187             public long getSessionId() {
188                 return sessid;
189             }
190         };
191     }
192 }