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;
28 import org.junit.Test;
30 public class DroolsSessionEntityTest {
33 public void testHashCode() {
34 DroolsSessionEntity entity = makeEnt("mynameA", 1);
36 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
38 // session id is not part of hash code
39 assertTrue(entity.hashCode() == e2.hashCode());
42 e2 = makeEnt("mynameB", 1);
43 assertTrue(entity.hashCode() != e2.hashCode());
46 /** Ensures that hashCode() functions as expected when the getXxx methods are overridden. */
48 public void testHashCode_Subclass() {
49 DroolsSessionEntity entity = makeEnt2("mynameA", 1);
51 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
53 // session id is not part of hash code
54 assertTrue(entity.hashCode() == e2.hashCode());
57 e2 = makeEnt("mynameB", 1);
58 assertTrue(entity.hashCode() != e2.hashCode());
62 public void testGetSessionName_testSetSessionName() {
63 DroolsSessionEntity entity = makeEnt("mynameZ", 1);
65 assertEquals("mynameZ", entity.getSessionName());
67 entity.setSessionName("another");
68 assertEquals("another", entity.getSessionName());
71 assertEquals(1, entity.getSessionId());
75 public void testGetSessionId_testSetSessionId() {
76 DroolsSessionEntity entity = makeEnt("mynameA", 1);
78 assertEquals(1, entity.getSessionId());
80 entity.setSessionId(20);
81 assertEquals(20, entity.getSessionId());
84 assertEquals("mynameA", entity.getSessionName());
88 public void testGetCreatedDate_testSetCreatedDate_testGetUpdatedDate_testSetUpdatedDate() {
89 DroolsSessionEntity entity = new DroolsSessionEntity();
91 Date crtdt = new Date(System.currentTimeMillis() - 100);
92 entity.setCreatedDate(crtdt);
94 Date updt = new Date(System.currentTimeMillis() - 200);
95 entity.setUpdatedDate(updt);
97 assertEquals(crtdt, entity.getCreatedDate());
98 assertEquals(updt, entity.getUpdatedDate());
102 public void testEqualsObject() {
103 DroolsSessionEntity entity = makeEnt("mynameA", 1);
106 assertFalse(entity.equals("hello"));
109 assertTrue(entity.equals(entity));
111 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
113 // session id is not part of hash code
114 assertTrue(entity.equals(e2));
115 assertTrue(entity.equals(e2));
118 e2 = makeEnt("mynameB", 1);
119 assertFalse(entity.equals(e2));
120 assertFalse(entity.equals(e2));
123 /** Ensures that equals() functions as expected when the getXxx methods are overridden. */
125 public void testEqualsObject_Subclass() {
126 DroolsSessionEntity entity = makeEnt2("mynameA", 1);
129 assertTrue(entity.equals(entity));
131 DroolsSessionEntity e2 = makeEnt("mynameA", 2);
133 // session id is not part of hash code
134 assertTrue(entity.equals(e2));
135 assertTrue(entity.equals(e2));
138 e2 = makeEnt("mynameB", 1);
139 assertFalse(entity.equals(e2));
140 assertFalse(entity.equals(e2));
144 public void testToString() {
145 DroolsSessionEntity entity = makeEnt("mynameA", 23);
147 assertEquals("{name=mynameA, id=23}", entity.toString());
151 * Makes a session Entity. The parameters are stored into the Entity object via the setXxx
154 * @param sessnm session name
155 * @param sessid session id
156 * @return a new session Entity
158 private DroolsSessionEntity makeEnt(String sessnm, long sessid) {
160 DroolsSessionEntity entity = new DroolsSessionEntity();
162 entity.setSessionName(sessnm);
163 entity.setSessionId(sessid);
169 * Makes a session Entity that overrides the getXxx methods. The parameters that are provided are
170 * returned by the overridden methods, but they are <i>not</i> stored into the Entity object via
171 * the setXxx methods.
173 * @param sessnm session name
174 * @param sessid session id
175 * @return a new session Entity
177 @SuppressWarnings("serial")
178 private DroolsSessionEntity makeEnt2(String sessnm, long sessid) {
180 return new DroolsSessionEntity() {
183 public String getSessionName() {
188 public long getSessionId() {