1
|
package unitest;
|
2
|
|
3
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
4
|
import com.fasterxml.jackson.databind.type.TypeFactory;
|
5
|
import eu.dnetlib.goldoa.domain.*;
|
6
|
import eu.dnetlib.goldoa.service.*;
|
7
|
import eu.dnetlib.goldoa.service.dao.UserDAO;
|
8
|
import net.sf.ehcache.hibernate.HibernateUtil;
|
9
|
import org.hibernate.Hibernate;
|
10
|
import org.hibernate.SessionFactory;
|
11
|
import org.hibernate.stat.SessionStatistics;
|
12
|
import org.hibernate.stat.Statistics;
|
13
|
import org.junit.Test;
|
14
|
import org.junit.runner.RunWith;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.context.annotation.PropertySource;
|
17
|
import org.springframework.test.annotation.Rollback;
|
18
|
import org.springframework.test.context.ContextConfiguration;
|
19
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
20
|
import org.springframework.util.DigestUtils;
|
21
|
|
22
|
import javax.persistence.EntityManager;
|
23
|
import javax.persistence.EntityManagerFactory;
|
24
|
import javax.transaction.Transactional;
|
25
|
import java.io.IOException;
|
26
|
import java.nio.charset.Charset;
|
27
|
import java.util.ArrayList;
|
28
|
import java.util.LinkedHashMap;
|
29
|
import java.util.List;
|
30
|
|
31
|
import static org.junit.Assert.*;
|
32
|
|
33
|
/**
|
34
|
* Created by panagiotis on 2/2/2017.
|
35
|
*/
|
36
|
@RunWith( SpringJUnit4ClassRunner.class )
|
37
|
@ContextConfiguration(locations = "classpath:applicationContext-goldoa-service-test.xml")
|
38
|
@PropertySource("classpath:applicationContext-goldoa-service.properties ")
|
39
|
public class ProfileTest {
|
40
|
|
41
|
@Autowired
|
42
|
UserManagerImpl um;
|
43
|
@Autowired
|
44
|
OrganizationManager om;
|
45
|
@Autowired
|
46
|
ProjectManager pm;
|
47
|
@Autowired
|
48
|
SearchManager sm;
|
49
|
@Autowired
|
50
|
PublisherManager pubm;
|
51
|
|
52
|
@Autowired
|
53
|
ProjectManagerImpl p;
|
54
|
|
55
|
@Autowired
|
56
|
UserDAO ud;
|
57
|
|
58
|
@Test
|
59
|
@Transactional
|
60
|
@Rollback(true)
|
61
|
public void login(){
|
62
|
User user = null;
|
63
|
try {
|
64
|
user = um.login("p4@di.uoa.gr","1234");
|
65
|
} catch (PersonManagerException e) {
|
66
|
e.printStackTrace();
|
67
|
}
|
68
|
assertNotEquals(user,null);
|
69
|
}
|
70
|
|
71
|
@Test
|
72
|
@Transactional
|
73
|
@Rollback(true)
|
74
|
public void getUser(){
|
75
|
User user = null;
|
76
|
Statistics stats = ud.getSession().getSessionFactory().getStatistics();
|
77
|
stats.setStatisticsEnabled(true);
|
78
|
try {
|
79
|
printStats(stats,0);
|
80
|
user = um.getByEmail("ant.lebesis@gmail.com");
|
81
|
// um.rejectRole(user.getEmail(),"library_staff");
|
82
|
//um.deactivateUser(user.getEmail());
|
83
|
// um.saveUser(user);
|
84
|
printStats(stats,1);
|
85
|
System.out.println("Cached output");
|
86
|
ud.getSession().evict(user);
|
87
|
user = um.getByEmail("ant.lebesis@gmail.com");
|
88
|
printStats(stats,2);
|
89
|
} catch (PersonManagerException e) {
|
90
|
e.printStackTrace();
|
91
|
}
|
92
|
}
|
93
|
|
94
|
@Test
|
95
|
@Transactional
|
96
|
@Rollback(true)
|
97
|
public void register() throws IOException, PersonManagerException {
|
98
|
User user = new User();
|
99
|
user.setEmail("p4@di.uoa.gr");
|
100
|
user.setPassword("1234");
|
101
|
List<Object> rs = null;
|
102
|
|
103
|
|
104
|
rs = sm.search(SearchManager.TYPE.PUBLISHER,"EDP Sciencesss");
|
105
|
Publisher pub = null;
|
106
|
if(rs.size() > 0)
|
107
|
pub = (Publisher)rs.get(0);
|
108
|
|
109
|
if(pub == null){
|
110
|
pub = new Publisher();
|
111
|
pub.setName("EDP");
|
112
|
|
113
|
user.setPublisher(pub);
|
114
|
|
115
|
pub.getContacts().add(user);
|
116
|
pubm.savePublisher(pub);
|
117
|
|
118
|
try {
|
119
|
um.register(user);
|
120
|
} catch (PersonManagerException e) {
|
121
|
e.printStackTrace();
|
122
|
}
|
123
|
}else{
|
124
|
user.setPublisher(pub);
|
125
|
|
126
|
try {
|
127
|
um.register(user);
|
128
|
} catch (PersonManagerException e) {
|
129
|
e.printStackTrace();
|
130
|
}
|
131
|
|
132
|
pub.getContacts().add(user);
|
133
|
pubm.savePublisher(pub);
|
134
|
}
|
135
|
|
136
|
|
137
|
}
|
138
|
|
139
|
@Test
|
140
|
@Transactional
|
141
|
@Rollback(true)
|
142
|
public void activateUser() throws IOException {
|
143
|
um.activateUser("pkanakakis@di.uoa.gr");
|
144
|
}
|
145
|
|
146
|
|
147
|
@Test
|
148
|
@Transactional
|
149
|
@Rollback(true)
|
150
|
public void eligibleProjectsByOrganization(){
|
151
|
List<Project> pr = pm.getEligibleProjectsForOrganization("corda::983580389");
|
152
|
for(Project p : pr)
|
153
|
System.out.println(p.getId());
|
154
|
|
155
|
pr = pm.getFutureEligibleProjectsForOrganization("corda::983580389");
|
156
|
for(Project p : pr)
|
157
|
System.out.println(p.getId());
|
158
|
}
|
159
|
|
160
|
@Test
|
161
|
@Transactional
|
162
|
@Rollback(true)
|
163
|
public void update(){
|
164
|
User u = null;
|
165
|
try {
|
166
|
u = um.getByEmail("ant.lebesis@gmail.com");
|
167
|
} catch (PersonManagerException e) {
|
168
|
e.printStackTrace();
|
169
|
}
|
170
|
u.setPassword("1234");
|
171
|
UserRole ur = new UserRole();
|
172
|
UserRolePK urpk = new UserRolePK();
|
173
|
urpk.setRole(new Role("library_staff","Library Staff"));
|
174
|
urpk.setUser(u);
|
175
|
ur.setPk(urpk);
|
176
|
ur.setApproved(false);
|
177
|
u.getRoles().add(ur);
|
178
|
|
179
|
try {
|
180
|
um.saveUser(u);
|
181
|
} catch (PersonManagerException e) {
|
182
|
e.printStackTrace();
|
183
|
}
|
184
|
}
|
185
|
|
186
|
|
187
|
@Test
|
188
|
@Transactional
|
189
|
@Rollback(true)
|
190
|
public void getModerators(){
|
191
|
List<User> users = um.getModerators();
|
192
|
for(User u:users)
|
193
|
System.out.println(u.getEmail());
|
194
|
}
|
195
|
|
196
|
@Test
|
197
|
@Transactional
|
198
|
@Rollback(true)
|
199
|
public void resetPassword(){
|
200
|
try {
|
201
|
um.resetPassword("ant.lebesis@gmail.com");
|
202
|
} catch (PersonManagerException e) {
|
203
|
e.printStackTrace();
|
204
|
}
|
205
|
}
|
206
|
|
207
|
@Test
|
208
|
@Transactional
|
209
|
@Rollback(true)
|
210
|
public void getPublisherContacts(){
|
211
|
List<Object> rs = null;
|
212
|
|
213
|
rs = sm.search(SearchManager.TYPE.PUBLISHER,"EDP Sciences");
|
214
|
Publisher pub = null;
|
215
|
pub = (Publisher)rs.get(0);
|
216
|
|
217
|
for(User contact:pub.getContacts())
|
218
|
System.out.println(contact.getEmail());
|
219
|
|
220
|
}
|
221
|
|
222
|
@Test
|
223
|
@Transactional
|
224
|
@Rollback(true)
|
225
|
public void registerAsPC() throws IOException, PersonManagerException {
|
226
|
User user = new User();
|
227
|
user.setEmail("p4@di.uoa.gr");
|
228
|
user.setPassword("1234");
|
229
|
List<Object> rs = null;
|
230
|
|
231
|
|
232
|
rs = sm.search(SearchManager.TYPE.PROJECT,"ECB");
|
233
|
user.getCoordinatedProjects().add((Project)rs.get(0));
|
234
|
um.saveUser(user);
|
235
|
|
236
|
}
|
237
|
|
238
|
@Test
|
239
|
@Transactional
|
240
|
@Rollback(true)
|
241
|
public void getUsers() throws IOException, PersonManagerException {
|
242
|
List<User> users = um.getUsers();
|
243
|
|
244
|
User user = users.get(0);
|
245
|
um.deactivateUser(user.getEmail());
|
246
|
System.out.println("************************User deactivation*********************");
|
247
|
users = um.getUsers();
|
248
|
|
249
|
}
|
250
|
|
251
|
|
252
|
private static void printStats(Statistics stats, int i) {
|
253
|
System.out.println("***** " + i + " *****");
|
254
|
System.out.println("Fetch Count="
|
255
|
+ stats.getEntityFetchCount());
|
256
|
System.out.println("Second Level Hit Count="
|
257
|
+ stats.getSecondLevelCacheHitCount());
|
258
|
System.out
|
259
|
.println("Second Level Miss Count="
|
260
|
+ stats
|
261
|
.getSecondLevelCacheMissCount());
|
262
|
System.out.println("Second Level Put Count="
|
263
|
+ stats.getSecondLevelCachePutCount());
|
264
|
}
|
265
|
|
266
|
|
267
|
}
|