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
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 entity = makeEnt("mynameA", 1);
38 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
40 // session id is not part of hash code
41 assertTrue(entity.hashCode() == e2.hashCode());
44 e2 = makeEnt("mynameB", 1);
45 assertTrue(entity.hashCode() != e2.hashCode());
48 /** Ensures that hashCode() functions as expected when the getXxx methods are overridden. */
50 public void testHashCode_Subclass() {
51 DroolsSessionEntity entity = makeEnt2("mynameA", 1);
53 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
55 // session id is not part of hash code
56 assertTrue(entity.hashCode() == e2.hashCode());
59 e2 = makeEnt("mynameB", 1);
60 assertTrue(entity.hashCode() != e2.hashCode());
64 public void testGetSessionName_testSetSessionName() {
65 DroolsSessionEntity entity = makeEnt("mynameZ", 1);
67 assertEquals("mynameZ", entity.getSessionName());
69 entity.setSessionName("another");
70 assertEquals("another", entity.getSessionName());
73 assertEquals(1, entity.getSessionId());
77 public void testGetSessionId_testSetSessionId() {
78 DroolsSessionEntity entity = makeEnt("mynameA", 1);
80 assertEquals(1, entity.getSessionId());
82 entity.setSessionId(20);
83 assertEquals(20, entity.getSessionId());
86 assertEquals("mynameA", entity.getSessionName());
90 public void testGetCreatedDate_testSetCreatedDate_testGetUpdatedDate_testSetUpdatedDate() {
91 DroolsSessionEntity entity = new DroolsSessionEntity();
93 Date crtdt = new Date(System.currentTimeMillis() - 100);
94 entity.setCreatedDate(crtdt);
96 Date updt = new Date(System.currentTimeMillis() - 200);
97 entity.setUpdatedDate(updt);
99 assertEquals(crtdt, entity.getCreatedDate());
100 assertEquals(updt, entity.getUpdatedDate());
104 public void testEqualsObject() {
105 DroolsSessionEntity entity = makeEnt("mynameA", 1);
108 assertTrue(entity.equals(entity));
110 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
112 // session id is not part of hash code
113 assertTrue(entity.equals(e2));
114 assertTrue(entity.equals(e2));
117 e2 = makeEnt("mynameB", 1);
118 assertFalse(entity.equals(e2));
119 assertFalse(entity.equals(e2));
122 /** Ensures that equals() functions as expected when the getXxx methods are overridden. */
124 public void testEqualsObject_Subclass() {
125 DroolsSessionEntity entity = makeEnt2("mynameA", 1);
128 assertTrue(entity.equals(entity));
130 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
132 // session id is not part of hash code
133 assertTrue(entity.equals(e2));
134 assertTrue(entity.equals(e2));
137 e2 = makeEnt("mynameB", 1);
138 assertFalse(entity.equals(e2));
139 assertFalse(entity.equals(e2));
143 public void testToString() {
144 DroolsSessionEntity entity = makeEnt("mynameA", 23);
146 assertEquals("{name=mynameA, id=23}", entity.toString());
150 * Makes a session Entity. The parameters are stored into the Entity object via the setXxx
153 * @param sessnm session name
154 * @param sessid session id
155 * @return a new session Entity
157 private DroolsSessionEntity makeEnt(String sessnm, long sessid) {
159 DroolsSessionEntity entity = new DroolsSessionEntity();
161 entity.setSessionName(sessnm);
162 entity.setSessionId(sessid);
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.
172 * @param sessnm session name
173 * @param sessid session id
174 * @return a new session Entity
176 @SuppressWarnings("serial")
177 private DroolsSessionEntity makeEnt2(String sessnm, long sessid) {
179 return new DroolsSessionEntity() {
182 public String getSessionName() {
187 public long getSessionId() {