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

File RepositoryTest.java

 

Code metrics

4
193
6
1
491
287
8
0.04
32.17
6
1.33

Classes

Class Line # Actions
RepositoryTest 56 193 0% 8 3
0.985221798.5%
 

Contributing tests

This file is covered by 1 test. .

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.test.ui.repository;
21   
22    import java.net.URL;
23    import java.util.Arrays;
24    import java.util.HashMap;
25    import java.util.Map;
26   
27    import org.apache.commons.httpclient.UsernamePasswordCredentials;
28    import org.apache.commons.io.FileUtils;
29    import org.junit.Assert;
30    import org.junit.Before;
31    import org.junit.Test;
32    import org.openqa.selenium.By;
33    import org.xwiki.extension.DefaultExtensionAuthor;
34    import org.xwiki.extension.DefaultExtensionDependency;
35    import org.xwiki.extension.ExtensionId;
36    import org.xwiki.extension.ExtensionLicense;
37    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionDependency;
38    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersion;
39    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionsSearchResult;
40    import org.xwiki.extension.version.internal.DefaultVersionConstraint;
41    import org.xwiki.repository.Resources;
42    import org.xwiki.repository.test.TestExtension;
43    import org.xwiki.repository.test.po.ExtensionImportPage;
44    import org.xwiki.repository.test.po.ExtensionPage;
45    import org.xwiki.repository.test.po.ExtensionsLiveTableElement;
46    import org.xwiki.repository.test.po.ExtensionsPage;
47    import org.xwiki.repository.test.po.RepositoryAdminPage;
48    import org.xwiki.repository.test.po.editor.ExtensionInlinePage;
49    import org.xwiki.test.ui.AbstractExtensionAdminAuthenticatedTest;
50   
51    /**
52    * Repository Test.
53    *
54    * @version $Id: 5b27ec57db380351f98306044817d0164571cf25 $
55    */
 
56    public class RepositoryTest extends AbstractExtensionAdminAuthenticatedTest
57    {
58    public static final UsernamePasswordCredentials USER_CREDENTIALS = new UsernamePasswordCredentials("Author",
59    "password");
60   
61    private static final String IDPREFIX = "prefix-";
62   
63    private TestExtension baseExtension;
64   
65    private ExtensionLicense baseLicense;
66   
67    private DefaultExtensionAuthor baseAuthor;
68   
69    private long sizeOfFile;
70   
 
71  1 toggle @Before
72    @Override
73    public void setUp() throws Exception
74    {
75  1 super.setUp();
76   
77    // base extension informations
78   
79  1 this.baseExtension =
80    getRepositoryTestUtils().getTestExtension(new ExtensionId(IDPREFIX + "macro-jar-extension", "1.0"), "jar");
81   
82  1 this.baseExtension.setName("Macro JAR extension");
83  1 this.baseExtension.setDescription("extension description");
84  1 this.baseExtension.setSummary("extension summary, **not bold**");
85   
86  1 this.baseLicense = new ExtensionLicense("Do What The Fuck You Want To Public License 2", null);
87  1 this.baseExtension.addLicense(this.baseLicense);
88   
89  1 this.baseAuthor =
90    new DefaultExtensionAuthor("User Name", new URL(getUtil().getURL("XWiki", USER_CREDENTIALS.getUserName())));
91  1 this.baseExtension.addAuthor(this.baseAuthor);
92   
93  1 this.baseExtension.addDependency(new DefaultExtensionDependency("dependencyid1", new DefaultVersionConstraint(
94    "1.0")));
95  1 this.baseExtension.addDependency(new DefaultExtensionDependency("dependencyid2", new DefaultVersionConstraint(
96    "2.0")));
97   
98  1 this.sizeOfFile = FileUtils.sizeOf(this.baseExtension.getFile().getFile());
99    }
100   
 
101  1 toggle @Test
102    public void validateAllFeatures() throws Exception
103    {
104  1 addExtension();
105  1 importExtension();
106  1 validateRecommendations();
107    }
108   
 
109  1 toggle private void addExtension() throws Exception
110    {
111    // Set id prefix
112   
113  1 RepositoryAdminPage repositoryAdminPage = RepositoryAdminPage.gotoPage();
114   
115  1 repositoryAdminPage.setDefaultIdPrefix(IDPREFIX);
116  1 repositoryAdminPage.clickUpdateButton();
117   
118    // Create extension
119   
120  1 getUtil().createUserAndLogin(USER_CREDENTIALS.getUserName(), USER_CREDENTIALS.getPassword(), "first_name",
121    "User", "last_name", "Name");
122   
123  1 ExtensionsPage extensionsPage = ExtensionsPage.gotoPage();
124   
125  1 ExtensionInlinePage extensionInline = extensionsPage.contributeExtension(this.baseExtension.getName());
126   
127  1 Assert.assertEquals(this.baseExtension.getName(), extensionInline.getName());
128   
129  1 extensionInline.setDescription(this.baseExtension.getDescription());
130  1 extensionInline.setInstallation("extension installation");
131  1 extensionInline.setLicenseName(this.baseLicense.getName());
132  1 extensionInline.setSource("http://source");
133  1 extensionInline.setSummary(this.baseExtension.getSummary());
134  1 extensionInline.setType(this.baseExtension.getType());
135   
136  1 ExtensionPage extensionPage = extensionInline.clickSaveAndView();
137   
138    // Test summary
139  1 getDriver().findElementsWithoutWaiting(By.xpath("//tt[text()=\"" + this.baseExtension.getSummary() + "\"]"));
140   
141  1 Assert.assertFalse(extensionPage.isValidExtension());
142   
143    // Add versions
144    // TODO: add XR UI to manipulate versions
145  1 getRepositoryTestUtils().addVersionObject(this.baseExtension);
146  1 getRepositoryTestUtils().addVersionObject(
147    this.baseExtension,
148    "10.0",
149    getUtil().getAttachmentURL("Extension", this.baseExtension.getName(),
150    this.baseExtension.getFile().getName()));
151  1 getRepositoryTestUtils().addVersionObject(this.baseExtension, "2.0",
152    "attach:" + this.baseExtension.getFile().getName());
153   
154    // Add dependencies
155    // TODO: add XR UI to manipulate dependencies
156  1 getRepositoryTestUtils().addDependencies(this.baseExtension, "10.0");
157   
158    // Add attachment
159  1 getRepositoryTestUtils().attachFile(this.baseExtension);
160   
161    // Wait until all asynchronous tasks are done
162  1 getRepositoryTestUtils().waitUntilReady();
163   
164    // Check livetable
165   
166  1 extensionsPage = ExtensionsPage.gotoPage();
167   
168  1 ExtensionsLiveTableElement livetable = extensionsPage.getLiveTable();
169   
170  1 livetable.filterName(this.baseExtension.getName());
171   
172  1 extensionPage = livetable.clickExtensionName(this.baseExtension.getName());
173   
174    // Validate extension state
175   
176  1 Assert.assertTrue(extensionPage.isValidExtension());
177   
178    // //////////////////////////////////////////
179    // Validate REST
180    // //////////////////////////////////////////
181   
182    // //////////////////////////////////////////
183    // 1.0
184    // //////////////////////////////////////////
185   
186    // Resolve
187   
188  1 ExtensionVersion extension =
189    getUtil().rest().getResource(Resources.EXTENSION_VERSION, null, this.baseExtension.getId().getId(), "1.0");
190   
191  1 Assert.assertEquals(this.baseExtension.getId().getId(), extension.getId());
192  1 Assert.assertEquals(this.baseExtension.getType(), extension.getType());
193  1 Assert.assertEquals(this.baseExtension.getSummary(), extension.getSummary());
194  1 Assert.assertEquals(this.baseLicense.getName(), extension.getLicenses().get(0).getName());
195  1 Assert.assertEquals(this.baseExtension.getDescription(), extension.getDescription());
196  1 Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
197  1 Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
198  1 Assert.assertEquals("1.0", extension.getVersion());
199   
200  1 Assert.assertEquals(getUtil().getURL(Arrays.asList("Extension", this.baseExtension.getName()), ""),
201    extension.getWebsite());
202   
203    // File
204   
205  1 Assert.assertEquals(
206    this.sizeOfFile,
207    getUtil().rest().getBuffer(Resources.EXTENSION_VERSION_FILE, null, this.baseExtension.getId().getId(),
208    "1.0").length);
209   
210    // //////////////////////////////////////////
211    // 2.0
212    // //////////////////////////////////////////
213   
214    // Resolve
215   
216  1 extension =
217    getUtil().rest().getResource(Resources.EXTENSION_VERSION, null, this.baseExtension.getId().getId(), "2.0");
218   
219  1 Assert.assertEquals(this.baseExtension.getId().getId(), extension.getId());
220  1 Assert.assertEquals(this.baseExtension.getType(), extension.getType());
221  1 Assert.assertEquals(this.baseExtension.getSummary(), extension.getSummary());
222  1 Assert.assertEquals(this.baseLicense.getName(), extension.getLicenses().get(0).getName());
223  1 Assert.assertEquals(this.baseExtension.getDescription(), extension.getDescription());
224  1 Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
225  1 Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
226  1 Assert.assertEquals("2.0", extension.getVersion());
227   
228  1 Assert.assertEquals(getUtil().getURL(Arrays.asList("Extension", this.baseExtension.getName()), ""),
229    extension.getWebsite());
230   
231    // File
232   
233  1 Assert.assertEquals(
234    this.sizeOfFile,
235    getUtil().rest().getBuffer(Resources.EXTENSION_VERSION_FILE, null, this.baseExtension.getId().getId(),
236    "2.0").length);
237   
238    // //////////////////////////////////////////
239    // 10.0
240    // //////////////////////////////////////////
241   
242    // Resolve
243   
244  1 extension =
245    getUtil().rest().getResource(Resources.EXTENSION_VERSION, null, this.baseExtension.getId().getId(), "10.0");
246   
247  1 Assert.assertEquals(this.baseExtension.getId().getId(), extension.getId());
248  1 Assert.assertEquals(this.baseExtension.getType(), extension.getType());
249  1 Assert.assertEquals(this.baseExtension.getSummary(), extension.getSummary());
250  1 Assert.assertEquals(this.baseLicense.getName(), extension.getLicenses().get(0).getName());
251  1 Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
252  1 Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
253  1 Assert.assertEquals("10.0", extension.getVersion());
254   
255  1 Assert.assertEquals(getUtil().getURL(Arrays.asList("Extension", this.baseExtension.getName()), ""),
256    extension.getWebsite());
257   
258  1 ExtensionDependency dependency1 = extension.getDependencies().get(0);
259  1 Assert.assertEquals("dependencyid1", dependency1.getId());
260  1 Assert.assertEquals("1.0", dependency1.getConstraint());
261  1 ExtensionDependency dependency2 = extension.getDependencies().get(1);
262  1 Assert.assertEquals("dependencyid2", dependency2.getId());
263  1 Assert.assertEquals("2.0", dependency2.getConstraint());
264   
265    // File
266   
267  1 Assert.assertEquals(
268    this.sizeOfFile,
269    getUtil().rest().getBuffer(Resources.EXTENSION_VERSION_FILE, null, this.baseExtension.getId().getId(),
270    this.baseExtension.getId().getVersion().getValue()).length);
271   
272    // //////////////////////////////////////////
273    // Search
274    // //////////////////////////////////////////
275   
276    // Empty search
277  1 extension = searchExtension(this.baseExtension.getId().getId());
278   
279  1 if (extension == null) {
280  0 Assert.fail("Could not find extension [" + this.baseExtension.getId().getId() + "]");
281    }
282   
283  1 Assert.assertEquals(this.baseExtension.getId().getId(), extension.getId());
284  1 Assert.assertEquals(this.baseExtension.getType(), extension.getType());
285  1 Assert.assertEquals(this.baseExtension.getSummary(), extension.getSummary());
286  1 Assert.assertEquals(this.baseLicense.getName(), extension.getLicenses().get(0).getName());
287  1 Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
288  1 Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
289  1 Assert.assertEquals("10.0", extension.getVersion());
290   
291    // TODO: add support for dependencies in XR search
292   
293    // Search pattern
294   
295  1 Map<String, Object[]> queryParams = new HashMap<String, Object[]>();
296  1 queryParams.put(Resources.QPARAM_SEARCH_QUERY, new Object[] {"macro"});
297   
298  1 ExtensionsSearchResult result = getUtil().rest().getResource(Resources.SEARCH, queryParams);
299   
300  1 Assert.assertEquals(1, result.getTotalHits());
301  1 Assert.assertEquals(0, result.getOffset());
302  1 extension = result.getExtensions().get(0);
303   
304  1 Assert.assertEquals(this.baseExtension.getId().getId(), extension.getId());
305  1 Assert.assertEquals(this.baseExtension.getType(), extension.getType());
306  1 Assert.assertEquals(this.baseExtension.getSummary(), extension.getSummary());
307  1 Assert.assertEquals(this.baseLicense.getName(), extension.getLicenses().get(0).getName());
308  1 Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
309  1 Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
310  1 Assert.assertEquals("10.0", extension.getVersion());
311   
312    // Wrong search pattern
313   
314  1 queryParams.clear();
315  1 queryParams.put(Resources.QPARAM_SEARCH_QUERY, new Object[] {"notexisting"});
316   
317  1 result = getUtil().rest().getResource(Resources.SEARCH, queryParams);
318   
319  1 Assert.assertEquals(0, result.getTotalHits());
320  1 Assert.assertEquals(0, result.getOffset());
321  1 Assert.assertEquals(0, result.getExtensions().size());
322   
323    // Search limit offset
324   
325  1 queryParams.clear();
326  1 queryParams.put(Resources.QPARAM_LIST_START, new Object[] {1});
327   
328  1 result = getUtil().rest().getResource(Resources.SEARCH, queryParams);
329   
330  1 Assert.assertEquals(1, result.getOffset());
331  1 Assert.assertEquals(result.getTotalHits() - 1, result.getExtensions().size());
332   
333    // Search limit nb
334   
335  1 queryParams.clear();
336  1 queryParams.put(Resources.QPARAM_LIST_NUMBER, new Object[] {0});
337   
338  1 result = getUtil().rest().getResource(Resources.SEARCH, queryParams);
339   
340  1 Assert.assertTrue(result.getTotalHits() >= 1);
341  1 Assert.assertEquals(0, result.getOffset());
342  1 Assert.assertEquals(0, result.getExtensions().size());
343    }
344   
 
345  1 toggle private ExtensionVersion searchExtension(String id) throws Exception
346    {
347  1 Map<String, Object[]> queryParams = new HashMap<String, Object[]>();
348  1 ExtensionsSearchResult result = getUtil().rest().getResource(Resources.SEARCH, queryParams);
349   
350  1 Assert.assertTrue(result.getTotalHits() >= 0);
351  1 Assert.assertEquals(0, result.getOffset());
352   
353  1 ExtensionVersion extension = null;
354  1 for (ExtensionVersion extensionVersion : result.getExtensions()) {
355  1 if (extensionVersion.getId().equals(id)) {
356  1 extension = extensionVersion;
357  1 break;
358    }
359    }
360   
361  1 return extension;
362    }
363   
 
364  1 toggle private void importExtension() throws Exception
365    {
366  1 ExtensionsPage extensionsPage = ExtensionsPage.gotoPage();
367   
368  1 ExtensionImportPage importPage = extensionsPage.clickImport();
369   
370  1 importPage.setExtensionId("maven:extension");
371  1 importPage.setSourceRepository("maven-test");
372  1 ExtensionPage extensionPage = importPage.clickImport();
373   
374    // Check
375   
376  1 Assert.assertEquals("1.1", extensionPage.getMetaDataValue("version"));
377  1 Assert.assertTrue(extensionPage.isValidExtension());
378   
379    // 2.0
380   
381  1 TestExtension emptyExtension =
382    getRepositoryTestUtils().getTestExtension(new ExtensionId("emptyjar", "1.0"), "jar");
383   
384  1 long fileSize = FileUtils.sizeOf(emptyExtension.getFile().getFile());
385   
386  1 ExtensionVersion extension =
387    getUtil().rest().getResource(Resources.EXTENSION_VERSION, null, "maven:extension", "2.0");
388   
389  1 Assert.assertEquals("maven:extension", extension.getId());
390  1 Assert.assertEquals("jar", extension.getType());
391  1 Assert.assertEquals("2.0", extension.getVersion());
392  1 Assert.assertEquals("name", extension.getName());
393  1 Assert.assertEquals("summary2", extension.getSummary());
394  1 Assert.assertEquals("summary2\n some more details", extension.getDescription());
395  1 Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
396  1 Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
397  1 Assert.assertEquals(Arrays.asList("maven:oldextension/2.0"), extension.getFeatures());
398  1 Assert.assertEquals("GNU Lesser General Public License 2.1", extension.getLicenses().get(0).getName());
399   
400  1 Assert.assertEquals(fileSize,
401    getUtil().rest().getBuffer(Resources.EXTENSION_VERSION_FILE, null, "maven:extension", "2.0").length);
402   
403    // 1.0
404   
405  1 extension = getUtil().rest().getResource(Resources.EXTENSION_VERSION, null, "maven:extension", "1.0");
406   
407  1 Assert.assertEquals("maven:extension", extension.getId());
408  1 Assert.assertEquals("jar", extension.getType());
409  1 Assert.assertEquals("1.0", extension.getVersion());
410  1 Assert.assertEquals("name", extension.getName());
411  1 Assert.assertEquals("summary2", extension.getSummary());
412  1 Assert.assertEquals("summary2\n some more details", extension.getDescription());
413  1 Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
414  1 Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
415  1 Assert.assertEquals(Arrays.asList("maven:oldextension/1.0"), extension.getFeatures());
416  1 Assert.assertEquals("GNU Lesser General Public License 2.1", extension.getLicenses().get(0).getName());
417   
418  1 Assert.assertEquals(FileUtils.sizeOf(emptyExtension.getFile().getFile()),
419    getUtil().rest().getBuffer(Resources.EXTENSION_VERSION_FILE, null, "maven:extension", "1.0").length);
420   
421    // 0.9
422   
423  1 extension = getUtil().rest().getResource(Resources.EXTENSION_VERSION, null, "maven:extension", "0.9");
424   
425  1 Assert.assertEquals("maven:extension", extension.getId());
426  1 Assert.assertEquals("jar", extension.getType());
427  1 Assert.assertEquals("0.9", extension.getVersion());
428  1 Assert.assertEquals("name", extension.getName());
429  1 Assert.assertEquals("summary2", extension.getSummary());
430  1 Assert.assertEquals("summary2\n some more details", extension.getDescription());
431  1 Assert.assertEquals(this.baseAuthor.getName(), extension.getAuthors().get(0).getName());
432  1 Assert.assertEquals(this.baseAuthor.getURL().toString(), extension.getAuthors().get(0).getUrl());
433  1 Assert.assertEquals(Arrays.asList(), extension.getFeatures());
434  1 Assert.assertEquals("GNU Lesser General Public License 2.1", extension.getLicenses().get(0).getName());
435   
436  1 Assert.assertEquals(fileSize,
437    getUtil().rest().getBuffer(Resources.EXTENSION_VERSION_FILE, null, "maven:extension", "0.9").length);
438   
439    // Import again
440   
441  1 extensionPage = extensionPage.updateExtension();
442   
443  1 Assert.assertEquals("1.1", extensionPage.getMetaDataValue("version"));
444    }
445   
 
446  1 toggle private void validateRecommendations() throws Exception
447    {
448    // At this stage we assume that we have 2 Extensions in the Repository app and we'll make one of them
449    // Recommended.
450    // Then we'll turn on Recommendations and verify that the home page lists only the Recommended one.
451    // Then we'll click on the Browse button to list all extensions and verify we have 2 in the list.
452    // Then we'll click again on the Browse button to list only Recommended extensions and verify we have 1
453    // in the list.
454    // Last we'll navigate to an extension and verify that when clicking the breadcrumb we're still on the
455    // Recommended list.
456   
457    // Note: We don't yet offer a UI for changing the Recommended flag for an Extension.
458  1 getUtil().updateObject(Arrays.asList("Extension", "Macro JAR extension"), "WebHome",
459    "ExtensionCode.ExtensionClass", 0, "recommended", 1);
460   
461    // Turn on Recommendation. There's also no Admin UI yet for this.
462  1 getUtil().updateObject("ExtensionCode", "RepositoryConfig", "ExtensionCode.RepositoryConfigClass", 0,
463    "useRecommendations", 1);
464   
465    // Verify that the home page now lists only the Recommended extension.
466  1 ExtensionsPage extensionsPage = ExtensionsPage.gotoPage();
467  1 ExtensionsLiveTableElement livetable = extensionsPage.getLiveTable();
468  1 Assert.assertEquals(1, livetable.getRowCount());
469   
470    // Click on Browse button to list All Extensions
471  1 extensionsPage = extensionsPage.clickBrowse();
472   
473    // Verify that the home page now lists all extensions.
474  1 livetable = extensionsPage.getLiveTable();
475  1 Assert.assertEquals(2, livetable.getRowCount());
476   
477    // Click on Browse button to list Recommended Extensions
478  1 extensionsPage = extensionsPage.clickBrowse();
479   
480    // Verify that the home page now lists only Recommended extensions.
481  1 livetable = extensionsPage.getLiveTable();
482  1 Assert.assertEquals(1, livetable.getRowCount());
483   
484    // Navigate to the extension and click the breadcrumb and verify that we're listing only Recommended extensions
485  1 ExtensionPage extensionPage = livetable.clickExtensionName("Macro JAR extension");
486  1 extensionPage.clickBreadcrumbLink("Extensions");
487  1 extensionsPage = new ExtensionsPage();
488  1 livetable = extensionsPage.getLiveTable();
489  1 Assert.assertEquals(1, livetable.getRowCount());
490    }
491    }