Skip to content

Commit

Permalink
Merge pull request #2727 from wiremock/additional-tests
Browse files Browse the repository at this point in the history
Additional tests for new helpers and Store implementations
  • Loading branch information
leeturner committed May 21, 2024
2 parents 1d3f0e6 + 03750cf commit db9dc91
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public Object apply(List<?> list, Options options) throws IOException {
}

if (position != null) {
if (position < 0 || position > mutableList.size()) {
return handleError(
"position must be greater than or equal to 0 and less than or equal to the size of the list");
}
mutableList.add(position, newValue);
} else {
mutableList.add(newValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public Object apply(List<?> list, Options options) throws IOException {

final Integer positionToRemove = getFirstNonNull(position, list.size() - 1);
final ArrayList<Object> mutableList = new ArrayList<>(list);
if (position != null && (position < 0 || position > mutableList.size())) {
return handleError(
"position must be greater than or equal to 0 and less than or equal to the size of the list");
}
mutableList.remove((int) positionToRemove);

return mutableList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2023 Thomas Akehurst
* Copyright (C) 2015-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.
Expand All @@ -26,6 +26,7 @@
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.github.tomakehurst.wiremock.matching.CustomMatcherDefinition;
import com.github.tomakehurst.wiremock.matching.MatchResult;
import com.github.tomakehurst.wiremock.matching.RequestMatcher;
import com.github.tomakehurst.wiremock.matching.RequestMatcherExtension;
Expand Down Expand Up @@ -112,7 +113,21 @@ public void namedCustomRequestMatcherCanBeCombinedWithStandardMatchers() {
}

@Test
public void throwsExecptionIfInlineCustomMatcherUsedWithRemote() {
public void customMatcherDefinitionCanBeCombinedWithStandardMatchers() {
wm.register(
get(urlPathMatching("/the/.*/one"))
.andMatching(
new CustomMatcherDefinition(
"path-contains-param", Parameters.one("path", "correct")))
.willReturn(ok()));

assertThat(client.get("/the/correct/one").statusCode(), is(200));
assertThat(client.get("/the/wrong/one").statusCode(), is(404));
assertThat(client.postJson("/the/correct/one", "{}").statusCode(), is(404));
}

@Test
public void throwsExceptionIfInlineCustomMatcherUsedWithRemote() {
assertThrows(
AdminException.class,
() ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,15 +863,84 @@ public void generatesAnArrayLiteral() {
}

@Test
void addsArrayItem() {
void addsArrayItemWthSpecifiedIntegerPosition() {
assertThat(transform("{{arrayAdd (array 1 'three') 2 position=1}}"), is("[1, 2, three]"));
}

@Test
void deletesArrayItem() {
void addsArrayItemWthSpecifiedStartPosition() {
assertThat(transform("{{arrayAdd (array 1 'three') 2 position='start'}}"), is("[2, 1, three]"));
}

@Test
void addsArrayItemWthSpecifiedEndPosition() {
assertThat(transform("{{arrayAdd (array 1 'three') 2 position='end'}}"), is("[1, three, 2]"));
}

@Test
void addsArrayItemWithNoPositionAddsToEnd() {
assertThat(transform("{{arrayAdd (array 1 'three') 2}}"), is("[1, three, 2]"));
}

@Test
void addsArrayItemWithNegativePositionThrowsAnError() {
assertThat(
transform("{{arrayAdd (array 1 'three') 2 position=-2}}"),
is(
"[ERROR: position must be greater than or equal to 0 and less than or equal to the size of the list]"));
}

@Test
void addsArrayItemWithPositionGreaterThanTheArrayLengthThrowsAnError() {
assertThat(
transform("{{arrayAdd (array 1 'three') 2 position=3}}"),
is(
"[ERROR: position must be greater than or equal to 0 and less than or equal to the size of the list]"));
}

@Test
void addsArrayItemWithMissingValueToAdd() {
assertThat(
transform("{{arrayAdd (array 1 'three') position=1}}"),
is("[ERROR: Missing required parameter: additional value to add to list]"));
}

@Test
void deletesArrayItemWthSpecifiedIntegerPosition() {
assertThat(transform("{{arrayRemove (array 1 2 'three') position=1}}"), is("[1, three]"));
}

@Test
void deletesArrayItemWthSpecifiedStartPosition() {
assertThat(transform("{{arrayRemove (array 1 2 'three') position='start'}}"), is("[2, three]"));
}

@Test
void deletesArrayItemWthSpecifiedEndPosition() {
assertThat(transform("{{arrayRemove (array 1 2 'three') position='end'}}"), is("[1, 2]"));
}

@Test
void deletesArrayItemWithNoPositionRemovesFromEnd() {
assertThat(transform("{{arrayRemove (array 1 2 'three') }}"), is("[1, 2]"));
}

@Test
void deletesArrayItemWithNegativePositionThrowsAnError() {
assertThat(
transform("{{arrayRemove (array 1 'three') position=-2}}"),
is(
"[ERROR: position must be greater than or equal to 0 and less than or equal to the size of the list]"));
}

@Test
void deletesArrayItemWithPositionGreaterThanTheArrayLengthThrowsAnError() {
assertThat(
transform("{{arrayRemove (array 1 'three') position=3}}"),
is(
"[ERROR: position must be greater than or equal to 0 and less than or equal to the size of the list]"));
}

@Test
public void parsesJsonLiteralToAMapOfMapsVariable() {
String result =
Expand Down Expand Up @@ -1041,9 +1110,40 @@ public void canParseLocalYear() {
}

@Test
void valHelperDefaultsNullValue() {
void valHelperReturnsDefaultsNullValue() {
assertThat(transform("{{val request.query.nonexist or='123'}}"), is("123"));
assertThat(transform("{{val request.query.nonexist default='123'}}"), is("123"));
}

@Test
void valHelperReturnsValueIfNotNullValue() {
assertThat(transform("{{val 'exists'}}"), is("exists"));
assertThat(transform("{{val null}}"), is(""));
assertThat(transform("{{val 'exists' or='123'}}"), is("exists"));
assertThat(transform("{{val 'exists' default='123'}}"), is("exists"));
assertThat(transform("{{val (array 1 2 3) default='123'}}"), is("[1, 2, 3]"));
}

@Test
void valHelperCanAssignValueToNamedVariable() {
assertThat(
transform("{{val 'value for myVar' assign='myVar'}}{{myVar}}"), is("value for myVar"));
assertThat(
transform("{{val null or='other value for myVar' assign='myVar'}}{{myVar}}"),
is("other value for myVar"));
assertThat(
transform("{{val null default='other value for myVar' assign='myVar'}}{{myVar}}"),
is("other value for myVar"));
assertThat(transform("{{val 12 assign='myVar'}}{{myVar}}"), is("12"));
assertThat(transform("{{val (array 1 2 3) assign='myVar'}}{{myVar}}"), is("[1, 2, 3]"));
assertThat(transform("{{val (array 1 2 3) assign='myVar'}}{{join '*' myVar}}"), is("1*2*3"));
}

@Test
void valHelperCanAssignValueToNamedVariableAndMaintainsType() {
assertThat(
transform("{{val 10 assign='myVar'}}{{#lt myVar 20}}Less Than{{else}}More Than{{/lt}}"),
is("Less Than"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ void returnsTheDefaultValueWhenValueIsNull() throws Exception {
void returnsTheValueWhenNotNullAndDefaultIsSpecified() throws Exception {
assertThat(
renderHelperValue(helper, "some value", Map.of("or", "other value")), is("some value"));
assertThat(
renderHelperValue(helper, "some value", Map.of("default", "other value")),
is("some value"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void removesLeastRecentlyAccessedWhenPuttingInExcessOfLimit() {
}

@Test
void sizeLimitRemainsConsistenWhenItemRemoved() {
void sizeLimitRemainsConsistentWhenItemRemoved() {
InMemoryObjectStore store = new InMemoryObjectStore(3);

store.put("one", "1");
Expand All @@ -86,4 +86,17 @@ void sizeLimitRemainsConsistenWhenItemRemoved() {
assertThat(store.getAllKeys().count(), is(3L));
assertThat(store.getAllKeys().collect(toList()), hasItems("three", "four", "five"));
}

@Test
void clearRemovesAllItems() {
InMemoryObjectStore store = new InMemoryObjectStore(3);

store.put("one", "1");
store.put("two", "2");
store.put("three", "3");

store.clear();

assertThat(store.getAllKeys().count(), is(0L));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,49 @@ void computeValueConcurrently() throws InterruptedException {

assertThat(store.get("count").get(), is(101));
}

@Test
void getAllKeysReturnsTheCorrectKeys() {
store.put("the_key", "Store this");
store.put("that_key", "Store that");

assertThat(store.getAllKeys().count(), is(2L));
assertThat(store.getAllKeys().anyMatch("the_key.json"::equals), is(true));
assertThat(store.getAllKeys().anyMatch("that_key.json"::equals), is(true));
}

@Test
void remove() {
store.put("the_key", "Store this");
store.put("that_key", "Store that");

assertThat(store.getAllKeys().count(), is(2L));
assertThat(store.getAllKeys().anyMatch("the_key.json"::equals), is(true));
assertThat(store.getAllKeys().anyMatch("that_key.json"::equals), is(true));

store.remove("the_key");

assertThat(store.getAllKeys().count(), is(1L));
assertThat(store.getAllKeys().anyMatch("the_key.json"::equals), is(false));
assertThat(store.getAllKeys().anyMatch("that_key.json"::equals), is(true));
}

@Test
void clear() {
store.put("the_key", "Store this");
store.put("that_key", "Store that");

assertThat(store.getAllKeys().count(), is(2L));
assertThat(store.getAllKeys().anyMatch("the_key.json"::equals), is(true));
assertThat(store.getAllKeys().anyMatch("that_key.json"::equals), is(true));

store.clear();

assertThat(store.getAllKeys().count(), is(0L));
}

@Test
void getPath() {
assertThat(store.getPath(), is(storeDir.toFile().getAbsolutePath()));
}
}

0 comments on commit db9dc91

Please sign in to comment.