Skip to content

Commit

Permalink
wrapValueWithQuotes accepts nulls
Browse files Browse the repository at this point in the history
without this change NPE is thrown

fixes gh-2117
  • Loading branch information
marcingrzejszczak committed Jun 27, 2024
1 parent 56673bd commit e08f13f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ private static String stringWithEscapedQuotes(Object object) {
return stringValue.replaceAll("\"", "\\\\\"");
}

private static String wrapValueWithQuotes(Object value) {
static String wrapValueWithQuotes(Object value) {
if (value == null) {
return null;
}
return value instanceof String ? "\"" + stringWithEscapedQuotes(value) + "\"" : value.toString();
}

Expand Down Expand Up @@ -198,8 +201,8 @@ public MethodBufferingJsonVerifiable isEqualTo(Number value) {
readyToCheck.methodsBuffer.offer(".value()");
}
else {
readyToCheck.appendMethodWithValue("isEqualTo",
value instanceof Long ? String.valueOf(value).concat("L") : String.valueOf(value));
readyToCheck.appendMethodWithValue("isEqualTo", value instanceof Long ? String.valueOf(value).concat("L")
: (value == null ? null : String.valueOf(value)));
}
return readyToCheck;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2020-2020 the original author or authors.
*
* 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
*
* https://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 org.springframework.cloud.contract.verifier.util;

import org.assertj.core.api.BDDAssertions;
import org.junit.jupiter.api.Test;

class DelegatingJsonVerifiableTests {

@Test
void shouldReturnNullValueForNull() {
String value = DelegatingJsonVerifiable.wrapValueWithQuotes(null);

BDDAssertions.then(value).isNull();
}

@Test
void shouldReturnQuotedValueForString() {
String value = DelegatingJsonVerifiable.wrapValueWithQuotes("hello");

BDDAssertions.then(value).isEqualTo("\"hello\"");
}

@Test
void shouldReturnStringValueForNonString() {
String value = DelegatingJsonVerifiable.wrapValueWithQuotes(5);

BDDAssertions.then(value).isEqualTo("5");
}

}

0 comments on commit e08f13f

Please sign in to comment.