1 | package clarin.cmdi.componentregistry.rest; |
---|
2 | |
---|
3 | import java.io.ByteArrayInputStream; |
---|
4 | import java.io.ByteArrayOutputStream; |
---|
5 | import java.io.IOException; |
---|
6 | import java.io.InputStream; |
---|
7 | import java.io.UnsupportedEncodingException; |
---|
8 | import java.text.ParseException; |
---|
9 | import java.util.regex.Matcher; |
---|
10 | import java.util.regex.Pattern; |
---|
11 | |
---|
12 | import javax.xml.bind.JAXBException; |
---|
13 | |
---|
14 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
15 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
16 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
17 | import clarin.cmdi.componentregistry.impl.database.UserDao; |
---|
18 | import clarin.cmdi.componentregistry.model.Comment; |
---|
19 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
20 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
21 | import clarin.cmdi.componentregistry.model.UserMapping.User; |
---|
22 | import java.security.Principal; |
---|
23 | |
---|
24 | /** |
---|
25 | * Static helper methods to be used in tests |
---|
26 | * |
---|
27 | */ |
---|
28 | public final class RegistryTestHelper { |
---|
29 | |
---|
30 | private RegistryTestHelper() { |
---|
31 | } |
---|
32 | |
---|
33 | public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException { |
---|
34 | return addComponent(testRegistry, id, getComponentTestContent()); |
---|
35 | } |
---|
36 | |
---|
37 | public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, String content) throws ParseException, |
---|
38 | JAXBException, UnsupportedEncodingException { |
---|
39 | return addComponent(testRegistry, id, new ByteArrayInputStream(content.getBytes("UTF-8"))); |
---|
40 | } |
---|
41 | |
---|
42 | private static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException, |
---|
43 | JAXBException { |
---|
44 | ComponentDescription desc = ComponentDescription.createNewDescription(); |
---|
45 | desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName()); |
---|
46 | desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName()); |
---|
47 | desc.setName(id); |
---|
48 | desc.setDescription("Test Description"); |
---|
49 | desc.setId(ComponentRegistry.REGISTRY_ID + id); |
---|
50 | desc.setHref("link:" + ComponentRegistry.REGISTRY_ID + id); |
---|
51 | CMDComponentSpec spec = MDMarshaller.unmarshal(CMDComponentSpec.class, content, MDMarshaller.getCMDComponentSchema()); |
---|
52 | testRegistry.register(desc, spec); |
---|
53 | return desc; |
---|
54 | } |
---|
55 | |
---|
56 | public static int updateComponent(ComponentRegistry testRegistry, ComponentDescription description, String content) throws JAXBException { |
---|
57 | return testRegistry.update(description, getComponentFromString(content), DummyPrincipal.DUMMY_CREDENTIALS.getPrincipal(), true); |
---|
58 | } |
---|
59 | |
---|
60 | public static String getProfileTestContentString() { |
---|
61 | return getProfileTestContentString("Actor"); |
---|
62 | } |
---|
63 | |
---|
64 | private static String getProfileTestContentString(String name) { |
---|
65 | String profileContent = ""; |
---|
66 | profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
67 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
68 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
69 | profileContent += " <Header />\n"; |
---|
70 | profileContent += " <CMD_Component name=\"" + name + "\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; |
---|
71 | profileContent += " <AttributeList>\n"; |
---|
72 | profileContent += " <Attribute>\n"; |
---|
73 | profileContent += " <Name>Name</Name>\n"; |
---|
74 | profileContent += " <Type>string</Type>\n"; |
---|
75 | profileContent += " </Attribute>\n"; |
---|
76 | profileContent += " </AttributeList>\n"; |
---|
77 | profileContent += " <CMD_Element name=\"Age\">\n"; |
---|
78 | profileContent += " <ValueScheme>\n"; |
---|
79 | profileContent += " <pattern>[23][0-9]</pattern>\n"; |
---|
80 | profileContent += " </ValueScheme>\n"; |
---|
81 | profileContent += " </CMD_Element>\n"; |
---|
82 | profileContent += " </CMD_Component>\n"; |
---|
83 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
84 | return profileContent; |
---|
85 | } |
---|
86 | |
---|
87 | public static InputStream getTestProfileContent() { |
---|
88 | return getTestProfileContent("Actor"); |
---|
89 | } |
---|
90 | |
---|
91 | public static InputStream getTestProfileContent(String name) { |
---|
92 | return new ByteArrayInputStream(getProfileTestContentString(name).getBytes()); |
---|
93 | } |
---|
94 | |
---|
95 | public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException { |
---|
96 | return addProfile(testRegistry, id, RegistryTestHelper.getTestProfileContent()); |
---|
97 | } |
---|
98 | |
---|
99 | public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, String content) throws ParseException, |
---|
100 | JAXBException { |
---|
101 | return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes())); |
---|
102 | } |
---|
103 | |
---|
104 | private static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException, |
---|
105 | JAXBException { |
---|
106 | ProfileDescription desc = ProfileDescription.createNewDescription(); |
---|
107 | desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName()); |
---|
108 | desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName()); |
---|
109 | desc.setName(id); |
---|
110 | desc.setDescription("Test Description"); |
---|
111 | desc.setId(ComponentRegistry.REGISTRY_ID + id); |
---|
112 | desc.setHref("link:" + ComponentRegistry.REGISTRY_ID + id); |
---|
113 | CMDComponentSpec spec = MDMarshaller.unmarshal(CMDComponentSpec.class, content, MDMarshaller.getCMDComponentSchema()); |
---|
114 | testRegistry.register(desc, spec); |
---|
115 | return desc; |
---|
116 | } |
---|
117 | |
---|
118 | public static CMDComponentSpec getTestProfile() throws JAXBException { |
---|
119 | return MDMarshaller.unmarshal(CMDComponentSpec.class, getTestProfileContent(), MDMarshaller.getCMDComponentSchema()); |
---|
120 | } |
---|
121 | |
---|
122 | public static String getComponentTestContentString() { |
---|
123 | return getComponentTestContentString("Access"); |
---|
124 | } |
---|
125 | |
---|
126 | public static InputStream getComponentTestContent() { |
---|
127 | return getComponentTestContent("Access"); |
---|
128 | } |
---|
129 | |
---|
130 | public static String getComponentTestContentString(String componentName) { |
---|
131 | String compContent = ""; |
---|
132 | compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
133 | compContent += "\n"; |
---|
134 | compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
135 | compContent += " xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n"; |
---|
136 | compContent += " \n"; |
---|
137 | compContent += " <Header/>\n"; |
---|
138 | compContent += " \n"; |
---|
139 | compContent += " <CMD_Component name=\"" + componentName + "\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n"; |
---|
140 | compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
141 | compContent += " <CMD_Element name=\"Date\">\n"; |
---|
142 | compContent += " <ValueScheme>\n"; |
---|
143 | compContent += " <!-- matching dates of the pattern yyyy-mm-dd (ISO 8601); this only matches dates from the years 1000 through 2999 and does allow some invalid dates (e.g. February, the 30th) -->\n"; |
---|
144 | compContent += " <pattern>(1|2)\\d{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])</pattern> \n"; |
---|
145 | compContent += " </ValueScheme>\n"; |
---|
146 | compContent += " </CMD_Element>\n"; |
---|
147 | compContent += " <CMD_Element name=\"Owner\" ValueScheme=\"string\" />\n"; |
---|
148 | compContent += " <CMD_Element name=\"Publisher\" ValueScheme=\"string\" />\n"; |
---|
149 | compContent += " </CMD_Component>\n"; |
---|
150 | compContent += "\n"; |
---|
151 | compContent += "</CMD_ComponentSpec>\n"; |
---|
152 | return compContent; |
---|
153 | } |
---|
154 | |
---|
155 | public static CMDComponentSpec getComponentFromString(String contentString) throws JAXBException { |
---|
156 | return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentContent(contentString), MDMarshaller.getCMDComponentSchema()); |
---|
157 | } |
---|
158 | |
---|
159 | public static InputStream getComponentContent(String content) { |
---|
160 | return new ByteArrayInputStream(content.getBytes()); |
---|
161 | } |
---|
162 | |
---|
163 | public static InputStream getComponentTestContent(String componentName) { |
---|
164 | return getComponentContent(getComponentTestContentString(componentName)); |
---|
165 | } |
---|
166 | |
---|
167 | public static CMDComponentSpec getTestComponent() throws JAXBException { |
---|
168 | return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), MDMarshaller.getCMDComponentSchema()); |
---|
169 | } |
---|
170 | |
---|
171 | public static CMDComponentSpec getTestComponent(String name) throws JAXBException { |
---|
172 | return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(name), MDMarshaller.getCMDComponentSchema()); |
---|
173 | } |
---|
174 | |
---|
175 | public static String getXml(CMDComponentSpec componentSpec) throws JAXBException, UnsupportedEncodingException { |
---|
176 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
---|
177 | MDMarshaller.marshal(componentSpec, os); |
---|
178 | String xml = os.toString(); |
---|
179 | try { |
---|
180 | os.close(); |
---|
181 | } catch (IOException ex) { |
---|
182 | } |
---|
183 | return xml; |
---|
184 | } |
---|
185 | |
---|
186 | public static Comment addComment(ComponentRegistry testRegistry, String id, String descriptionId, String principal) throws ParseException, JAXBException { |
---|
187 | return addComment(testRegistry, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal); |
---|
188 | } |
---|
189 | |
---|
190 | private static Comment addComment(ComponentRegistry testRegistry, InputStream content, String principal) throws ParseException, |
---|
191 | JAXBException { |
---|
192 | Comment spec = MDMarshaller.unmarshal(Comment.class, content, null); |
---|
193 | testRegistry.registerComment(spec, principal); |
---|
194 | return spec; |
---|
195 | } |
---|
196 | |
---|
197 | public static String getCommentTestContentString(String commentName, String profileId) { |
---|
198 | String comContent = ""; |
---|
199 | comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
---|
200 | comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; |
---|
201 | comContent += " <comments>" + commentName + "</comments>\n"; |
---|
202 | comContent += " <commentDate>" + Comment.createNewDate() + "</commentDate>\n"; |
---|
203 | comContent += " <profileDescriptionId>" + profileId + "</profileDescriptionId>\n"; |
---|
204 | comContent += " <userId>0</userId>\n"; |
---|
205 | comContent += " <id>1</id>\n"; |
---|
206 | comContent += "</comment>\n"; |
---|
207 | return comContent; |
---|
208 | } |
---|
209 | |
---|
210 | public static String getCommentTestContentStringForComponent(String commentName, String componentId) { |
---|
211 | String comContent = ""; |
---|
212 | comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
---|
213 | comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; |
---|
214 | comContent += " <comments>" + commentName + "</comments>\n"; |
---|
215 | comContent += " <commentDate>" + Comment.createNewDate() + "</commentDate>\n"; |
---|
216 | comContent += " <componentDescriptionId>" + componentId + "</componentDescriptionId>"; |
---|
217 | comContent += " <userId>0</userId>\n"; |
---|
218 | comContent += " <id>1</id>\n"; |
---|
219 | comContent += "</comment>\n"; |
---|
220 | return comContent; |
---|
221 | } |
---|
222 | |
---|
223 | public static InputStream getTestCommentContent(String content, String descriptionId) { |
---|
224 | if (descriptionId.contains("profile")) { |
---|
225 | return new ByteArrayInputStream(getCommentTestContentString(content, descriptionId).getBytes()); |
---|
226 | } else { |
---|
227 | return new ByteArrayInputStream(getCommentTestContentStringForComponent(content, descriptionId).getBytes()); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | public static String getCommentTestContent(String commentId, String descriptionId) { |
---|
232 | return getCommentTestContentString(commentId, descriptionId); |
---|
233 | } |
---|
234 | |
---|
235 | public static InputStream getCommentTestContent() { |
---|
236 | return getTestCommentContent("Actual", "profile1"); |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * Testing a big xsd string is a bit hard, so doing a best effort by checking the xs:element which represent the nested components used |
---|
241 | * in a profile/component |
---|
242 | */ |
---|
243 | public static boolean hasComponent(String xsd, String name, String min, String max) { |
---|
244 | Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">"); |
---|
245 | Matcher matcher = pattern.matcher(xsd); |
---|
246 | return matcher.find() && !matcher.find(); //find only one |
---|
247 | } |
---|
248 | } |
---|