]> source.dussan.org Git - archiva.git/blob
b2471edfe8e9937e78341a05ab8f84d39d9dd4b7
[archiva.git] /
1 package org.apache.archiva.rest.services;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.archiva.admin.model.beans.RemoteRepository;
23 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
24 import org.apache.cxf.jaxrs.client.WebClient;
25 import org.junit.Test;
26
27 import javax.ws.rs.ForbiddenException;
28 import java.util.List;
29 import java.util.Locale;
30
31 /**
32  * @author Olivier Lamy
33  */
34 public class RemoteRepositoriesServiceTest
35     extends AbstractArchivaRestTest
36 {
37
38
39     private void removeRemoteRepositories(String... repos) {
40         try {
41             RemoteRepositoriesService service = getRemoteRepositoriesService();
42             WebClient.client( service ).header( "Authorization", authorizationHeader );
43             for (String repo : repos ) {
44                 try {
45                     service.deleteRemoteRepository(repo);
46                 } catch (Throwable ex) {
47                     log.warn("Could not remove repo {}", repo);
48                     // ignore
49                 }
50             }
51         } catch (Throwable ex) {
52             // ignore
53         }
54     }
55
56     @Test( expected = ForbiddenException.class )
57     public void listRemoteRepositoriesKarmaFailed()
58         throws Exception
59     {
60         RemoteRepositoriesService service = getRemoteRepositoriesService();
61         try
62         {
63             assertFalse( service.getRemoteRepositories().isEmpty() );
64         }
65         catch ( ForbiddenException e )
66         {
67             assertEquals( 403, e.getResponse().getStatus() );
68             throw e;
69         }
70     }
71
72     @Test
73     public void listRemoteRepositoriesKarma()
74         throws Exception
75     {
76         RemoteRepositoriesService service = getRemoteRepositoriesService();
77
78         WebClient.client( service ).header( "Authorization", authorizationHeader );
79         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
80         List<RemoteRepository> repos = service.getRemoteRepositories();
81         assertFalse( repos.isEmpty() );
82         log.info( "repos {}", repos );
83
84     }
85
86     @Test
87     public void addAndDeleteRemoteRepository()
88         throws Exception
89     {
90         RemoteRepositoriesService service = getRemoteRepositoriesService();
91
92         WebClient.client( service ).header( "Authorization", authorizationHeader );
93
94         int initialSize = service.getRemoteRepositories().size();
95
96         service.addRemoteRepository( getRemoteRepository() );
97
98         assertNotNull( service.getRemoteRepository( "id-new" ) );
99
100         assertEquals( getRemoteRepository().getName(), service.getRemoteRepository( "id-new" ).getName() );
101         assertEquals( getRemoteRepository().getUrl(), service.getRemoteRepository( "id-new" ).getUrl() );
102         assertEquals( getRemoteRepository().getLayout(), service.getRemoteRepository( "id-new" ).getLayout() );
103         assertEquals( getRemoteRepository().getUserName(), service.getRemoteRepository( "id-new" ).getUserName() );
104         assertEquals( getRemoteRepository().getPassword(), service.getRemoteRepository( "id-new" ).getPassword() );
105         assertEquals( getRemoteRepository().getTimeout(), service.getRemoteRepository( "id-new" ).getTimeout() );
106         assertEquals( getRemoteRepository().getDescription(),
107                       service.getRemoteRepository( "id-new" ).getDescription() );
108
109         assertEquals( initialSize + 1, service.getRemoteRepositories().size() );
110
111         service.deleteRemoteRepository( "id-new" );
112
113         assertNull( service.getRemoteRepository( "id-new" ) );
114
115         assertEquals( initialSize, service.getRemoteRepositories().size() );
116
117     }
118
119     @Test
120     public void addAndUpdateAndDeleteRemoteRepository()
121         throws Exception
122     {
123         RemoteRepositoriesService service = getRemoteRepositoriesService();
124
125         WebClient.client( service ).header( "Authorization", authorizationHeader );
126
127         int initialSize = service.getRemoteRepositories().size();
128
129         service.addRemoteRepository( getRemoteRepository() );
130
131         assertNotNull( service.getRemoteRepository( "id-new" ) );
132
133         assertEquals( getRemoteRepository().getName(), service.getRemoteRepository( "id-new" ).getName() );
134         assertEquals( getRemoteRepository().getUrl(), service.getRemoteRepository( "id-new" ).getUrl() );
135         assertEquals( getRemoteRepository().getLayout(), service.getRemoteRepository( "id-new" ).getLayout() );
136         assertEquals( getRemoteRepository().getUserName(), service.getRemoteRepository( "id-new" ).getUserName() );
137         assertEquals( getRemoteRepository().getPassword(), service.getRemoteRepository( "id-new" ).getPassword() );
138         assertEquals( getRemoteRepository().getTimeout(), service.getRemoteRepository( "id-new" ).getTimeout() );
139
140         assertEquals( initialSize + 1, service.getRemoteRepositories().size() );
141
142         RemoteRepository repo = getRemoteRepository();
143         repo.setName( "name changed" );
144         repo.setPassword( "new password" );
145         repo.setUserName( "new username" );
146         repo.setUrl( "http://foo.new.org" );
147         repo.setDescription( "foo bar" );
148
149         service.updateRemoteRepository( repo );
150
151         assertEquals( repo.getName(), service.getRemoteRepository( "id-new" ).getName() );
152         assertEquals( repo.getUrl(), service.getRemoteRepository( "id-new" ).getUrl() );
153         assertEquals( repo.getLayout(), service.getRemoteRepository( "id-new" ).getLayout() );
154         assertEquals( repo.getUserName(), service.getRemoteRepository( "id-new" ).getUserName() );
155         assertEquals( repo.getPassword(), service.getRemoteRepository( "id-new" ).getPassword() );
156         assertEquals( repo.getTimeout(), service.getRemoteRepository( "id-new" ).getTimeout() );
157         assertEquals( repo.getDescription(), service.getRemoteRepository( "id-new" ).getDescription() );
158
159         service.deleteRemoteRepository( "id-new" );
160
161         assertNull( service.getRemoteRepository( "id-new" ) );
162
163         assertEquals( initialSize, service.getRemoteRepositories().size() );
164
165     }
166
167     @Test
168     public void checkRemoteConnectivity()
169             throws Exception {
170         try {
171             RemoteRepositoriesService service = getRemoteRepositoriesService();
172
173             WebClient.client(service).header("Authorization", authorizationHeader);
174
175             int initialSize = service.getRemoteRepositories().size();
176
177             service.addRemoteRepository(getRemoteRepository());
178
179             assertTrue(service.checkRemoteConnectivity("id-new"));
180         } finally {
181             removeRemoteRepositories("id-new");
182         }
183
184     }
185
186     /*
187      * Check maven repository
188      */
189     @Test
190     public void checkRemoteConnectivity2()
191             throws Exception {
192         try {
193             RemoteRepositoriesService service = getRemoteRepositoriesService();
194
195             WebClient.client(service).header("Authorization", authorizationHeader);
196
197             int initialSize = service.getRemoteRepositories().size();
198
199             service.addRemoteRepository(getRemoteMavenRepository());
200
201             assertTrue(service.checkRemoteConnectivity("id-maven1"));
202         } finally {
203             removeRemoteRepositories("id-maven1");
204         }
205
206     }
207
208
209     /*
210      *  Check oracle repository that allows not browsing (MRM-1933)
211      */
212     @Test
213     public void checkRemoteConnectivity3()
214             throws Exception {
215         try {
216             RemoteRepositoriesService service = getRemoteRepositoriesService();
217
218             WebClient.client(service).header("Authorization", authorizationHeader);
219             WebClient.client(service).accept("application/json");
220
221             int initialSize = service.getRemoteRepositories().size();
222
223             service.addRemoteRepository(getRemoteOracleRepository());
224
225             assertTrue(service.checkRemoteConnectivity("id-oracle"));
226         } finally {
227             removeRemoteRepositories("id-oracle");
228         }
229
230     }
231
232     RemoteRepository getRemoteRepository()
233     {
234         return new RemoteRepository( Locale.getDefault( ), "id-new", "new one", "http://www.apache.org", "default", "foo", "foopassword", 120,
235                                      "cool repo" );
236     }
237
238
239     RemoteRepository getRemoteMavenRepository()
240     {
241         return new RemoteRepository( Locale.getDefault( ),"id-maven1", "Maven1", "https://repo.maven.apache.org/maven2", "default", "foo", "foopassword", 120,
242                 "cool repo3" );
243     }
244
245
246     RemoteRepository getRemoteOracleRepository()
247     {
248         return new RemoteRepository( Locale.getDefault( ),"id-oracle", "Oracle", "http://download.oracle.com/maven", "default", "foo", "foopassword", 120,
249                 "cool repo4" );
250     }
251
252 }