Skip to content

Commit

Permalink
Merge pull request #2725 from wiremock/join_handlerbars_helper
Browse files Browse the repository at this point in the history
Join - Handlebar helper
  • Loading branch information
dieppa committed May 17, 2024
2 parents a127ecc + 01396e3 commit 3f02154
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2019-2024 Thomas Akehurst
*
* 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.
*/
package com.github.tomakehurst.wiremock.extension.responsetemplating.helpers;

import com.github.jknack.handlebars.Options;
import com.github.jknack.handlebars.TagType;
import java.io.IOException;
import java.util.List;

public class JoinHelper extends HandlebarsHelper<Object> {

@Override
public Object apply(Object context, Options options) throws IOException {

if (!(context instanceof String)) {
return handleError("Separator parameter must be a String");
}

String separator = (String) context;

Object param = options.param(0, null);
if (param == null || !(Iterable.class.isAssignableFrom(param.getClass()))) {
return handleError("The parameter must be list");
}
List<Object> items = (List<Object>) param;
if (items.isEmpty()) {
return "";
}
String result;
if (options.tagType == TagType.SECTION) {
result = processSection(options, separator, items);
} else {
result = processWithoutSection(separator, items);
}

return result;
}

private static String processWithoutSection(String separator, List<Object> items) {
StringBuilder sb = new StringBuilder();
boolean initialised = false;

for (Object item : items) {
if (initialised) {
sb.append(separator);
}
sb.append(item.toString());
initialised = true;
}
return sb.toString();
}

private static String processSection(Options options, String separator, List<Object> list)
throws IOException {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
CharSequence itemRendered = options.fn(list.get(i));
sb.append(itemRendered);
if (i < list.size() - 1) {
sb.append(separator);
}
}

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ public Object apply(Object context, Options options) throws IOException {
val {
private final ValHelper helper = new ValHelper();

@Override
public Object apply(Object context, Options options) throws IOException {
return helper.apply(context, options);
}
},

join {
private final JoinHelper helper = new JoinHelper();

@Override
public Object apply(Object context, Options options) throws IOException {
return helper.apply(context, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,84 @@ void valHelperDefaultsNullValue() {
assertThat(transform("{{val 'exists' or='123'}}"), is("exists"));
}

@Test
public void joinWithObjectBody() {
String result =
transform(
"{{#parseJson 'myThings'}}\n"
+ "[\n"
+ " { \"id\": 1, \"name\": \"One\" },\n"
+ " { \"id\": 2, \"name\": \"Two\" },\n"
+ " { \"id\": 3, \"name\": \"Three\" }\n"
+ "]\n"
+ "{{/parseJson}}"
+ "[{{#join ',' myThings as |item|}}"
+ "{\n"
+ "\"name{{item.id}}\": \"{{item.name}}\"\n"
+ "}\n"
+ "{{/join}}]");

assertThat(
result,
equalToCompressingWhiteSpace(
"[{\n\"name1\": \"One\"\n}\n,{\n\"name2\": \"Two\"\n}\n,{\n\"name3\": \"Three\"\n}\n]"));
}

@Test
public void joinWithArrayOfStrings() {
String result = transform("{{join ',' (array 'One\n' 'Two' 'Three')}}");

assertThat(result, equalToCompressingWhiteSpace("One\n,Two,Three"));
}

@Test
public void joinWithEmptyArray() {
String result = transform("{{join ',' (array )}}");

assertThat(result, equalToCompressingWhiteSpace(""));
}

@Test
public void joinWithNoSeparatorShouldReturnError() {
String result = transform("{{join (array 'One' 'Two' 'Three')}}");

assertThat(
result, equalToCompressingWhiteSpace("[ERROR: Separator parameter must be a String]\n"));
}

@Test
public void joinWithNoParameterShouldReturnError() {
String result = transform("{{join ','}}");

assertThat(result, equalToCompressingWhiteSpace("[ERROR: The parameter must be list]\n"));
}

@Test
public void joinWithStringAsParameterShouldReturnError() {
String result = transform("{{join ',' \"blablabla\"}}");

assertThat(result, equalToCompressingWhiteSpace("[ERROR: The parameter must be list]\n"));
}

@Test
public void joinWithDifferentSeparators() {
String result1 = transform("{{join (pickRandom ':') (array 'One' 'Two' 'Three')}}");
assertThat(result1, equalToCompressingWhiteSpace("One:Two:Three"));

String result2 = transform("{{join '*' (array 1 2 3)}}");
assertThat(result2, equalToCompressingWhiteSpace("1*2*3"));

String result3 = transform("{{join ' ' (array 'WireMock' 'Rocks')}}");
assertThat(result3, equalToCompressingWhiteSpace("WireMock Rocks"));

String result4 =
transform("{{join '' (array 'W' 'i' 'r' 'e' 'M' 'o' 'c' 'k' ' ' 'R' 'o' 'c' 'k' 's')}}");
assertThat(result4, equalToCompressingWhiteSpace("WireMock Rocks"));

String result5 = transform("{{join \" - * - \" (array 'One' 'Two' 'Three')}}");
assertThat(result5, equalToCompressingWhiteSpace("One - * - Two - * - Three"));
}

private Integer transformToInt(String responseBodyTemplate) {
return Integer.parseInt(transform(responseBodyTemplate));
}
Expand Down

0 comments on commit 3f02154

Please sign in to comment.