diff --git a/spring-geode/src/main/java/org/springframework/geode/data/json/converter/ObjectArrayToJsonConverter.java b/spring-geode/src/main/java/org/springframework/geode/data/json/converter/ObjectArrayToJsonConverter.java new file mode 100644 index 00000000..03949a59 --- /dev/null +++ b/spring-geode/src/main/java/org/springframework/geode/data/json/converter/ObjectArrayToJsonConverter.java @@ -0,0 +1,50 @@ +/* + * Copyright 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.geode.data.json.converter; + +import java.util.Arrays; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.data.gemfire.util.ArrayUtils; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; + +/** + * A Spring {@link Converter} interface extension defining a contract to convert + * an {@link Iterable} or an array of {@link Object Objects} into {@link String JSON}. + * + * @author John Blum + * @see java.lang.FunctionalInterface + * @see java.lang.Iterable + * @see java.lang.Object + * @see java.lang.String + * @see org.springframework.core.convert.converter.Converter + * @since 1.3.0 + */ +@FunctionalInterface +public interface ObjectArrayToJsonConverter extends Converter, String> { + + /** + * Converts the given array of {@link Object Objects} into {@link String JSON}. + * + * @param array array of {@link Object Objects} to convert into {@link String JSON}. + * @return {@link String JSON} generated from the given array of {@link Object Objects}. + * @see #convert(Object) + */ + default @NonNull String convert(@Nullable Object... array) { + return convert(Arrays.asList(ArrayUtils.nullSafeArray(array, Object.class))); + } +} diff --git a/spring-geode/src/test/java/org/springframework/geode/data/json/converter/ObjectArrayToJsonConverterUnitTests.java b/spring-geode/src/test/java/org/springframework/geode/data/json/converter/ObjectArrayToJsonConverterUnitTests.java new file mode 100644 index 00000000..09838139 --- /dev/null +++ b/spring-geode/src/test/java/org/springframework/geode/data/json/converter/ObjectArrayToJsonConverterUnitTests.java @@ -0,0 +1,93 @@ +/* + * Copyright 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.geode.data.json.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.Test; +import org.mockito.ArgumentMatchers; + +import example.app.crm.model.Customer; + +/** + * Unit Tests for {@link ObjectArrayToJsonConverter}. + * + * @author John Blum + * @see org.junit.Test + * @see org.mockito.Mockito + * @see org.springframework.geode.data.json.converter.ObjectArrayToJsonConverter + * @since 1.3.0 + */ +public class ObjectArrayToJsonConverterUnitTests { + + @Test + public void convertObjectArrayCallsConvertIterable() { + + String json = "[{ \"name\": \"Jon Doe\"}, { \"name\": \"Jane Doe\"}]"; + + ObjectArrayToJsonConverter mockConverter = mock(ObjectArrayToJsonConverter.class); + + doCallRealMethod().when(mockConverter).convert(ArgumentMatchers.any()); + doReturn(json).when(mockConverter).convert(any(Iterable.class)); + + Customer jonDoe = Customer.newCustomer(1L, "Jon Doe"); + Customer janeDoe = Customer.newCustomer(2L, "Jane Doe"); + + assertThat(mockConverter.convert(jonDoe, janeDoe)).isEqualTo(json); + + verify(mockConverter, times(1)).convert(eq(Arrays.asList(jonDoe, janeDoe))); + } + + @Test + public void convertEmptyObjectArrayToEmptyJsonArray() { + + String json = "[]"; + + ObjectArrayToJsonConverter mockConverter = mock(ObjectArrayToJsonConverter.class); + + doCallRealMethod().when(mockConverter).convert(ArgumentMatchers.any()); + doReturn(json).when(mockConverter).convert(any(Iterable.class)); + + assertThat(mockConverter.convert()).isEqualTo(json); + + verify(mockConverter, times(1)).convert(eq(Collections.emptyList())); + } + + @Test + public void convertNullObjectArrayIsNullSafeAndReturnsEmptyJsonArray() { + + String json = "[]"; + + ObjectArrayToJsonConverter mockConverter = mock(ObjectArrayToJsonConverter.class); + + doCallRealMethod().when(mockConverter).convert(ArgumentMatchers.any()); + doReturn(json).when(mockConverter).convert(any(Iterable.class)); + + assertThat(mockConverter.convert((Object[]) null)).isEqualTo(json); + + verify(mockConverter, times(1)).convert(eq(Collections.emptyList())); + } +}