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