1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
58 |
|
|
59 |
|
@version |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (35) |
Complexity: 2 |
Complexity Density: 0.06 |
|
61 |
|
public class XWikiExtensionRepositoryTest |
62 |
|
{ |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private XWikiExtensionRepository repository; |
67 |
|
|
68 |
|
private Unmarshaller unmarshaller = mock(Unmarshaller.class); |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
|
70 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
97 |
1 |
@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 |
|
|
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 |
|
} |