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