Project

General

Profile

Download (7.66 KB) Statistics
| Branch: | Tag: | Revision:
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.io.IOException;
12
import java.lang.reflect.Field;
13
import java.util.Map;
14

    
15
import org.junit.AfterClass;
16
import org.junit.Assert;
17
import org.junit.BeforeClass;
18
import org.junit.Test;
19
import org.springframework.core.io.ClassPathResource;
20
import org.springframework.core.io.Resource;
21
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
22
import org.springframework.security.core.Authentication;
23
import org.springframework.security.core.context.SecurityContextHolder;
24
import org.springframework.security.core.context.SecurityContextImpl;
25
import org.unitils.UnitilsJUnit4;
26
import org.unitils.database.DatabaseUnitils;
27
import org.unitils.database.annotations.Transactional;
28
import org.unitils.database.util.TransactionMode;
29
import org.unitils.spring.annotation.SpringApplicationContext;
30

    
31
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
32
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
33
import eu.etaxonomy.taxeditor.remoting.cache.CdmClientCacheException;
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.ICdmEntitySessionManager;
41
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
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 class BaseRemotingTest extends UnitilsJUnit4 {
54
    //private static final Logger logger = Logger.getLogger(BaseRemotingTest.class);
55

    
56
    private static final Resource TEST_REMOTE_APPLICATION_CONTEXT_RESOURCE =
57
            new ClassPathResource("/eu/etaxonomy/cdm/testRemotingApplicationContext.xml");
58

    
59
    private static CdmApplicationRemoteController remoteApplicationController;
60
    private static ICdmRemoteSource cdmRemoteSource;
61
    private static CdmPersistentRemoteSource remotePersistentSource;
62

    
63
    public static boolean useManagedServer = false;
64

    
65
    public static final Resource CDMLIB_DISK_STORE_RESOURCE =
66
            new ClassPathResource("/eu/etaxonomy/cache");
67

    
68
    protected static ICdmEntitySessionManager cdmEntitySessionManager;
69

    
70

    
71
    @BeforeClass
72
    public static void initializeBaseRemotingTest() {
73
        //NOTE: Run this the cdmTest H2 DB whenever it needs to be
74
    	//      recreated e.g. after a model change
75
    	DatabaseUnitils.disableConstraints();
76

    
77
		try {
78
			System.setProperty("ehcache.disk.store.dir", CDMLIB_DISK_STORE_RESOURCE.getFile().getAbsolutePath());
79
		} catch (IOException e) {
80
			throw new CdmClientCacheException(e);
81
		}
82

    
83
        useManagedServer = (System.getProperty("use.managed.server") == null) ? useManagedServer : Boolean.valueOf(System.getProperty("use.managed.server"));
84
        if(useManagedServer) {
85
            try {
86
                CDMServer.getInstance().start();
87
            } catch (Exception e) {
88
                e.printStackTrace();
89
                Assert.fail("Server failed to start. Reason : " + e.getMessage());
90
            }
91

    
92
        initializeController(CDMServer.getInstance().getName(),
93
                CDMServer.getInstance().getHost(),
94
                CDMServer.getInstance().getPort(),
95
                CDMServer.getInstance().getContextPath(),
96
                NomenclaturalCode.ICNAFP,
97
                "admin",
98
                "00000");
99
        }
100

    
101

    
102
    }
103

    
104

    
105
    @Test
106
    public void disableConstraints() {
107
    	// To be run on the src/test/resources/h2/cdmTest h2 db after
108
    	// updating the unitils.properties 'database.url' property
109
    	DatabaseUnitils.disableConstraints();
110
    }
111

    
112
    public static void initializeController(String sourceName, String host, int port, String contextPath, NomenclaturalCode ncode, String username, String password) {
113

    
114
        cdmRemoteSource = CdmRemoteSource.NewInstance(sourceName, host, port, contextPath, ncode);
115
        remoteApplicationController =
116
                CdmApplicationRemoteController.NewInstance(cdmRemoteSource,
117
                        false,
118
                        null,
119
                        null);
120

    
121
        try {
122
            remotePersistentSource = CdmPersistentRemoteSource.NewInstance(sourceName);
123
        } catch (CdmRemoteSourceException e) {
124
            Assert.fail("Default Remote Persistent Source failed to load. Reason : " + e.getMessage());
125
        }
126
        cdmEntitySessionManager = getRemoteApplicationController().getCdmEntitySessionManager();
127

    
128
        SecurityContextHolder.clearContext();
129
        SecurityContextImpl sc = new SecurityContextImpl();
130
        Authentication token = new UsernamePasswordAuthenticationToken(username,password);
131
        Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
132
        authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
133
        sc.setAuthentication(authentication);
134

    
135
        SecurityContextHolder.setContext(sc);
136
    }
137

    
138

    
139

    
140
    protected static CdmApplicationRemoteController getRemoteApplicationController() {
141
        return remoteApplicationController;
142
    }
143

    
144
    protected static ICdmRemoteSource getCdmRemoteSource() {
145
        return cdmRemoteSource;
146
    }
147

    
148
    protected static CdmPersistentRemoteSource getCdmPersistentRemoteSource() {
149
        return remotePersistentSource;
150
    }
151

    
152
    protected static ICdmEntitySessionManager getCdmEntitySessionManager() {
153
    	return cdmEntitySessionManager;
154
    }
155

    
156

    
157
    protected static CdmEntitySession getSession(ICdmEntitySessionEnabled sessionOwner) {
158
    	Map<ICdmEntitySessionEnabled, CdmEntitySession> ownerSessionMap =
159
    			(Map<ICdmEntitySessionEnabled, CdmEntitySession>) getFieldValueViaReflection(cdmEntitySessionManager, "ownerSessionMap");
160
    	return ownerSessionMap.get(sessionOwner);
161
    }
162

    
163
    protected static CdmTransientEntityCacher getActiveSession() {
164
    	return (CdmTransientEntityCacher) getFieldValueViaReflection(cdmEntitySessionManager, "activeSession");
165
    }
166

    
167
    protected static CdmTransientEntityCacher getCacher(ICdmEntitySessionEnabled sessionOwner) {
168
    	return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
169
    }
170

    
171
    @AfterClass
172
    public static void cleanup() {
173
        try {
174
            CDMServer.getInstance().stop();
175
        } catch (Exception e) {
176
            Assert.fail("Server could not be stopped. Reason : " + e.getMessage());
177
        }
178
    }
179

    
180

    
181
    protected static Object getFieldValueViaReflection(Object object, String fieldName) {
182
        Class<?> clazz = object.getClass();
183
        try {
184
            Field field = clazz.getDeclaredField(fieldName);
185
            field.setAccessible(true);
186
            return field.get(object);
187
        } catch (NoSuchFieldException e) {
188
            e.printStackTrace();
189
        } catch (SecurityException e) {
190
            e.printStackTrace();
191
        } catch (IllegalArgumentException e) {
192
            e.printStackTrace();
193
        } catch (IllegalAccessException e) {
194
            e.printStackTrace();
195
        }
196
        return null;
197
    }
198

    
199

    
200

    
201

    
202
}
(1-1/7)