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