summaryrefslogtreecommitdiffstats
path: root/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/DroolsSessionEntityTest.java
blob: c7fa84865ab275fd3ea8a20f767991d1a638469e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*-
 * ============LICENSE_START=======================================================
 * feature-session-persistence
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.persistence;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Date;

import org.junit.Test;
import org.onap.policy.drools.persistence.DroolsSessionEntity;

public class DroolsSessionEntityTest {

	@Test
	public void testHashCode() {
		DroolsSessionEntity e = makeEnt("mynameA", 1);

		DroolsSessionEntity e2 = makeEnt("mynameA", 2);
		
		// session id is not part of hash code
		assertTrue(e.hashCode() == e2.hashCode());
		
		// diff sess name
		e2 = makeEnt("mynameB", 1);
		assertTrue(e.hashCode() != e2.hashCode());
	}

	/**
	 * Ensures that hashCode() functions as expected when the getXxx methods
	 * are overridden.
	 */
	@Test
	public void testHashCode_Subclass() {
		DroolsSessionEntity e = makeEnt2("mynameA", 1);

		DroolsSessionEntity e2 = makeEnt("mynameA", 2);
		
		// session id is not part of hash code
		assertTrue(e.hashCode() == e2.hashCode());
		
		// diff sess name
		e2 = makeEnt("mynameB", 1);
		assertTrue(e.hashCode() != e2.hashCode());
	}

	@Test
	public void testGetSessionName_testSetSessionName() {
		DroolsSessionEntity e = makeEnt("mynameZ", 1);

		assertEquals("mynameZ", e.getSessionName());
		
		e.setSessionName("another");
		assertEquals("another", e.getSessionName());
		
		// others unchanged
		assertEquals(1, e.getSessionId());
	}

	@Test
	public void testGetSessionId_testSetSessionId() {
		DroolsSessionEntity e = makeEnt("mynameA", 1);

		assertEquals(1, e.getSessionId());
		
		e.setSessionId(20);
		assertEquals(20, e.getSessionId());
		
		// others unchanged
		assertEquals("mynameA", e.getSessionName());
	}

	@Test
	public void testGetCreatedDate_testSetCreatedDate_testGetUpdatedDate_testSetUpdatedDate() {
		DroolsSessionEntity e = new DroolsSessionEntity();
		
		Date crtdt = new Date(System.currentTimeMillis() - 100);
		e.setCreatedDate(crtdt);

		Date updt = new Date(System.currentTimeMillis() - 200);
		e.setUpdatedDate(updt);
		
		assertEquals(crtdt, e.getCreatedDate());
		assertEquals(updt, e.getUpdatedDate());
	}

	@Test
	public void testEqualsObject() {
		DroolsSessionEntity e = makeEnt("mynameA", 1);
		
		// reflexive
		assertTrue(e.equals(e));

		DroolsSessionEntity e2 = makeEnt("mynameA", 2);
		
		// session id is not part of hash code
		assertTrue(e.equals(e2));
		assertTrue(e.equals(e2));
		
		// diff sess name
		e2 = makeEnt("mynameB", 1);
		assertFalse(e.equals(e2));
		assertFalse(e.equals(e2));
	}

	/**
	 * Ensures that equals() functions as expected when the getXxx methods
	 * are overridden.
	 */
	@Test
	public void testEqualsObject_Subclass() {
		DroolsSessionEntity e = makeEnt2("mynameA", 1);
		
		// reflexive
		assertTrue(e.equals(e));

		DroolsSessionEntity e2 = makeEnt("mynameA", 2);
		
		// session id is not part of hash code
		assertTrue(e.equals(e2));
		assertTrue(e.equals(e2));
		
		// diff sess name
		e2 = makeEnt("mynameB", 1);
		assertFalse(e.equals(e2));
		assertFalse(e.equals(e2));
	}

	@Test
	public void testToString() {
		DroolsSessionEntity e = makeEnt("mynameA", 23);
		
		assertEquals("{name=mynameA, id=23}", e.toString());
	}

	/**
	 * Makes a session Entity.  The parameters are stored into the Entity
	 * object via the setXxx methods.
	 * @param sessnm	session name
	 * @param sessid	session id
	 * @return a new session Entity
	 */
	private DroolsSessionEntity makeEnt(String sessnm, long sessid) {

		DroolsSessionEntity e = new DroolsSessionEntity();
		
		e.setSessionName(sessnm);
		e.setSessionId(sessid);
		
		return e;
	}
	
	/**
	 * Makes a session Entity that overrides the getXxx methods.  The
	 * parameters that are provided are returned by the overridden methods,
	 * but they are <i>not</i> stored into the Entity object via the setXxx
	 * methods.
	 * @param sessnm	session name
	 * @param sessid	session id
	 * @return a new session Entity
	 */
	@SuppressWarnings("serial")
	private DroolsSessionEntity makeEnt2(String sessnm, long sessid) {

		return new DroolsSessionEntity() {

			@Override
			public String getSessionName() {
				return sessnm;
			}

			@Override
			public long getSessionId() {
				return sessid;
			}
		};
	}

}