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