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

File XWikiExtensionRepositoryTest.java

 

Code metrics

0
33
2
1
121
77
2
0.06
16.5
2
1

Classes

Class Line # Actions
XWikiExtensionRepositoryTest 61 33 0% 2 0
1.0100%
 

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.extension.repository.xwiki.internal;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.InputStream;
24    import java.net.URI;
25    import java.util.ArrayList;
26    import java.util.Arrays;
27    import java.util.List;
28   
29    import javax.xml.bind.Unmarshaller;
30   
31    import org.apache.http.HttpEntity;
32    import org.apache.http.HttpStatus;
33    import org.apache.http.StatusLine;
34    import org.apache.http.client.methods.CloseableHttpResponse;
35    import org.apache.http.client.methods.HttpGet;
36    import org.apache.http.impl.client.CloseableHttpClient;
37    import org.junit.Before;
38    import org.junit.Test;
39    import org.xwiki.extension.ExtensionLicenseManager;
40    import org.xwiki.extension.internal.ExtensionFactory;
41    import org.xwiki.extension.repository.ExtensionRepositoryDescriptor;
42    import org.xwiki.extension.repository.http.internal.HttpClientFactory;
43    import org.xwiki.extension.repository.result.IterableResult;
44    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersionSummary;
45    import org.xwiki.extension.repository.xwiki.model.jaxb.ExtensionVersions;
46    import org.xwiki.extension.version.Version;
47    import org.xwiki.extension.version.internal.DefaultVersion;
48   
49    import com.google.common.collect.Iterators;
50   
51    import static org.junit.Assert.assertEquals;
52    import static org.mockito.ArgumentMatchers.any;
53    import static org.mockito.Mockito.mock;
54    import static org.mockito.Mockito.when;
55   
56    /**
57    * Unit tests for {@link XWikiExtensionRepository}.
58    *
59    * @version $Id: 222dc59b0fc999a570510395232f633b7a7abec9 $
60    */
 
61    public class XWikiExtensionRepositoryTest
62    {
63    /**
64    * The object being tested.
65    */
66    private XWikiExtensionRepository repository;
67   
68    private Unmarshaller unmarshaller = mock(Unmarshaller.class);
69   
 
70  1 toggle @Before
71    public void setUp() throws Exception
72    {
73  1 ExtensionRepositoryDescriptor repositoryDescriptor = mock(ExtensionRepositoryDescriptor.class);
74  1 when(repositoryDescriptor.getURI()).thenReturn(new URI("http://extensions.xwiki.org/xwiki/rest"));
75   
76  1 XWikiExtensionRepositoryFactory repositoryFactory = mock(XWikiExtensionRepositoryFactory.class);
77  1 when(repositoryFactory.createUnmarshaller()).thenReturn(this.unmarshaller);
78   
79    // Simulate a call to the remote URL through HttpClient and a valid answer
80  1 HttpEntity httpEntity = mock(HttpEntity.class);
81  1 when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("any content".getBytes()));
82  1 StatusLine statusLine = mock(StatusLine.class);
83  1 when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
84  1 CloseableHttpResponse response = mock(CloseableHttpResponse.class);
85  1 when(response.getStatusLine()).thenReturn(statusLine);
86  1 when(response.getEntity()).thenReturn(httpEntity);
87  1 CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
88  1 when(httpClient.execute(any(HttpGet.class))).thenReturn(response);
89  1 HttpClientFactory httpClientFactory = mock(HttpClientFactory.class);
90  1 when(httpClientFactory.createClient(null, null)).thenReturn(httpClient);
91  1 ExtensionFactory extensionFactory = new ExtensionFactory();
92   
93  1 this.repository = new XWikiExtensionRepository(repositoryDescriptor, repositoryFactory,
94    mock(ExtensionLicenseManager.class), httpClientFactory, extensionFactory);
95    }
96   
 
97  1 toggle @Test
98    public void resolveVersions() throws Exception
99    {
100  1 ExtensionVersionSummary v1 = mock(ExtensionVersionSummary.class);
101  1 when(v1.getVersion()).thenReturn("1.3");
102   
103  1 ExtensionVersionSummary v2 = mock(ExtensionVersionSummary.class);
104  1 when(v2.getVersion()).thenReturn("2.4.1");
105   
106  1 List<ExtensionVersionSummary> versionSummaries = Arrays.asList(v1, v2);
107  1 ExtensionVersions restVersions = mock(ExtensionVersions.class);
108  1 when(this.unmarshaller.unmarshal(any(InputStream.class))).thenReturn(restVersions);
109  1 when(restVersions.getExtensionVersionSummaries()).thenReturn(versionSummaries);
110  1 when(restVersions.getOffset()).thenReturn(5);
111  1 when(restVersions.getTotalHits()).thenReturn(7);
112   
113  1 IterableResult<Version> result = this.repository.resolveVersions("foo", 0, -1);
114    // The result must take the offset and total hits number from the REST response.
115  1 assertEquals(5, result.getOffset());
116  1 assertEquals(7, result.getTotalHits());
117  1 List<Version> versions = new ArrayList<Version>();
118  1 Iterators.addAll(versions, result.iterator());
119  1 assertEquals(Arrays.asList(new DefaultVersion("1.3"), new DefaultVersion("2.4.1")), versions);
120    }
121    }