]> source.dussan.org Git - archiva.git/blob
041f94922fbda88bbe7f0115ce57e5f87e085e30
[archiva.git] /
1 package org.apache.archiva.web.action.admin.networkproxies;
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 com.opensymphony.xwork2.validator.ActionValidatorManager;
23 import junit.framework.TestCase;
24 import org.apache.archiva.admin.model.beans.NetworkProxy;
25 import org.apache.archiva.web.validator.utils.ValidatorUtil;
26 import org.apache.archiva.web.action.admin.repositories.DefaultActionValidatorManagerFactory;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import org.apache.archiva.test.ArchivaBlockJUnit4ClassRunner;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
37 public class ConfigureNetworkProxyActionTest extends TestCase
38 {
39     private static final String EMPTY_STRING = "";
40
41     private static final String VALIDATION_CONTEXT = "saveNetworkProxy";
42
43     // valid inputs
44     private static final String PROXY_ID_VALID_INPUT = "abcXYZ0129._-";
45
46     private static final String PROXY_PROTOCOL_VALID_INPUT = "-abcXYZ0129./:\\";
47
48     private static final String PROXY_HOST_VALID_INPUT = "abcXYZ0129._/\\~:?!&=-";
49
50     private static final int PROXY_PORT_VALID_INPUT = 8080;
51
52     private static final String PROXY_USERNAME_VALID_INPUT = "abcXYZ0129.@/_-\\";
53
54     // invalid inputs
55     private static final String PROXY_ID_INVALID_INPUT = "<> \\/~+[ ]'\"";
56
57     private static final String PROXY_PROTOCOL_INVALID_INPUT = "<> ~+[ ]'\"";
58
59     private static final String PROXY_HOST_INVALID_INPUT = "<> ~+[ ]'\"";
60
61     private static final int PROXY_PORT_INVALID_INPUT = 0;
62
63     private static final String PROXY_USERNAME_INVALID_INPUT = "<> ~+[ ]'\"";
64
65     // testing requisite
66     private ConfigureNetworkProxyAction configureNetworkProxyAction;
67
68     private ActionValidatorManager actionValidatorManager;
69     
70     @Override
71     @Before
72     public void setUp()
73         throws Exception
74     {
75         configureNetworkProxyAction = new ConfigureNetworkProxyAction();
76
77         DefaultActionValidatorManagerFactory factory = new DefaultActionValidatorManagerFactory();
78
79         actionValidatorManager = factory.createDefaultActionValidatorManager();
80     }
81
82     @Test
83     public void testStruts2ValidationFrameworkWithNullInputs() throws Exception
84     {
85         // prep
86         NetworkProxy networkProxy = createNetworkProxy(null, null, null, null);
87         configureNetworkProxyAction.setProxy(networkProxy);
88
89         // test
90         actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
91
92         // verify
93         assertTrue(configureNetworkProxyAction.hasFieldErrors());
94
95         Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
96
97         // make an expected field error object
98         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
99
100         // populate
101         List<String> expectedErrorMessages = new ArrayList<String>();
102         expectedErrorMessages.add("You must enter an identifier.");
103         expectedFieldErrors.put("proxy.id", expectedErrorMessages);
104
105         expectedErrorMessages = new ArrayList<String>();
106         expectedErrorMessages.add("You must enter a protocol.");
107         expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
108
109         expectedErrorMessages = new ArrayList<String>();
110         expectedErrorMessages.add("You must enter a host.");
111         expectedFieldErrors.put("proxy.host", expectedErrorMessages);
112
113         ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
114     }
115
116     @Test
117     public void testStruts2ValidationFrameworkWithBlankInputs() throws Exception
118     {
119         // prep
120         NetworkProxy networkProxy = createNetworkProxy(EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING);
121         configureNetworkProxyAction.setProxy(networkProxy);
122
123         // test
124         actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
125
126         // verify
127         assertTrue(configureNetworkProxyAction.hasFieldErrors());
128
129         Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
130
131         // make an expected field error object
132         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
133
134         // populate
135         List<String> expectedErrorMessages = new ArrayList<String>();
136         expectedErrorMessages.add("You must enter an identifier.");
137         expectedFieldErrors.put("proxy.id", expectedErrorMessages);
138
139         expectedErrorMessages = new ArrayList<String>();
140         expectedErrorMessages.add("You must enter a protocol.");
141         expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
142
143         expectedErrorMessages = new ArrayList<String>();
144         expectedErrorMessages.add("You must enter a host.");
145         expectedFieldErrors.put("proxy.host", expectedErrorMessages);
146
147         ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
148     }
149
150     @Test
151     public void testStruts2ValidationFrameworkWithInvalidInputs() throws Exception
152     {
153         // prep
154         NetworkProxy networkProxy = createNetworkProxy( PROXY_ID_INVALID_INPUT, PROXY_HOST_INVALID_INPUT,
155                                                         PROXY_PORT_INVALID_INPUT, PROXY_PROTOCOL_INVALID_INPUT,
156                                                         PROXY_USERNAME_INVALID_INPUT );
157         configureNetworkProxyAction.setProxy(networkProxy);
158
159         // test
160         actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
161
162         // verify
163         assertTrue(configureNetworkProxyAction.hasFieldErrors());
164
165         Map<String, List<String>> fieldErrors = configureNetworkProxyAction.getFieldErrors();
166
167         // make an expected field error object
168         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
169
170         // populate
171         List<String> expectedErrorMessages = new ArrayList<String>();
172         expectedErrorMessages.add("Proxy id must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-).");
173         expectedFieldErrors.put("proxy.id", expectedErrorMessages);
174
175         expectedErrorMessages = new ArrayList<String>();
176         expectedErrorMessages.add("Protocol must only contain alphanumeric characters, forward-slashes(/), back-slashes(\\), dots(.), colons(:), and dashes(-).");
177         expectedFieldErrors.put("proxy.protocol", expectedErrorMessages);
178
179         expectedErrorMessages = new ArrayList<String>();
180         expectedErrorMessages.add("Host must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-).");
181         expectedFieldErrors.put("proxy.host", expectedErrorMessages);
182
183         expectedErrorMessages = new ArrayList<String>();
184         expectedErrorMessages.add("Port needs to be larger than 1");
185         expectedFieldErrors.put("proxy.port", expectedErrorMessages);
186
187         expectedErrorMessages = new ArrayList<String>();
188         expectedErrorMessages.add("Username must only contain alphanumeric characters, at's(@), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), and dashes(-).");
189         expectedFieldErrors.put("proxy.username", expectedErrorMessages);
190
191         ValidatorUtil.assertFieldErrors(expectedFieldErrors, fieldErrors);
192     }
193
194     @Test
195     public void testStruts2ValidationFrameworkWithValidInputs() throws Exception
196     {
197         // prep
198         NetworkProxy networkProxy = createNetworkProxy(PROXY_ID_VALID_INPUT, PROXY_HOST_VALID_INPUT, PROXY_PORT_VALID_INPUT, PROXY_PROTOCOL_VALID_INPUT, PROXY_USERNAME_VALID_INPUT);
199         configureNetworkProxyAction.setProxy(networkProxy);
200
201         // test
202         actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
203
204         // verify
205         assertFalse(configureNetworkProxyAction.hasFieldErrors());
206     }
207
208     private NetworkProxy createNetworkProxy(String id, String host, int port, String protocol, String username)
209     {
210         NetworkProxy networkProxy = new NetworkProxy();
211         networkProxy.setId( id );
212         networkProxy.setHost( host );
213         networkProxy.setPort( port );
214         networkProxy.setProtocol( protocol );
215         networkProxy.setUsername( username );
216         return networkProxy;
217     }
218
219     // over-loaded
220     // for simulating empty/null form purposes; excluding primitive data-typed values
221     private NetworkProxy createNetworkProxy(String id, String host, String protocol, String username)
222     {
223         NetworkProxy networkProxy = new NetworkProxy();
224         networkProxy.setId( id );
225         networkProxy.setHost( host );
226         networkProxy.setProtocol( protocol );
227         networkProxy.setUsername( username );
228         return networkProxy;
229     }
230 }