2 * ============LICENSE_START=======================================================
3 * feature-session-persistence
4 * ================================================================================
5 * Copyright (C) 2017-2018, 2020 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.assertNotEquals;
26 import java.util.Date;
27 import org.junit.Test;
29 public class DroolsSessionEntityTest {
32 public void testHashCode() {
33 DroolsSessionEntity entity = makeEnt("mynameA", 1);
35 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
37 // session id is not part of hash code
38 assertEquals(entity.hashCode(), e2.hashCode());
41 e2 = makeEnt("mynameB", 1);
42 assertNotEquals(entity.hashCode(), e2.hashCode());
45 /** Ensures that hashCode() functions as expected when the getXxx methods are overridden. */
47 public void testHashCode_Subclass() {
48 DroolsSessionEntity entity = makeEnt2("mynameA", 1);
50 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
52 // session id is not part of hash code
53 assertEquals(entity.hashCode(), e2.hashCode());
56 e2 = makeEnt("mynameB", 1);
57 assertNotEquals(entity.hashCode(), e2.hashCode());
61 public void testGetSessionName_testSetSessionName() {
62 DroolsSessionEntity entity = makeEnt("mynameZ", 1);
64 assertEquals("mynameZ", entity.getSessionName());
66 entity.setSessionName("another");
67 assertEquals("another", entity.getSessionName());
70 assertEquals(1, entity.getSessionId());
74 public void testGetSessionId_testSetSessionId() {
75 DroolsSessionEntity entity = makeEnt("mynameA", 1);
77 assertEquals(1, entity.getSessionId());
79 entity.setSessionId(20);
80 assertEquals(20, entity.getSessionId());
83 assertEquals("mynameA", entity.getSessionName());
87 public void testGetCreatedDate_testSetCreatedDate_testGetUpdatedDate_testSetUpdatedDate() {
88 DroolsSessionEntity entity = new DroolsSessionEntity();
90 Date crtdt = new Date(System.currentTimeMillis() - 100);
91 entity.setCreatedDate(crtdt);
93 Date updt = new Date(System.currentTimeMillis() - 200);
94 entity.setUpdatedDate(updt);
96 assertEquals(crtdt, entity.getCreatedDate());
97 assertEquals(updt, entity.getUpdatedDate());
101 public void testEqualsObject() {
102 DroolsSessionEntity entity = makeEnt("mynameA", 1);
105 assertNotEquals(entity, "hello");
108 assertEquals(entity, entity);
110 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
112 // session id is not part of hash code
113 assertEquals(entity, e2);
114 assertEquals(entity, e2);
117 e2 = makeEnt("mynameB", 1);
118 assertNotEquals(entity, e2);
119 assertNotEquals(entity, 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 assertEquals(entity, entity);
130 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
132 // session id is not part of hash code
133 assertEquals(entity, e2);
134 assertEquals(entity, e2);
137 e2 = makeEnt("mynameB", 1);
138 assertNotEquals(entity, e2);
139 assertNotEquals(entity, 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() {