Skip to content

Commit

Permalink
#849 Support for path specific options in MvcMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-krecan committed Nov 1, 2024
1 parent 2bf13e1 commit 49090dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.math.BigDecimal;
import java.util.function.BiConsumer;
import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.ConfigurationWhen;
import net.javacrumbs.jsonunit.core.Option;
import net.javacrumbs.jsonunit.core.internal.Path;
import net.javacrumbs.jsonunit.core.internal.matchers.InternalMatcher;
Expand Down Expand Up @@ -113,6 +114,17 @@ public ME when(@NotNull Option firstOption, @NotNull Option... otherOptions) {
return matchers(path, configuration.withOptions(firstOption, otherOptions));
}

/**
* Adds path specific options.
*
* @see Configuration#when(ConfigurationWhen.PathsParam, ConfigurationWhen.ApplicableForPath...)
*/
@NotNull
public ME when(
@NotNull ConfigurationWhen.PathsParam object, @NotNull ConfigurationWhen.ApplicableForPath... actions) {
return matchers(path, configuration.when(object, actions));
}

/**
* Compares JSON for equality. The expected object is converted to JSON
* before comparison. Ignores order of sibling nodes and whitespaces.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package net.javacrumbs.jsonunit.spring.testit;

import static java.math.BigDecimal.valueOf;
import static net.javacrumbs.jsonunit.core.ConfigurationWhen.path;
import static net.javacrumbs.jsonunit.core.ConfigurationWhen.then;
import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER;
import static net.javacrumbs.jsonunit.spring.JsonUnitResultMatchers.json;
import static net.javacrumbs.jsonunit.spring.testit.demo.ExampleController.CORRECT_JSON;
import static net.javacrumbs.jsonunit.spring.testit.demo.ExampleController.ISO_VALUE;
Expand All @@ -28,7 +31,6 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import net.javacrumbs.jsonunit.core.Option;
import net.javacrumbs.jsonunit.core.listener.Difference;
import net.javacrumbs.jsonunit.core.listener.DifferenceContext;
import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
Expand Down Expand Up @@ -65,6 +67,13 @@ void shouldPassIfEqualsWithProduces() throws Exception {
exec("/sampleProduces").andExpect(json().isEqualTo(CORRECT_JSON));
}

@Test
void shouldAllowOptionsOnPath() throws Exception {
exec("/sampleProduces")
.andExpect(json().when(path("result.array"), then(IGNORING_ARRAY_ORDER))
.isEqualTo(CORRECT_JSON));
}

@Test
void shouldPassIfEqualsWithIsoEncoding() throws Exception {
exec("/sampleIso").andExpect(json().node("result").isEqualTo(ISO_VALUE));
Expand Down Expand Up @@ -234,9 +243,7 @@ void shouldSetTolerance() throws Exception {

@Test
void settingOptionShouldTakeEffect() throws Exception {
exec().andExpect(json().node("result.array")
.when(Option.IGNORING_ARRAY_ORDER)
.isEqualTo(new int[] {3, 2, 1}));
exec().andExpect(json().node("result.array").when(IGNORING_ARRAY_ORDER).isEqualTo(new int[] {3, 2, 1}));
}

@Test
Expand Down

0 comments on commit 49090dc

Please sign in to comment.