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