1
|
package gr.uoa.di.driver.xml;
|
2
|
|
3
|
import java.io.ByteArrayInputStream;
|
4
|
import java.io.File;
|
5
|
import java.io.IOException;
|
6
|
import java.util.Date;
|
7
|
|
8
|
import javax.xml.XMLConstants;
|
9
|
import javax.xml.bind.JAXBException;
|
10
|
import javax.xml.transform.sax.SAXSource;
|
11
|
import javax.xml.validation.Schema;
|
12
|
import javax.xml.validation.SchemaFactory;
|
13
|
import javax.xml.validation.Validator;
|
14
|
|
15
|
import org.junit.Assert;
|
16
|
|
17
|
import org.junit.Test;
|
18
|
import org.xml.sax.InputSource;
|
19
|
import org.xml.sax.SAXException;
|
20
|
|
21
|
import eu.dnetlib.domain.functionality.SavedQuery;
|
22
|
import eu.dnetlib.domain.functionality.UserProfile;
|
23
|
import eu.dnetlib.domain.functionality.UserStore;
|
24
|
|
25
|
public class TestUserProfileXmlConverter {
|
26
|
UserProfileXmlConverter conv;
|
27
|
Schema schema;
|
28
|
|
29
|
@Test
|
30
|
public void testSimpleUserProfile() throws Exception {
|
31
|
|
32
|
conv = new UserProfileXmlConverter();
|
33
|
UserProfile profile = new UserProfile();
|
34
|
profile.setEmail("Helenaki");
|
35
|
profile.setActivationId("123");
|
36
|
profile.setActive(true);
|
37
|
profile.setDateOfCreation(new Date());
|
38
|
|
39
|
testConverter(profile);
|
40
|
conv = null;
|
41
|
|
42
|
}
|
43
|
|
44
|
|
45
|
|
46
|
@Test
|
47
|
public void testUserProfile() throws Exception {
|
48
|
|
49
|
conv = new UserProfileXmlConverter();
|
50
|
UserProfile profile = new UserProfile();
|
51
|
profile.setEmail("Helenaki");
|
52
|
profile.setActivationId("123");
|
53
|
profile.setActive(true);
|
54
|
profile.setDateOfCreation(new Date());
|
55
|
|
56
|
profile.setFilter(new SavedQuery());
|
57
|
profile.getFilter().setCqlText("cql");
|
58
|
|
59
|
profile.getFilter().setRefineText("refine");
|
60
|
profile.getFilter().getCollectionIds().add("Collection 1");
|
61
|
|
62
|
profile.getDocumentIds().add("Document 1");
|
63
|
profile.getDocumentIds().add("Document 2");
|
64
|
|
65
|
profile.setUserPhotoID("photoID");
|
66
|
profile.setStore(new UserStore());
|
67
|
profile.getStore().setStoreID("store ID");
|
68
|
profile.getStore().setStoreServiceURL("service URL");
|
69
|
|
70
|
SavedQuery squery = new SavedQuery();
|
71
|
squery.setCqlText("cqlTesxt");
|
72
|
squery.getCommunityIds().add("My community1");
|
73
|
|
74
|
profile.getSavedQueries().add(squery);
|
75
|
|
76
|
testConverter(profile);
|
77
|
conv = null;
|
78
|
|
79
|
}
|
80
|
|
81
|
private void testConverter(UserProfile userProfile) throws JAXBException,
|
82
|
SAXException, IOException {
|
83
|
String xml1 = conv.ObjectToXml(userProfile);
|
84
|
|
85
|
UserProfile user = conv.XmlToObject(xml1);
|
86
|
|
87
|
String xml2 = conv.ObjectToXml(user);
|
88
|
|
89
|
Assert.assertEquals(xml1, xml2);
|
90
|
System.out.println(xml1 + "\n\n\n");
|
91
|
schema = loadSchema("src/xsd/UserDSResourceType.xsd");
|
92
|
validateXml(schema, xml1);
|
93
|
}
|
94
|
|
95
|
public static void validateXml(Schema schema, String xml)
|
96
|
throws SAXException, IOException {
|
97
|
Validator validator = schema.newValidator();
|
98
|
SAXSource source = new SAXSource(new InputSource(
|
99
|
new ByteArrayInputStream(xml.getBytes())));
|
100
|
|
101
|
validator.validate(source);
|
102
|
}
|
103
|
|
104
|
private Schema loadSchema(String name) throws SAXException {
|
105
|
String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
|
106
|
SchemaFactory factory = SchemaFactory.newInstance(language);
|
107
|
Schema schema = factory.newSchema(new File(name));
|
108
|
|
109
|
return schema;
|
110
|
}
|
111
|
}
|