17
17
18
18
import java .io .IOException ;
19
19
import java .io .InputStream ;
20
- import java .io .PrintWriter ;
21
- import java .io .Writer ;
22
- import java .util .ArrayList ;
23
20
import java .util .Arrays ;
24
- import java .util .Collection ;
25
21
import java .util .HashMap ;
26
22
import java .util .HashSet ;
27
23
import java .util .Map ;
28
24
import java .util .Objects ;
29
25
import java .util .Set ;
30
26
27
+ import org .apache .commons .lang3 .tuple .Pair ;
31
28
import org .apache .sling .api .SlingHttpServletRequest ;
32
29
import org .apache .sling .api .resource .LoginException ;
33
30
import org .apache .sling .api .resource .Resource ;
@@ -92,8 +89,6 @@ class ClientLibrariesImplTest {
92
89
93
90
private final AemContext context = CoreComponentTestContext .newAemContext ();
94
91
95
- private Map <String ,ClientLibrary > allLibraries ; // a map of (path, library) of all the libraries
96
- private Map <String ,ClientLibrary > librariesMap ; // a map of (category, library) of all the libraries
97
92
private Map <String ,String > jsIncludes ; // expected js includes
98
93
private Map <String ,String > cssIncludes ; // expected css includes
99
94
private Map <String ,String > jsIncludesWithAttributes ; // expected js includes when injecting attributes
@@ -156,10 +151,11 @@ void setUp() {
156
151
when (carouselClientLibrary .getCategories ()).thenReturn (carouselCategories );
157
152
when (carouselClientLibrary .getPath ()).thenReturn (CAROUSEL_CLIENTLIB_PATH );
158
153
159
- librariesMap = new HashMap <>();
160
- librariesMap .put (TEASER_CATEGORY , teaserClientLibrary );
161
- librariesMap .put (ACCORDION_CATEGORY , accordionClientLibrary );
162
- librariesMap .put (CAROUSEL_CATEGORY , carouselClientLibrary );
154
+ // a map of (category, library) of all the libraries
155
+ Map <String , ClientLibrary > librariesByCategory = new HashMap <>();
156
+ librariesByCategory .put (TEASER_CATEGORY , teaserClientLibrary );
157
+ librariesByCategory .put (ACCORDION_CATEGORY , accordionClientLibrary );
158
+ librariesByCategory .put (CAROUSEL_CATEGORY , carouselClientLibrary );
163
159
164
160
// Mock HtmlLibrary
165
161
HtmlLibrary teaserJsHtmlLibrary = mock (HtmlLibrary .class );
@@ -186,99 +182,37 @@ void setUp() {
186
182
} catch (IOException e ) {
187
183
fail (String .format ("Unable to get input stream from the library: %s" , e .getMessage ()));
188
184
}
189
-
190
- // Mock htmlLibraryManager.getLibraries()
191
- allLibraries = new HashMap <>();
192
- allLibraries .put (TEASER_CLIENTLIB_PATH , teaserClientLibrary );
193
- allLibraries .put (ACCORDION_CLIENTLIB_PATH , accordionClientLibrary );
194
- allLibraries .put (CAROUSEL_CLIENTLIB_PATH , carouselClientLibrary );
195
- HtmlLibraryManager htmlLibraryManager = context .registerInjectActivateService (mock (MockHtmlLibraryManager .class ));
196
- when (htmlLibraryManager .getLibraries ()).thenReturn (allLibraries );
197
-
198
- // Mock htmlLibraryManager.getLibraries(a, b, c, d)
199
- doAnswer (invocation -> {
200
- Object [] args = invocation .getArguments ();
201
- String [] categories = (String []) args [0 ];
202
- Collection <ClientLibrary > libraries = new ArrayList <>();
203
- for (String category : categories ) {
204
- libraries .add (librariesMap .get (category ));
205
- }
206
- return libraries ;
207
- }).when (htmlLibraryManager ).getLibraries (any (String [].class ), any (LibraryType .class ), anyBoolean (), anyBoolean ());
208
-
209
- // Mock htmlLibraryManager.getLibrary(libraryType, clientlib.getPath())
210
- when (htmlLibraryManager .getLibrary (eq (LibraryType .JS ), eq (TEASER_CLIENTLIB_PATH ))).thenReturn (teaserJsHtmlLibrary );
211
- when (htmlLibraryManager .getLibrary (eq (LibraryType .JS ), eq (ACCORDION_CLIENTLIB_PATH ))).thenReturn (accordionJsHtmlLibrary );
212
- when (htmlLibraryManager .getLibrary (eq (LibraryType .JS ), eq (CAROUSEL_CLIENTLIB_PATH ))).thenReturn (carouselJsHtmlLibrary );
213
- when (htmlLibraryManager .getLibrary (eq (LibraryType .CSS ), eq (TEASER_CLIENTLIB_PATH ))).thenReturn (teaserCssHtmlLibrary );
214
- when (htmlLibraryManager .getLibrary (eq (LibraryType .CSS ), eq (ACCORDION_CLIENTLIB_PATH ))).thenReturn (accordionCssHtmlLibrary );
215
- when (htmlLibraryManager .getLibrary (eq (LibraryType .CSS ), eq (CAROUSEL_CLIENTLIB_PATH ))).thenReturn (carouselCssHtmlLibrary );
216
-
217
- // Mock htmlLibraryManager.writeJsInclude
218
- try {
219
- doAnswer (invocation -> {
220
- Object [] args = invocation .getArguments ();
221
- StringBuilder scriptIncludes = new StringBuilder ();
222
- for (int i = 2 ; i < args .length ; i ++) {
223
- String category = (String ) args [i ];
224
- String script = jsIncludes .get (category );
225
- scriptIncludes .append (script );
226
- }
227
- ((PrintWriter )args [1 ]).write (scriptIncludes .toString ());
228
- return null ;
229
- }).when (htmlLibraryManager ).writeJsInclude (any (SlingHttpServletRequest .class ), any (Writer .class ), any (String .class ));
230
- } catch (IOException e ) {
231
- fail (String .format ("Unable to write JS include: %s" , e .getMessage ()));
232
- }
233
-
234
- // Mock htmlLibraryManager.writeCssInclude
235
- try {
236
- doAnswer (invocation -> {
237
- Object [] args = invocation .getArguments ();
238
- StringBuilder linkIncludes = new StringBuilder ();
239
- for (int i = 2 ; i < args .length ; i ++) {
240
- String category = (String ) args [i ];
241
- String link = cssIncludes .get (category );
242
- linkIncludes .append (link );
243
- }
244
- ((PrintWriter )args [1 ]).write (linkIncludes .toString ());
245
- return null ;
246
- }).when (htmlLibraryManager ).writeCssInclude (any (SlingHttpServletRequest .class ), any (Writer .class ), any (String .class ));
247
- } catch (IOException e ) {
248
- fail (String .format ("Unable to write CSS include: %s" , e .getMessage ()));
249
- }
250
-
251
- // Mock htmlLibraryManager.writeIncludes
252
- try {
253
- doAnswer (invocation -> {
254
- Object [] args = invocation .getArguments ();
255
- StringBuilder includes = new StringBuilder ();
256
- for (int i = 2 ; i < args .length ; i ++) {
257
- String category = (String ) args [i ];
258
- String script = jsIncludes .get (category );
259
- includes .append (script );
260
- }
261
- for (int i = 2 ; i < args .length ; i ++) {
262
- String category = (String ) args [i ];
263
- String link = cssIncludes .get (category );
264
- includes .append (link );
265
- }
266
- ((PrintWriter )args [1 ]).write (includes .toString ());
267
- return null ;
268
- }).when (htmlLibraryManager ).writeIncludes (any (SlingHttpServletRequest .class ), any (Writer .class ), any (String .class ));
269
- } catch (IOException e ) {
270
- fail (String .format ("Unable to write include: %s" , e .getMessage ()));
271
- }
272
-
185
+ Map <Pair <String , LibraryType >, HtmlLibrary > htmlLibrariesByPathAndType = new HashMap <>();
186
+ htmlLibrariesByPathAndType .put (Pair .of (TEASER_CLIENTLIB_PATH , LibraryType .JS ), teaserJsHtmlLibrary );
187
+ htmlLibrariesByPathAndType .put (Pair .of (ACCORDION_CLIENTLIB_PATH , LibraryType .JS ), accordionJsHtmlLibrary );
188
+ htmlLibrariesByPathAndType .put (Pair .of (CAROUSEL_CLIENTLIB_PATH , LibraryType .JS ), carouselJsHtmlLibrary );
189
+ htmlLibrariesByPathAndType .put (Pair .of (TEASER_CLIENTLIB_PATH , LibraryType .CSS ), teaserCssHtmlLibrary );
190
+ htmlLibrariesByPathAndType .put (Pair .of (ACCORDION_CLIENTLIB_PATH , LibraryType .CSS ), accordionCssHtmlLibrary );
191
+ htmlLibrariesByPathAndType .put (Pair .of (CAROUSEL_CLIENTLIB_PATH , LibraryType .CSS ), carouselCssHtmlLibrary );
192
+
193
+ // a map of (path, library) of all the libraries
194
+ Map <String , ClientLibrary > librariesByPath = new HashMap <>();
195
+ librariesByPath .put (TEASER_CLIENTLIB_PATH , teaserClientLibrary );
196
+ librariesByPath .put (ACCORDION_CLIENTLIB_PATH , accordionClientLibrary );
197
+ librariesByPath .put (CAROUSEL_CLIENTLIB_PATH , carouselClientLibrary );
198
+
199
+ HtmlLibraryManager htmlLibraryManager = new MockHtmlLibraryManager (mock (ClientLibrary .class ),
200
+ htmlLibrariesByPathAndType ,
201
+ librariesByPath ,
202
+ librariesByCategory ,
203
+ jsIncludes ,
204
+ cssIncludes );
205
+ context .registerInjectActivateService (htmlLibraryManager );
273
206
}
274
207
275
208
@ Test
276
209
void testGetCategories () {
277
210
PageManager pageManager = context .pageManager ();
278
211
Page page = pageManager .getPage (ROOT_PAGE );
279
212
Map <String , Object > attributes = new HashMap <>();
280
- attributes .put (ClientLibraries .OPTION_RESOURCE_TYPES , Utils .getPageResourceTypes (page , context .request (), mock (ModelFactory .class )));
281
213
ClientLibrariesImpl clientlibs = Objects .requireNonNull ((ClientLibrariesImpl ) getClientLibrariesUnderTest (ROOT_PAGE , attributes ));
214
+ attributes .put (ClientLibraries .OPTION_RESOURCE_TYPES , Utils .getPageResourceTypes (page , context .request (), mock (ModelFactory .class )));
215
+ clientlibs = Objects .requireNonNull ((ClientLibrariesImpl ) getClientLibrariesUnderTest (ROOT_PAGE , attributes ));
282
216
283
217
Set <String > categories = new HashSet <>();
284
218
categories .add (TEASER_CATEGORY );
@@ -520,7 +454,8 @@ private ClientLibraries getClientLibrariesUnderTest(String path, Map<String,Obje
520
454
}
521
455
}
522
456
context .request ().setResource (resource );
523
- return context .request ().adaptTo (ClientLibraries .class );
457
+ ClientLibraries clientLibraries = context .request ().adaptTo (ClientLibraries .class );
458
+ return clientLibraries ;
524
459
}
525
460
return null ;
526
461
}
0 commit comments