This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
/
EditorParameterPersistenceTest.java
262 lines (229 loc) · 11.8 KB
/
EditorParameterPersistenceTest.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
256
257
258
259
260
261
262
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2014 wcm.io
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package io.wcm.config.editor.impl;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HttpConstants;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import com.day.cq.wcm.api.Page;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.wcm.config.api.Configuration;
import io.wcm.config.api.Parameter;
import io.wcm.config.api.ParameterBuilder;
import io.wcm.config.core.management.ConfigurationFinder;
import io.wcm.config.core.management.ParameterPersistence;
import io.wcm.config.core.management.ParameterPersistenceData;
import io.wcm.config.core.management.ParameterResolver;
import io.wcm.config.editor.WidgetTypes;
import io.wcm.sling.commons.resource.ImmutableValueMap;
import io.wcm.testing.mock.aem.junit.AemContext;
import io.wcm.wcm.commons.util.RunMode;
/**
* Tests for the {@link EditorParameterPersistence}
*/
@RunWith(MockitoJUnitRunner.class)
public class EditorParameterPersistenceTest {
@Mock
private ParameterResolver parameterResolver;
private static final String APP_ID = "/app/test";
private static final Parameter<String> NON_EDITABLE_PARAMETER = ParameterBuilder.create("some-param", String.class, APP_ID)
.defaultValue("defaultValue").build();
private static final Parameter<Map> PARAMETER_MAP = ParameterBuilder.create("map-param", Map.class, APP_ID)
.properties(WidgetTypes.TEXTAREA.getDefaultWidgetConfiguration()).build();
private static final Parameter<String[]> PARAMETER_MULTIVALUE = ParameterBuilder.create("multivalue-param", String[].class, APP_ID)
.properties(WidgetTypes.TEXTAREA.getDefaultWidgetConfiguration()).build();
private static final Parameter<String> PARAMETER_STRING = ParameterBuilder.create("string-param", String.class, APP_ID)
.properties(WidgetTypes.TEXTAREA.getDefaultWidgetConfiguration()).build();
private static final Parameter<Boolean> PARAMETER_BOOLEAN = ParameterBuilder.create("boolean-param", Boolean.class, APP_ID)
.properties(WidgetTypes.TEXTAREA.getDefaultWidgetConfiguration()).build();
private static final Parameter<Integer> PARAMETER_INTEGER = ParameterBuilder.create("integer-param", Integer.class, APP_ID)
.properties(WidgetTypes.TEXTAREA.getDefaultWidgetConfiguration()).build();
private static final Parameter<Long> PARAMETER_LONG = ParameterBuilder.create("long-param", Long.class, APP_ID)
.properties(WidgetTypes.TEXTAREA.getDefaultWidgetConfiguration()).build();
private static final Parameter<Double> PARAMETER_DOUBLE = ParameterBuilder.create("double-param", Double.class, APP_ID)
.properties(WidgetTypes.TEXTAREA.getDefaultWidgetConfiguration()).build();
private static final Set<Parameter<?>> PARAMETERS = ImmutableSet.<Parameter<?>>of(PARAMETER_BOOLEAN, PARAMETER_DOUBLE, PARAMETER_INTEGER, PARAMETER_LONG,
PARAMETER_MAP, PARAMETER_MULTIVALUE, PARAMETER_STRING, NON_EDITABLE_PARAMETER);
private static final Map<String, Object> REQUEST_PARAMETERS = ImmutableValueMap.builder()
.put("map-param" + EditorParameterPersistence.MAP_KEY_SUFFIX, new String[] {
"key1", "key2"
})
.put("map-param", new String[] {
"value1", "value2"
})
.put("multivalue-param", new String[] {
"value1", "value2"
})
.put("string-param", "value")
.put("integer-param", "1")
.put("long-param", "5")
.put("double-param", "3.454")
.put("boolean-param", "true")
.put(ParameterPersistence.PN_LOCKED_PARAMETER_NAMES, new String[]{"string-param", "boolean-param"})
.build();
@Mock
private ConfigurationFinder configurationFinder;
@Mock
private Configuration configuration;
@Mock
private ResourceResolver resourceResolver;
@Mock
private ParameterPersistence persistence;
@Mock
private SlingHttpServletRequest request;
@Mock
private Resource resource;
@Rule
public AemContext context = new AemContext();
@Before
public void setUp() {
context.runMode(RunMode.AUTHOR);
context.request().setParameterMap(REQUEST_PARAMETERS);
Page page = context.create().page("/content/test/tools/config");
context.currentPage(page);
context.request().setMethod(HttpConstants.METHOD_POST);
context.registerService(ConfigurationFinder.class, configurationFinder);
context.registerService(ParameterResolver.class, parameterResolver);
context.registerService(ParameterPersistence.class, persistence);
when(parameterResolver.getAllParameters()).thenReturn(PARAMETERS);
when(configurationFinder.find(ArgumentMatchers.any(Resource.class))).thenReturn(configuration);
when(configuration.getConfigurationId()).thenReturn("/content/test");
}
@Test
public void testResponseNoConfigurationFound() throws Exception {
when(configurationFinder.find(ArgumentMatchers.any(Resource.class))).thenReturn(null);
EditorParameterPersistence underTest = new EditorParameterPersistence();
context.registerInjectActivateService(underTest);
underTest.service(context.request(), context.response());
assertEquals(HttpServletResponse.SC_BAD_REQUEST, context.response().getStatus());
}
@Test
public void testResponseConfigurationFound() throws Exception {
EditorParameterPersistence underTest = new EditorParameterPersistence();
context.registerInjectActivateService(underTest);
underTest.service(context.request(), context.response());
assertEquals(HttpServletResponse.SC_OK, context.response().getStatus());
}
@SuppressWarnings("unchecked")
@Test
public void testMapValue() throws Exception {
EditorParameterPersistence underTest = new EditorParameterPersistence();
context.registerInjectActivateService(underTest);
ArgumentCaptor<ParameterPersistenceData> argument = ArgumentCaptor.forClass(ParameterPersistenceData.class);
underTest.service(context.request(), context.response());
verify(persistence).storeData(any(ResourceResolver.class), eq("/content/test"), argument.capture(), eq(false));
Map<String, Object> mapParam = (Map<String, Object>)argument.getValue().getValues().get("map-param");
assertEquals("value1", mapParam.get("key1"));
assertEquals("value2", mapParam.get("key2"));
}
@Test
public void testMultiValue() throws Exception {
EditorParameterPersistence underTest = new EditorParameterPersistence();
context.registerInjectActivateService(underTest);
ArgumentCaptor<ParameterPersistenceData> argument = ArgumentCaptor.forClass(ParameterPersistenceData.class);
underTest.service(context.request(), context.response());
verify(persistence).storeData(any(ResourceResolver.class), eq("/content/test"), argument.capture(), eq(false));
String[] multiParam = (String[])argument.getValue().getValues().get("multivalue-param");
assertEquals("value1", multiParam[0]);
assertEquals("value2", multiParam[1]);
}
@Test
public void testBooleanValue() throws Exception {
EditorParameterPersistence underTest = new EditorParameterPersistence();
context.registerInjectActivateService(underTest);
ArgumentCaptor<ParameterPersistenceData> argument = ArgumentCaptor.forClass(ParameterPersistenceData.class);
underTest.service(context.request(), context.response());
verify(persistence).storeData(any(ResourceResolver.class), eq("/content/test"), argument.capture(), eq(false));
Boolean value = (Boolean)argument.getValue().getValues().get("boolean-param");
assertEquals(true, value);
}
@Test
public void testIntegerValue() throws Exception {
EditorParameterPersistence underTest = new EditorParameterPersistence();
context.registerInjectActivateService(underTest);
ArgumentCaptor<ParameterPersistenceData> argument = ArgumentCaptor.forClass(ParameterPersistenceData.class);
underTest.service(context.request(), context.response());
verify(persistence).storeData(any(ResourceResolver.class), eq("/content/test"), argument.capture(), eq(false));
Integer value = (Integer)argument.getValue().getValues().get("integer-param");
assertEquals(Integer.valueOf(1), value);
}
@Test
public void testLongValue() throws Exception {
EditorParameterPersistence underTest = new EditorParameterPersistence();
context.registerInjectActivateService(underTest);
ArgumentCaptor<ParameterPersistenceData> argument = ArgumentCaptor.forClass(ParameterPersistenceData.class);
underTest.service(context.request(), context.response());
verify(persistence).storeData(any(ResourceResolver.class), eq("/content/test"), argument.capture(), eq(false));
Long value = (Long)argument.getValue().getValues().get("long-param");
assertEquals(Long.valueOf(5L), value);
}
@Test
public void testDoubleValue() throws Exception {
EditorParameterPersistence underTest = new EditorParameterPersistence();
context.registerInjectActivateService(underTest);
ArgumentCaptor<ParameterPersistenceData> argument = ArgumentCaptor.forClass(ParameterPersistenceData.class);
underTest.service(context.request(), context.response());
verify(persistence).storeData(any(ResourceResolver.class), eq("/content/test"), argument.capture(), eq(false));
Double value = (Double)argument.getValue().getValues().get("double-param");
assertEquals(Double.valueOf(3.454), value);
}
@Test
public void testLockedParameterValue() throws Exception {
EditorParameterPersistence underTest = new EditorParameterPersistence();
context.registerInjectActivateService(underTest);
ArgumentCaptor<ParameterPersistenceData> argument = ArgumentCaptor.forClass(ParameterPersistenceData.class);
underTest.service(context.request(), context.response());
verify(persistence).storeData(any(ResourceResolver.class), eq("/content/test"), argument.capture(), eq(false));
Set<String> value = argument.getValue().getLockedParameterNames();
assertEquals(2, value.size());
Iterator<String> iterator = value.iterator();
assertEquals("boolean-param", iterator.next());
assertEquals("string-param", iterator.next());
}
@Test
public void testLockedParameterValueEmpty() throws Exception {
context.request().setParameterMap(ImmutableMap.<String, Object>of());
EditorParameterPersistence underTest = new EditorParameterPersistence();
context.registerInjectActivateService(underTest);
ArgumentCaptor<ParameterPersistenceData> argument = ArgumentCaptor.forClass(ParameterPersistenceData.class);
underTest.service(context.request(), context.response());
verify(persistence).storeData(any(ResourceResolver.class), eq("/content/test"), argument.capture(), eq(false));
Set<String> value = argument.getValue().getLockedParameterNames();
assertEquals(0, value.size());
}
}