177638db1f8460cd725afb73bb7b8b2279196145
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / taxeditor / httpinvoker / BaseRemotingTest.java
1 /**
2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.httpinvoker;
10
11 import java.lang.reflect.Field;
12 import java.util.Map;
13
14 import net.sf.ehcache.CacheManager;
15
16 import org.apache.log4j.Logger;
17 import org.junit.Assert;
18 import org.junit.BeforeClass;
19 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
20 import org.springframework.security.core.Authentication;
21 import org.springframework.security.core.context.SecurityContextHolder;
22 import org.springframework.security.core.context.SecurityContextImpl;
23 import org.unitils.database.DatabaseUnitils;
24 import org.unitils.database.annotations.Transactional;
25 import org.unitils.database.util.TransactionMode;
26 import org.unitils.spring.annotation.SpringApplicationContext;
27
28 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
29 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
30 import eu.etaxonomy.cdm.api.application.CdmDataChangeService;
31 import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
32 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
33 import eu.etaxonomy.taxeditor.remoting.cache.CdmRemoteCacheManager;
34 import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
35 import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
36 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
37 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
38 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
39 import eu.etaxonomy.taxeditor.session.CdmEntitySession;
40 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
41 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
42
43
44 /**
45 * Base class for remoting tests, responsible for
46 * - starting / stop the cdm server
47 * - running some basic connection tests
48 * - setting up the remote configuration.
49 *
50 */
51 @Transactional(TransactionMode.DISABLED)
52 @SpringApplicationContext("file:./target/classes/eu/etaxonomy/cdm/testRemotingApplicationContext.xml")
53 public abstract class BaseRemotingTest extends ThreadedTest {
54 private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
55
56 private static ICdmRemoteSource cdmRemoteSource;
57 private static CdmPersistentRemoteSource remotePersistentSource;
58 protected static ICdmEntitySessionManager cdmEntitySessionManager;
59
60 @BeforeClass
61 public static void initializeBaseRemotingTest() {
62
63 DatabaseUnitils.disableConstraints();
64
65 try {
66 initializeController(sourceName,
67 host,
68 httpPort,
69 contextPath,
70 NomenclaturalCode.ICNAFP,
71 user,
72 password);
73 } catch (Exception e) {
74 e.printStackTrace();
75 // Assert.fail("Server failed to start. Reason : " + e.getMessage());
76 }
77
78
79 }
80
81 public static void emptyAllCachesExceptModelCache() {
82 CacheManager cm = CacheManager.create();
83 String[] cacheNames = CacheManager.create().getCacheNames();
84 for(String cacheName : cacheNames) {
85 if(!cacheName.equals(CdmRemoteCacheManager.CDM_MODEL_CACHE_NAME)) {
86 cm.removeCache(cacheName);
87 }
88 }
89 }
90
91
92 public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode, String username, String password) {
93
94 if(CdmApplicationState.getCurrentAppConfig() != null) {
95 return;
96 }
97 cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
98 CdmApplicationRemoteController remoteApplicationController =
99 CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
100 null,
101 null);
102 CdmApplicationState.setCurrentAppConfig(remoteApplicationController);
103 CdmApplicationState.setCdmServiceCacher(new CdmServiceCacher());
104 try {
105 remotePersistentSource = CdmPersistentRemoteSource.NewInstance(sourceName);
106 } catch (CdmRemoteSourceException e) {
107 Assert.fail("Default Remote Persistent Source failed to load. Reason : " + e.getMessage());
108 }
109 cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
110
111 CdmApplicationState.setCurrentDataChangeService(new CdmDataChangeService());
112
113 authenticate(username, password);
114
115 }
116
117 protected static void authenticate(String username, String password) {
118
119 //FIXME:Remoting the authentication code should be replaced by a method call which actually
120 // does the authentication in the editor code so that the 'real' authentication can be tested
121 SecurityContextHolder.clearContext();
122 SecurityContextImpl sc = new SecurityContextImpl();
123 Authentication token = new UsernamePasswordAuthenticationToken(username,password);
124 Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
125 authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
126 sc.setAuthentication(authentication);
127
128 SecurityContextHolder.setContext(sc);
129 CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
130
131 }
132
133
134 protected static CdmApplicationRemoteController getRemoteApplicationController() {
135 return (CdmApplicationRemoteController) CdmApplicationState.getCurrentAppConfig();
136 }
137
138 protected static ICdmRemoteSource getCdmRemoteSource() {
139 return cdmRemoteSource;
140 }
141
142 protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
143 return remotePersistentSource;
144 }
145
146 protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
147 return cdmEntitySessionManager;
148 }
149
150
151 protected static CdmEntitySession getSession(ICdmEntitySessionEnabled sessionOwner) {
152 Map<ICdmEntitySessionEnabled, CdmEntitySession> ownerSessionMap =
153 (Map<ICdmEntitySessionEnabled, CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap");
154 return ownerSessionMap.get(sessionOwner);
155 }
156
157 protected static CdmEntitySession getActiveSession() {
158 return ((InheritableThreadLocal<CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "tlActiveSession")).get();
159 }
160
161 protected static CdmTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) {
162 return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
163 }
164
165
166 protected static Object getFieldValueViaReflection(Object object, String fieldName) {
167 Class<?> clazz = object.getClass();
168 try {
169 Field field = clazz.getDeclaredField(fieldName);
170 field.setAccessible(true);
171 return field.get(object);
172 } catch (NoSuchFieldException e) {
173 e.printStackTrace();
174 } catch (SecurityException e) {
175 e.printStackTrace();
176 } catch (IllegalArgumentException e) {
177 e.printStackTrace();
178 } catch (IllegalAccessException e) {
179 e.printStackTrace();
180 }
181 return null;
182 }
183
184
185 protected static void authenticateDefaultUser() {
186 authenticate(user, password);
187 }
188
189
190 }