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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.persistence;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.util.Date;
29 import org.junit.Test;
30 import org.onap.policy.drools.persistence.DroolsSessionEntity;
32 public class DroolsSessionEntityTest {
35 public void testHashCode() {
36 DroolsSessionEntity e = makeEnt("mynameA", 1);
38 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
40 // session id is not part of hash code
41 assertTrue(e.hashCode() == e2.hashCode());
44 e2 = makeEnt("mynameB", 1);
45 assertTrue(e.hashCode() != e2.hashCode());
49 * Ensures that hashCode() functions as expected when the getXxx methods are
53 public void testHashCode_Subclass() {
54 DroolsSessionEntity e = makeEnt2("mynameA", 1);
56 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
58 // session id is not part of hash code
59 assertTrue(e.hashCode() == e2.hashCode());
62 e2 = makeEnt("mynameB", 1);
63 assertTrue(e.hashCode() != e2.hashCode());
67 public void testGetSessionName_testSetSessionName() {
68 DroolsSessionEntity e = makeEnt("mynameZ", 1);
70 assertEquals("mynameZ", e.getSessionName());
72 e.setSessionName("another");
73 assertEquals("another", e.getSessionName());
76 assertEquals(1, e.getSessionId());
80 public void testGetSessionId_testSetSessionId() {
81 DroolsSessionEntity e = makeEnt("mynameA", 1);
83 assertEquals(1, e.getSessionId());
86 assertEquals(20, e.getSessionId());
89 assertEquals("mynameA", e.getSessionName());
93 public void testGetCreatedDate_testSetCreatedDate_testGetUpdatedDate_testSetUpdatedDate() {
94 DroolsSessionEntity e = new DroolsSessionEntity();
96 Date crtdt = new Date(System.currentTimeMillis() - 100);
97 e.setCreatedDate(crtdt);
99 Date updt = new Date(System.currentTimeMillis() - 200);
100 e.setUpdatedDate(updt);
102 assertEquals(crtdt, e.getCreatedDate());
103 assertEquals(updt, e.getUpdatedDate());
107 public void testEqualsObject() {
108 DroolsSessionEntity e = makeEnt("mynameA", 1);
111 assertTrue(e.equals(e));
113 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
115 // session id is not part of hash code
116 assertTrue(e.equals(e2));
117 assertTrue(e.equals(e2));
120 e2 = makeEnt("mynameB", 1);
121 assertFalse(e.equals(e2));
122 assertFalse(e.equals(e2));
126 * Ensures that equals() functions as expected when the getXxx methods are
130 public void testEqualsObject_Subclass() {
131 DroolsSessionEntity e = makeEnt2("mynameA", 1);
134 assertTrue(e.equals(e));
136 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
138 // session id is not part of hash code
139 assertTrue(e.equals(e2));
140 assertTrue(e.equals(e2));
143 e2 = makeEnt("mynameB", 1);
144 assertFalse(e.equals(e2));
145 assertFalse(e.equals(e2));
149 public void testToString() {
150 DroolsSessionEntity e = makeEnt("mynameA", 23);
152 assertEquals("{name=mynameA, id=23}", e.toString());
156 * Makes a session Entity. The parameters are stored into the Entity object
157 * via the setXxx methods.
163 * @return a new session Entity
165 private DroolsSessionEntity makeEnt(String sessnm, long sessid) {
167 DroolsSessionEntity e = new DroolsSessionEntity();
169 e.setSessionName(sessnm);
170 e.setSessionId(sessid);
176 * Makes a session Entity that overrides the getXxx methods. The parameters
177 * that are provided are returned by the overridden methods, but they are
178 * <i>not</i> stored into the Entity object via the setXxx methods.
184 * @return a new session Entity
186 @SuppressWarnings("serial")
187 private DroolsSessionEntity makeEnt2(String sessnm, long sessid) {
189 return new DroolsSessionEntity() {
192 public String getSessionName() {
197 public long getSessionId() {