1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.repository.test

File RepositoryTestUtils.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

26
80
22
1
322
209
36
0.45
3.64
22
1.64

Classes

Class Line # Actions
RepositoryTestUtils 50 80 0% 36 19
0.851562585.2%
 

Contributing tests

This file is covered by 12 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.repository.test;
21   
22    import java.io.File;
23    import java.io.InputStream;
24    import java.util.ArrayList;
25    import java.util.Arrays;
26    import java.util.HashMap;
27    import java.util.List;
28    import java.util.Map;
29   
30    import org.xwiki.extension.Extension;
31    import org.xwiki.extension.ExtensionAuthor;
32    import org.xwiki.extension.ExtensionDependency;
33    import org.xwiki.extension.ExtensionId;
34    import org.xwiki.extension.RemoteExtension;
35    import org.xwiki.extension.internal.converter.ExtensionIdConverter;
36    import org.xwiki.extension.test.RepositoryUtils;
37    import org.xwiki.model.reference.LocalDocumentReference;
38    import org.xwiki.repository.internal.XWikiRepositoryModel;
39    import org.xwiki.rest.model.jaxb.Objects;
40    import org.xwiki.rest.model.jaxb.Page;
41    import org.xwiki.test.ui.TestUtils;
42   
43    import static org.xwiki.test.ui.TestUtils.RestTestUtils.object;
44    import static org.xwiki.test.ui.TestUtils.RestTestUtils.property;
45   
46    /**
47    * @version $Id: 06f676b943cf3c64705be18669a9983b27f6ade9 $
48    * @since 4.2M1
49    */
 
50    public class RepositoryTestUtils
51    {
52    public final static String PROPERTY_KEY = "repositoryutils";
53   
54    private final static String SPACENAME_EXTENSION = "Extension";
55   
56    /**
57    * @since 7.3M1
58    */
 
59  15 toggle public static org.xwiki.rest.model.jaxb.Object extensionObject(RemoteExtension extension)
60    {
61  15 org.xwiki.rest.model.jaxb.Object extensionObject = object(XWikiRepositoryModel.EXTENSION_CLASSNAME);
62   
63  15 extensionObject.getProperties()
64    .add(property(XWikiRepositoryModel.PROP_EXTENSION_ID, extension.getId().getId()));
65  15 extensionObject.getProperties().add(property(XWikiRepositoryModel.PROP_EXTENSION_TYPE, extension.getType()));
66  15 extensionObject.getProperties().add(property(XWikiRepositoryModel.PROP_EXTENSION_NAME, extension.getName()));
67  15 extensionObject.getProperties()
68    .add(property(XWikiRepositoryModel.PROP_EXTENSION_SUMMARY, extension.getSummary()));
69  15 extensionObject.getProperties()
70  15 .add(property(XWikiRepositoryModel.PROP_EXTENSION_RECOMMENDED, extension.isRecommended() ? 1 : 0));
71  15 if (!extension.getLicenses().isEmpty()) {
72  1 extensionObject.getProperties().add(property(XWikiRepositoryModel.PROP_EXTENSION_LICENSENAME,
73    extension.getLicenses().iterator().next().getName()));
74    }
75  15 extensionObject.getProperties().add(property(XWikiRepositoryModel.PROP_EXTENSION_FEATURES,
76    ExtensionIdConverter.toStringList(extension.getExtensionFeatures())));
77  15 List<String> authors = new ArrayList<String>();
78  15 for (ExtensionAuthor author : extension.getAuthors()) {
79  2 authors.add(author.getName());
80    }
81  15 extensionObject.getProperties().add(property(XWikiRepositoryModel.PROP_EXTENSION_AUTHORS, authors));
82  15 extensionObject.getProperties()
83    .add(property(XWikiRepositoryModel.PROP_EXTENSION_WEBSITE, extension.getWebSite()));
84  15 if (extension.getAllowedNamespaces() != null) {
85  0 extensionObject.getProperties()
86    .add(property(XWikiRepositoryModel.PROP_EXTENSION_ALLOWEDNAMESPACES, extension.getAllowedNamespaces()));
87  0 extensionObject.getProperties().add(property(XWikiRepositoryModel.PROP_EXTENSION_ALLOWEDNAMESPACES_EMPTY,
88  0 extension.getAllowedNamespaces() != null && extension.getAllowedNamespaces().isEmpty() ? 1 : 0));
89    }
90  15 if (extension.getScm() != null) {
91  1 extensionObject.getProperties()
92    .add(property(XWikiRepositoryModel.PROP_EXTENSION_SCMURL, extension.getScm().getUrl()));
93    }
94  15 return extensionObject;
95    }
96   
97    /**
98    * @since 7.3M1
99    */
 
100  15 toggle public static org.xwiki.rest.model.jaxb.Object extensionVersionObject(Extension extension)
101    {
102  15 org.xwiki.rest.model.jaxb.Object extensionVersionObject = extensionVersionObject(extension.getId().getVersion(),
103    null, XWikiRepositoryModel.toStringList(extension.getRepositories()));
104   
105  15 extensionVersionObject.getProperties().add(property(XWikiRepositoryModel.PROP_VERSION_FEATURES,
106    ExtensionIdConverter.toStringList(extension.getExtensionFeatures())));
107   
108  15 return extensionVersionObject;
109    }
110   
111    /**
112    * @since 7.3M1
113    */
 
114  15 toggle public static org.xwiki.rest.model.jaxb.Object extensionVersionObject(Object version, Object download,
115    Object repositories)
116    {
117  15 org.xwiki.rest.model.jaxb.Object versionObject = object(XWikiRepositoryModel.EXTENSIONVERSION_CLASSNAME);
118   
119  15 if (version != null) {
120  15 versionObject.getProperties().add(property(XWikiRepositoryModel.PROP_VERSION_VERSION, version));
121    }
122  15 if (download != null) {
123  0 versionObject.getProperties().add(property(XWikiRepositoryModel.PROP_VERSION_DOWNLOAD, download));
124    }
125  15 if (repositories != null) {
126  15 versionObject.getProperties().add(property(XWikiRepositoryModel.PROP_VERSION_REPOSITORIES, repositories));
127    }
128   
129  15 return versionObject;
130    }
131   
132    /**
133    * @since 7.3M1
134    */
 
135  15 toggle public static List<org.xwiki.rest.model.jaxb.Object> extensionDependencyObjects(Extension extension)
136    {
137  15 List<org.xwiki.rest.model.jaxb.Object> dependencies = new ArrayList<>(extension.getDependencies().size());
138   
139  15 int number = 0;
140  15 for (ExtensionDependency dependency : extension.getDependencies()) {
141  8 org.xwiki.rest.model.jaxb.Object dependencyObject =
142    extensionDependencyObject(extension.getId().getVersion(), dependency);
143  8 dependencyObject.setNumber(number++);
144   
145  8 dependencies.add(dependencyObject);
146    }
147   
148  15 return dependencies;
149    }
150   
151    /**
152    * @since 7.3M1
153    */
 
154  8 toggle public static org.xwiki.rest.model.jaxb.Object extensionDependencyObject(Object version,
155    ExtensionDependency dependency)
156    {
157  8 org.xwiki.rest.model.jaxb.Object dependencyObject = object(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSNAME);
158   
159  8 dependencyObject.getProperties()
160    .add(property(XWikiRepositoryModel.PROP_DEPENDENCY_CONSTRAINT, dependency.getVersionConstraint()));
161   
162  8 dependencyObject.getProperties().add(property(XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION, version));
163   
164  8 dependencyObject.getProperties().add(property(XWikiRepositoryModel.PROP_DEPENDENCY_ID, dependency.getId()));
165   
166  8 return dependencyObject;
167    }
168   
169    private final TestUtils testUtils;
170   
171    private RepositoryUtils repositoryUtil;
172   
173    private SolrTestUtils solrUtils;
174   
 
175  4 toggle public RepositoryTestUtils(TestUtils testUtils, RepositoryUtils repositoryUtil, SolrTestUtils solrUtils)
176    {
177  4 this.testUtils = testUtils;
178  4 this.repositoryUtil = repositoryUtil != null ? repositoryUtil : new RepositoryUtils();
179  4 this.solrUtils = solrUtils;
180    }
181   
 
182  20 toggle public RepositoryUtils getRepositoryUtil()
183    {
184  20 return this.repositoryUtil;
185    }
186   
187    // Test init
188   
 
189  4 toggle public void init() throws Exception
190    {
191    // Initialize extensions and repositories
192  4 this.repositoryUtil.setup();
193    }
194   
195    // Test utils
196   
 
197  24 toggle public TestExtension getTestExtension(ExtensionId id, String type)
198    {
199  24 File extensionFile = this.repositoryUtil.getExtensionPackager().getExtensionFile(id);
200   
201  24 return extensionFile != null ? new TestExtension(id, type, extensionFile) : null;
202    }
203   
 
204  57 toggle private LocalDocumentReference getExtensionPageReference(Extension extension)
205    {
206  57 String extensionName = extension.getName() != null ? extension.getName() : extension.getId().getId();
207   
208  57 return getExtensionPageReference(extensionName);
209    }
210   
211    /**
212    * @since 7.3M2
213    */
 
214  132 toggle private LocalDocumentReference getExtensionPageReference(String extensionName)
215    {
216  132 return new LocalDocumentReference(Arrays.asList(SPACENAME_EXTENSION, extensionName), "WebHome");
217    }
218   
 
219  0 toggle public void deleteExtension(Extension extension) throws Exception
220    {
221  0 this.testUtils.rest().delete(getExtensionPageReference(extension));
222    }
223   
224    /**
225    * @since 7.3M2
226    */
 
227  75 toggle public void deleteExtension(String extensionName) throws Exception
228    {
229  75 this.testUtils.rest().delete(getExtensionPageReference(extensionName));
230    }
231   
 
232  15 toggle public void addExtension(RemoteExtension extension) throws Exception
233    {
234    // Delete any pre existing extension
235  15 this.testUtils.rest().delete(getExtensionPageReference(extension));
236   
237    // Create Page
238  15 Page extensionPage = this.testUtils.rest().page(getExtensionPageReference(extension));
239   
240  15 extensionPage.setObjects(new Objects());
241   
242    // Add extension object
243  15 extensionPage.getObjects().getObjectSummaries().add(extensionObject(extension));
244   
245    // Add the ExtensionVersion object
246  15 extensionPage.getObjects().getObjectSummaries().add(extensionVersionObject(extension));
247   
248    // Add the ExtensionDependency objects
249  15 extensionPage.getObjects().getObjectSummaries().addAll(extensionDependencyObjects(extension));
250   
251    // Save the extension page
252  15 this.testUtils.rest().save(extensionPage, TestUtils.STATUS_CREATED);
253   
254    // Attach the extension file
255  15 attachFile(extension);
256    }
257   
 
258  1 toggle public void addVersionObject(Extension extension)
259    {
260  1 addVersionObject(extension, extension.getId().getVersion(), null);
261    }
262   
 
263  6 toggle public void addVersionObject(Extension extension, Object version, Object download)
264    {
265  6 Map<String, Object> queryParameters = new HashMap<String, Object>();
266   
267  6 if (version != null) {
268  6 queryParameters.put(XWikiRepositoryModel.PROP_VERSION_VERSION, version);
269    }
270  6 if (download != null) {
271  5 queryParameters.put(XWikiRepositoryModel.PROP_VERSION_DOWNLOAD, download);
272    }
273   
274  6 this.testUtils.addObject(getExtensionPageReference(extension), XWikiRepositoryModel.EXTENSIONVERSION_CLASSNAME,
275    queryParameters);
276    }
277   
 
278  0 toggle public void addDependencies(Extension extension)
279    {
280  0 addDependencies(extension, extension.getId().getVersion());
281    }
282   
 
283  1 toggle public void addDependencies(Extension extension, Object version)
284    {
285  1 for (ExtensionDependency dependency : extension.getDependencies()) {
286  2 addDependency(extension, version, dependency);
287    }
288    }
289   
 
290  0 toggle public void addDependency(Extension extension, ExtensionDependency dependency)
291    {
292  0 addDependency(extension, extension.getId().getVersion(), dependency);
293    }
294   
 
295  2 toggle public void addDependency(Extension extension, Object version, ExtensionDependency dependency)
296    {
297  2 this.testUtils.addObject(getExtensionPageReference(extension),
298    XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSNAME, XWikiRepositoryModel.PROP_DEPENDENCY_CONSTRAINT,
299    dependency.getVersionConstraint(), XWikiRepositoryModel.PROP_DEPENDENCY_ID, dependency.getId(),
300    XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION, version);
301    }
302   
 
303  19 toggle public void attachFile(Extension extension) throws Exception
304    {
305  19 InputStream is = extension.getFile().openStream();
306  19 try {
307  19 this.testUtils.attachFile(getExtensionPageReference(extension),
308    extension.getId().getId() + "-" + extension.getId().getVersion() + "." + extension.getType(), is, true);
309    } finally {
310  19 is.close();
311    }
312    }
313   
314    /**
315    * Make sure any change done in extension has been taken into account by Solr.
316    */
 
317  18 toggle public void waitUntilReady() throws Exception
318    {
319    // Make sure Solr queue is empty
320  18 this.solrUtils.waitEmpyQueue();
321    }
322    }