Project

General

Profile

Download (7.24 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.api.service.security;
2

    
3
import java.util.Arrays;
4
import java.util.List;
5
import java.util.UUID;
6

    
7
import org.apache.log4j.Logger;
8
import org.junit.Assert;
9
import org.junit.Before;
10
import org.junit.Test;
11
import org.springframework.core.io.ClassPathResource;
12
import org.springframework.core.io.Resource;
13
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
14
import org.springframework.security.core.Authentication;
15
import org.springframework.security.core.context.SecurityContextHolder;
16
import org.springframework.security.core.context.SecurityContextImpl;
17
import org.springframework.security.core.userdetails.UserDetails;
18

    
19
import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteController;
20
import eu.etaxonomy.cdm.api.application.ICdmApplicationRemoteConfiguration;
21
import eu.etaxonomy.cdm.api.service.IClassificationService;
22
import eu.etaxonomy.cdm.api.service.ITaxonService;
23
import eu.etaxonomy.cdm.api.service.IUserService;
24
import eu.etaxonomy.cdm.model.common.Annotation;
25
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
26
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
27

    
28
public class TestCdmAppicationContextSecurity {
29

    
30
	private static final List<String> TAXON_INIT_STRATEGY = Arrays.asList(new String []{
31
			"*",
32
			// taxon relations 
33
			"relationsToThisName.fromTaxon.name",
34
			// the name
35
			"name.$",
36
			"name.rank.representations",
37
			"name.status.type.representations",
38

    
39
			// taxon descriptions
40
			"descriptions.elements.area.$",
41
			"descriptions.elements.multilanguageText",
42
			"descriptions.elements.media.representations.parts",
43
			"descriptions.elements.media.title",
44
			// supplemental
45
			"annotations"
46
	});
47

    
48
	private ICdmApplicationRemoteConfiguration appCtr;
49
	private static final Logger logger = Logger.getLogger(TestCdmAppicationContextSecurity.class);
50

    
51
	@Before
52
	public void setUp() {
53
		
54
		Resource applicationContextResource = new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
55
		appCtr = CdmApplicationRemoteController.NewInstance(applicationContextResource, null);
56
		
57
        String user = "admin"; // Role:ROLE_ADMIN, Role:ROLE_USER
58
        String pwd = "0000";
59

    
60
        Authentication auth = new UsernamePasswordAuthenticationToken(user, pwd);
61
        SecurityContextImpl sc = new SecurityContextImpl();
62
        sc.setAuthentication(auth);
63
        SecurityContextHolder.setContext(sc);
64
        logger.info("setting up security context");
65
	}
66

    
67
	@Test 
68
	public void testUserService() {
69
		
70
		IUserService userService = appCtr.getUserService();
71
		UserDetails userDetails = userService.loadUserByUsername("admin");
72
		String username = userDetails.getUsername();
73
		String pwd = userDetails.getPassword();
74
		logger.info("userDetails for " + username + ": '" + pwd + "'");
75
	}
76

    
77
	@Test
78
	public void testAddAnnotationToTaxon(){
79
		
80
      ITaxonService taxonService = appCtr.getTaxonService();		
81
      UUID uuid = UUID.fromString("7267e704-e0e8-41ee-8112-5aa22c61dea9");
82
      TaxonBase taxon = taxonService.load(uuid, TAXON_INIT_STRATEGY);
83

    
84
      Annotation annotation = Annotation.NewDefaultLanguageInstance("Test");
85

    
86
      taxon.addAnnotation(annotation);
87

    
88
      taxonService.saveOrUpdate(taxon);
89
	}
90

    
91
	@Test
92
	public void testGetTaxon() {
93
		List<String> TAXON_INIT_STRATEGY = Arrays.asList(new String []{
94
				//"*",
95
				"synonymRelations.synonym.name",
96
				"relationsToThisTaxon",
97
				"relationsFromThisTaxon",
98
				// taxon relations 
99
				"relationsToThisName.fromTaxon.name",
100
				// the name
101
				"name.descriptions.elements",
102
				"name.typeDesignations", 
103
				"name.relationsToThisName",
104
				"name.relationsFromThisName",
105
				"name.homotypicalGroup.typifiedNames",
106
				"name.rank.representations",
107
				"name.status.type.representations",
108
				// name supplemental
109
				"name.annotations",
110
				"name.markers",
111
				"name.credits",
112
				"name.extensions",
113
				"name.rights",
114
				"name.sources",
115
				// taxon descriptions
116
				"descriptions.elements.area.$",
117
				"descriptions.elements.multilanguageText",
118
				"descriptions.elements.media.representations.parts",
119
				"descriptions.elements.media.title",
120
				// supplemental
121
				"annotations",
122
				"markers",
123
				"credits",
124
				"extensions",
125
				"rights",
126
				"sources"
127
				});
128
		
129
		ITaxonService taxonService = appCtr.getTaxonService();
130
		UUID uuid = UUID.fromString("9763e5f0-6cd4-4d96-b8a4-4420854f7727");
131
		TaxonBase taxon = taxonService.load(uuid, TAXON_INIT_STRATEGY);
132
		logger.info("taxonName=" + taxon.getName());
133
		
134
		TaxonNameBase name = taxon.getName();
135
	}
136

    
137
	@Test 
138
	public void testEditTaxonName() {
139
		ITaxonService taxonService = appCtr.getTaxonService();		
140
		UUID uuid = UUID.fromString("7267e704-e0e8-41ee-8112-5aa22c61dea9");
141
		TaxonBase taxon = taxonService.load(uuid, TAXON_INIT_STRATEGY);
142
		logger.info("taxonName=" + taxon.getName());
143
		
144
		TaxonNameBase name = taxon.getName();
145
		
146
		String nameString = "Malvaceaeyyyyy";
147
		name.setTitleCache(nameString);
148
		
149
		taxonService.saveOrUpdate(taxon);
150
		
151
		TaxonBase taxon2 = taxonService.load(uuid, TAXON_INIT_STRATEGY);
152
		Assert.assertEquals(nameString, taxon2.getName().getTitleCache());
153
	}
154

    
155
	@Test 
156
	public void testEditTaxonNameConcurrent() {
157
		ITaxonService taxonService = appCtr.getTaxonService();		
158
		UUID uuid = UUID.fromString("7267e704-e0e8-41ee-8112-5aa22c61dea9");
159
		
160
		// Session 1 aktiv
161
		TaxonBase taxon1 = taxonService.load(uuid, TAXON_INIT_STRATEGY);
162
		logger.info("taxonName=" + taxon1.getName());
163
		
164
		// Session 2 aktiv
165
		TaxonBase taxon2 = taxonService.load(uuid, TAXON_INIT_STRATEGY);
166
		TaxonNameBase name2 = taxon2.getName();
167
		
168
		
169
		// Session 1 aktiv
170
		TaxonNameBase name1 = taxon1.getName();
171
		
172
		Assert.assertEquals(name1, name2);
173
		
174
		String nameString = "Malvaceae";
175
		name1.setTitleCache(nameString);
176
		taxonService.saveOrUpdate(taxon1);
177
		
178
		// Session 2 aktiv
179
		System.out.println("The names are the same: " + name1.getTitleCache().equals(name2.getTitleCache()));
180
		
181
		
182
	}
183

    
184
	@Test
185
	public void testClassificationData() {
186
		IClassificationService classificationService = appCtr.getClassificationService();
187
		String uuidString = "0c2b5d25-7b15-4401-8b51-dd4be0ee5cab"; // true: "0c2b5d25-7b15-4401-8b51-dd4be0ee5cab"
188
		UUID uuid = UUID.fromString(uuidString);
189
		boolean exists = classificationService.exists(uuid);
190
		logger.info("classificationService with uuid='" + uuid + "' exists=" + exists);
191
	}
192

    
193
//	@Test
194
//	public void testNewDatabaseConnection(){
195
//		boolean omitTermLoading = false;
196
//		Resource applicationContextResource = new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
197
//		CdmDataSource dataSource = CdmDataSource.NewMySqlInstance("localhost", "cdm_demo_cyprus", "root", "juergen1");
198
//		CdmApplicationController appCtr = CdmApplicationController.NewInstance(applicationContextResource, dataSource, DbSchemaValidation.VALIDATE, omitTermLoading);
199
//		appCtr.close();
200
//	}
201
	
202
	@Test
203
	public void testApplicationRemoteController(){
204
		Resource applicationContextResource = new ClassPathResource("/eu/etaxonomy/cdm/remotingApplicationContext.xml");
205
		CdmApplicationRemoteController appCtr = CdmApplicationRemoteController.NewInstance(applicationContextResource, null);
206
		appCtr.getClassificationService();
207
		
208
		appCtr.close();
209
	}
210

    
211
}
(1-1/2)