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 assertFalse(entity.equals("hello"));
111 assertTrue(entity.equals(entity));
113 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
115 // session id is not part of hash code
116 assertTrue(entity.equals(e2));
117 assertTrue(entity.equals(e2));
120 e2 = makeEnt("mynameB", 1);
121 assertFalse(entity.equals(e2));
122 assertFalse(entity.equals(e2));
125 /** Ensures that equals() functions as expected when the getXxx methods are overridden. */
127 public void testEqualsObject_Subclass() {
128 DroolsSessionEntity entity = makeEnt2("mynameA", 1);
131 assertTrue(entity.equals(entity));
133 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
135 // session id is not part of hash code
136 assertTrue(entity.equals(e2));
137 assertTrue(entity.equals(e2));
140 e2 = makeEnt("mynameB", 1);
141 assertFalse(entity.equals(e2));
142 assertFalse(entity.equals(e2));
146 public void testToString() {
147 DroolsSessionEntity entity = makeEnt("mynameA", 23);
149 assertEquals("{name=mynameA, id=23}", entity.toString());
153 * Makes a session Entity. The parameters are stored into the Entity object via the setXxx
156 * @param sessnm session name
157 * @param sessid session id
158 * @return a new session Entity
160 private DroolsSessionEntity makeEnt(String sessnm, long sessid) {
162 DroolsSessionEntity entity = new DroolsSessionEntity();
164 entity.setSessionName(sessnm);
165 entity.setSessionId(sessid);
171 * Makes a session Entity that overrides the getXxx methods. The parameters that are provided are
172 * returned by the overridden methods, but they are <i>not</i> stored into the Entity object via
173 * the setXxx methods.
175 * @param sessnm session name
176 * @param sessid session id
177 * @return a new session Entity
179 @SuppressWarnings("serial")
180 private DroolsSessionEntity makeEnt2(String sessnm, long sessid) {
182 return new DroolsSessionEntity() {
185 public String getSessionName() {
190 public long getSessionId() {