-
Notifications
You must be signed in to change notification settings - Fork 65
/
ConfigurationTest.java
255 lines (208 loc) · 9.02 KB
/
ConfigurationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*******************************************************************************
* Copyright (c) 2007 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.openshift.express.internal.client.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
import com.openshift.express.client.OpenShiftException;
import com.openshift.express.client.OpenShiftService;
import com.openshift.express.client.configuration.DefaultConfiguration;
import com.openshift.express.client.configuration.IOpenShiftConfiguration;
import com.openshift.express.client.configuration.OpenShiftConfiguration;
import com.openshift.express.client.configuration.SystemConfiguration;
import com.openshift.express.client.configuration.SystemProperties;
import com.openshift.express.client.configuration.UserConfiguration;
import com.openshift.express.internal.client.test.fakes.SystemConfigurationFake;
import com.openshift.express.internal.client.test.fakes.TestUser;
import com.openshift.express.internal.client.test.fakes.UserConfigurationFake;
import com.openshift.express.internal.client.utils.StreamUtils;
/**
* @author André Dietisheim
*/
public class ConfigurationTest {
private static final String USERNAME_REGEX = "[^=]+=(.+)";
private static final String USERNAME = "dummyUser";
private static final String USERNAME2 = "André";
private static final String USERNAME3 = "Dietisheim";
private static final String ANOTHER_USERNAME = "anotherUser";
protected static final String LIBRA_SERVER = "openshift.redhat.com";
@Test
public void versionTest() throws OpenShiftException, IOException {
String version = OpenShiftService.getVersion();
assertNotNull(version);
assertFalse(version.contains("pom"));
}
@Test
public void canReadUsername() throws OpenShiftException, IOException {
UserConfigurationFake userConfiguration = new UserConfigurationFake() {
protected void initFile(Writer writer) throws IOException {
writer.append(KEY_RHLOGIN).append('=').append(USERNAME).append('\n');
}
};
assertEquals(USERNAME, userConfiguration.getRhlogin());
}
@Test
public void canStoreUsername() throws OpenShiftException, IOException {
UserConfigurationFake userConfiguration = new UserConfigurationFake() {
protected void initFile(Writer writer) throws IOException {
writer.append(KEY_RHLOGIN).append('=').append(USERNAME).append('\n');
}
};
userConfiguration.setRhlogin(ANOTHER_USERNAME);
userConfiguration.save();
final File userConfigurationFile = userConfiguration.getFile();
assertNotNull(userConfigurationFile);
String fileContent = StreamUtils.readToString(new FileReader(userConfigurationFile));
Pattern pattern = Pattern.compile(USERNAME_REGEX);
Matcher matcher = pattern.matcher(fileContent);
assertTrue(matcher.matches());
assertEquals(1, matcher.groupCount());
assertEquals(ANOTHER_USERNAME, matcher.group(1));
}
@Test
public void canStoreAndReadUsername() throws OpenShiftException, IOException {
UserConfigurationFake userConfiguration = new UserConfigurationFake() {
protected void initFile(Writer writer) throws IOException {
writer.append(KEY_RHLOGIN).append('=').append(USERNAME).append('\n');
}
};
assertEquals(USERNAME, userConfiguration.getRhlogin());
userConfiguration.setRhlogin(ANOTHER_USERNAME);
userConfiguration.save();
final File userConfigurationFile = userConfiguration.getFile();
assertNotNull(userConfigurationFile);
UserConfigurationFake userConfiguration2 = new UserConfigurationFake() {
protected File createFile() {
return userConfigurationFile;
}
protected void initFile(File file) {
}
};
assertEquals(ANOTHER_USERNAME, userConfiguration2.getRhlogin());
}
@Test
public void canReadUsernameIfItsInSystemConfigurationOnly() throws OpenShiftException, IOException {
SystemConfiguration systemConfiguration = new SystemConfigurationFake(new DefaultConfiguration()) {
protected void init(Properties properties) {
properties.put(KEY_RHLOGIN, USERNAME);
}
};
UserConfigurationFake userConfiguration = new UserConfigurationFake(systemConfiguration);
assertEquals(USERNAME, userConfiguration.getRhlogin());
}
@Test
public void usernameInUserconfigOverridesUsernameInSystemconfig() throws OpenShiftException, IOException {
SystemConfiguration systemConfiguration = new SystemConfigurationFake(new DefaultConfiguration()) {
protected void init(Properties properties) {
properties.put(KEY_RHLOGIN, USERNAME);
}
};
UserConfigurationFake userConfiguration = new UserConfigurationFake(systemConfiguration) {
protected void initFile(Writer writer) throws IOException {
writer.append(KEY_RHLOGIN).append('=').append(USERNAME2).append('\n');
}
};
assertEquals(USERNAME2, userConfiguration.getRhlogin());
}
@Test
public void quotedLibraServerIsReturnedWithoutQuotes() throws OpenShiftException, IOException {
SystemConfiguration systemConfiguration = new SystemConfigurationFake(new DefaultConfiguration()) {
protected void init(Properties properties) {
properties.put(KEY_RHLOGIN, USERNAME);
}
};
UserConfigurationFake userConfiguration = new UserConfigurationFake(systemConfiguration);
assertEquals(UserConfiguration.SCHEME_HTTPS + LIBRA_SERVER, userConfiguration.getLibraServer());
}
@Test
public void nonQuotedLibraServerIsReturnedAsIs() throws OpenShiftException, IOException {
SystemConfiguration systemConfiguration = new SystemConfigurationFake(new DefaultConfiguration()) {
protected void init(Properties properties) {
properties.put(KEY_RHLOGIN, USERNAME);
}
};
UserConfigurationFake userConfiguration = new UserConfigurationFake(systemConfiguration);
assertEquals(UserConfiguration.SCHEME_HTTPS + LIBRA_SERVER, userConfiguration.getLibraServer());
}
@Test
public void nullLibraServerIsReturnedAsNull() throws OpenShiftException, IOException {
SystemConfiguration systemConfiguration = new SystemConfigurationFake();
assertNull(systemConfiguration.getLibraServer());
}
@Test
public void systemPropsOverrideSystemconfig() throws OpenShiftException, IOException {
SystemConfiguration systemConfiguration = new SystemConfigurationFake(new DefaultConfiguration()) {
@Override
protected void init(Properties properties) {
properties.put(KEY_RHLOGIN, USERNAME);
}
};
UserConfigurationFake userConfiguration = new UserConfigurationFake(systemConfiguration) {
@Override
protected void initFile(Writer writer) throws IOException {
writer.append(KEY_RHLOGIN).append('=').append(USERNAME2).append('\n');
}
};
IOpenShiftConfiguration configuration = new SystemProperties(userConfiguration) {
@Override
protected Properties getProperties(File file, Properties defaultProperties) {
// save orig rhlogin
String originalRhLogin = System.getProperty(KEY_RHLOGIN);
System.setProperty(KEY_RHLOGIN, USERNAME3);
Properties properties = super.getProperties(file, defaultProperties);
// resore orig rhlogin
if (originalRhLogin != null) {
System.setProperty(KEY_RHLOGIN, originalRhLogin);
} else {
System.clearProperty(KEY_RHLOGIN);
}
return properties;
}
};
configuration.setRhlogin(USERNAME3);
assertEquals(USERNAME3, configuration.getRhlogin());
}
@Test
public void systemPropertiesDefaultToUserConfig() throws OpenShiftException, IOException {
SystemConfiguration systemConfiguration = new SystemConfigurationFake(new DefaultConfiguration()) {
@Override
protected void init(Properties properties) {
properties.put(KEY_RHLOGIN, USERNAME);
}
};
UserConfigurationFake userConfiguration = new UserConfigurationFake(systemConfiguration) {
@Override
protected void initFile(Writer writer) throws IOException {
writer.append(KEY_RHLOGIN).append('=').append(USERNAME2).append('\n');
}
};
IOpenShiftConfiguration configuration = new SystemProperties(userConfiguration);
assertEquals(USERNAME2, configuration.getRhlogin());
}
@Test
public void fallsBackToDefaultUrl() throws OpenShiftException, IOException {
IOpenShiftConfiguration configuration = new SystemProperties(
new UserConfigurationFake(new SystemConfigurationFake(new DefaultConfiguration())));
assertNotNull(configuration.getLibraServer());
assertTrue(configuration.getLibraServer().contains(DefaultConfiguration.LIBRA_SERVER));
assertNull(configuration.getRhlogin());
}
}