/
AbstractKapuaUpdatableEntityTest.java
165 lines (144 loc) · 6.9 KB
/
AbstractKapuaUpdatableEntityTest.java
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
/*******************************************************************************
* Copyright (c) 2020, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.model;
import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.security.KapuaSession;
import org.eclipse.kapua.commons.util.RandomUtils;
import org.eclipse.kapua.model.KapuaUpdatableEntity;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
@Category(JUnitTests.class)
public class AbstractKapuaUpdatableEntityTest {
private final static Random RANDOM = RandomUtils.getInstance();
private class ActualKapuaUpdatableEntity extends AbstractKapuaUpdatableEntity {
@Override
public String getType() {
return null;
}
public ActualKapuaUpdatableEntity() {
super();
}
public ActualKapuaUpdatableEntity(KapuaId scopeId) {
super(scopeId);
}
public ActualKapuaUpdatableEntity(KapuaUpdatableEntity entity) {
super(entity);
}
}
@Test
public void abstractKapuaUpdatableEntityScopeIdTest() {
KapuaId scopeId = new KapuaEid();
AbstractKapuaUpdatableEntity updatableEntity = new ActualKapuaUpdatableEntity(scopeId);
Assert.assertEquals("Actual and expected values are not the same!", scopeId, updatableEntity.getScopeId());
}
@Test
public void abstractKapuaUpdatableEntityEntityIdTest() {
KapuaUpdatableEntity entity = new ActualKapuaUpdatableEntity();
entity.setEntityAttributes(new Properties());
entity.setEntityProperties(new Properties());
entity.setOptlock(10);
AbstractKapuaUpdatableEntity updatableEntity = new ActualKapuaUpdatableEntity(entity);
updatableEntity.setModifiedOn(new Date());
updatableEntity.setModifiedBy(new KapuaEid());
Assert.assertNotNull("Expected true", updatableEntity.getModifiedOn());
Assert.assertNotNull("Expected true", updatableEntity.getModifiedBy());
Assert.assertNotNull("Expected true", updatableEntity.getOptlock());
Assert.assertNotNull("Expected true", updatableEntity.getEntityAttributes());
Assert.assertNotNull("Expected true", updatableEntity.getEntityProperties());
}
@Test
public void getModifiedOnTest() {
KapuaId scopeId = new KapuaEid();
AbstractKapuaUpdatableEntity updatableEntity = new ActualKapuaUpdatableEntity(scopeId);
Date modifiedOn = new Date();
updatableEntity.setModifiedOn(modifiedOn);
Assert.assertEquals("Actual and expected values are not the same!", modifiedOn, updatableEntity.getModifiedOn());
}
@Test
public void getModifiedByTest() {
KapuaId scopeId = new KapuaEid();
AbstractKapuaUpdatableEntity updatableEntity = new ActualKapuaUpdatableEntity(scopeId);
KapuaId modifiedBy = new KapuaEid();
updatableEntity.setModifiedBy(modifiedBy);
Assert.assertEquals("Actual and expected values are not the same!", modifiedBy, updatableEntity.getModifiedBy());
}
@Test
public void getOptlockTest() {
KapuaId scopeId = new KapuaEid();
AbstractKapuaUpdatableEntity updatableEntity = new ActualKapuaUpdatableEntity(scopeId);
int optlock = 25;
updatableEntity.setOptlock(optlock);
Assert.assertEquals("Actual and expected values are not the same!", optlock, updatableEntity.getOptlock());
}
@Test(expected = IOException.class)
public void getEntityAttributesTest() {
AbstractKapuaUpdatableEntity kapuaUpdatableEntity = new ActualKapuaUpdatableEntity();
AbstractKapuaUpdatableEntity spy = Mockito.spy(kapuaUpdatableEntity);
Mockito.when(spy.getEntityAttributes()).thenThrow(IOException.class);
spy.getEntityAttributes();
}
@Test(expected = IOException.class)
public void setEntityAttributesTest() {
AbstractKapuaUpdatableEntity kapuaUpdatableEntity = new ActualKapuaUpdatableEntity();
AbstractKapuaUpdatableEntity spy = Mockito.spy(kapuaUpdatableEntity);
Mockito.doThrow(IOException.class).when(spy).setEntityAttributes(new Properties());
spy.setEntityAttributes(new Properties());
}
@Test(expected = IOException.class)
public void getEntityPropertiesTest() {
AbstractKapuaUpdatableEntity kapuaUpdatableEntity = new ActualKapuaUpdatableEntity();
AbstractKapuaUpdatableEntity spy = Mockito.spy(kapuaUpdatableEntity);
Mockito.when(spy.getEntityProperties()).thenThrow(IOException.class);
spy.getEntityProperties();
}
@Test(expected = IOException.class)
public void setEntityPropertiesTest() {
AbstractKapuaUpdatableEntity kapuaUpdatableEntity = new ActualKapuaUpdatableEntity();
AbstractKapuaUpdatableEntity spy = Mockito.spy(kapuaUpdatableEntity);
Mockito.doThrow(IOException.class).when(spy).setEntityProperties(new Properties());
spy.setEntityProperties(new Properties());
}
@Test
public void prePersistsActionTest() {
KapuaId scopeId = new KapuaEid();
BigInteger eid = new BigInteger(64, RANDOM);
AbstractKapuaUpdatableEntity updatableEntity = new ActualKapuaUpdatableEntity(scopeId);
KapuaSession mockedSession = Mockito.mock(KapuaSession.class);
Mockito.when(mockedSession.getUserId()).thenReturn(new KapuaEid(eid));
KapuaSecurityUtils.setSession(mockedSession);
updatableEntity.prePersistsAction();
Assert.assertNotNull("Expected true", updatableEntity.getModifiedBy());
Assert.assertNotNull("Expected true", updatableEntity.getModifiedOn());
}
@Test
public void preUpdateActionTest() {
BigInteger eid = new BigInteger(64, RANDOM);
AbstractKapuaUpdatableEntity updatableEntity = new ActualKapuaUpdatableEntity();
KapuaSession mockedSession = Mockito.mock(KapuaSession.class);
Mockito.when(mockedSession.getUserId()).thenReturn(new KapuaEid(eid));
KapuaSecurityUtils.setSession(mockedSession);
updatableEntity.preUpdateAction();
Assert.assertNotNull("Expected true", updatableEntity.getModifiedBy());
Assert.assertNotNull("Expected true", updatableEntity.getModifiedOn());
}
}