From e67c383de81bf0ce0f3397df1e1219110b0b24ae Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 6 May 2020 00:16:21 -0700 Subject: [PATCH] Add JsonToPdxArrayConverter interface and Spring Converter extension specifying a contract to convert JSON into PDX, as an array of PdxInstance objects. Resolves gh-69. --- .../converter/JsonToPdxArrayConverter.java | 50 ++++++++++++++ .../JsonToPdxArrayConverterUnitTests.java | 68 +++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 spring-geode/src/main/java/org/springframework/geode/data/json/converter/JsonToPdxArrayConverter.java create mode 100644 spring-geode/src/test/java/org/springframework/geode/data/json/converter/JsonToPdxArrayConverterUnitTests.java diff --git a/spring-geode/src/main/java/org/springframework/geode/data/json/converter/JsonToPdxArrayConverter.java b/spring-geode/src/main/java/org/springframework/geode/data/json/converter/JsonToPdxArrayConverter.java new file mode 100644 index 00000000..133d7f1f --- /dev/null +++ b/spring-geode/src/main/java/org/springframework/geode/data/json/converter/JsonToPdxArrayConverter.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 org.apache.geode.pdx.PdxInstance; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.lang.NonNull; + +/** + * A Spring {@link Converter} interface extension defining a contract to convert + * from {@link String JSON} to an array of {@link PdxInstance} objects. + * + * @author John Blum + * @see java.lang.FunctionalInterface + * @see java.lang.String + * @see org.apache.geode.pdx.PdxInstance + * @see org.springframework.core.convert.converter.Converter + * @since 1.3.0 + */ +@FunctionalInterface +public interface JsonToPdxArrayConverter extends Converter { + + /** + * Converts the array of {@link Byte#TYPE bytes} containing {@link String JSON} + * into an array of {@link PdxInstance} objects. + * + * @param json array of {@link Byte#TYPE bytes} containing the {@link String JSON}; + * must not be {@literal null}. + * @return an array of {@link PdxInstance} objects for the given {@link String JSON}. + * @see org.apache.geode.pdx.PdxInstance + * @see #convert(Object) + */ + default @NonNull PdxInstance[] convert(@NonNull byte[] json) { + return convert(new String(json)); + } +} diff --git a/spring-geode/src/test/java/org/springframework/geode/data/json/converter/JsonToPdxArrayConverterUnitTests.java b/spring-geode/src/test/java/org/springframework/geode/data/json/converter/JsonToPdxArrayConverterUnitTests.java new file mode 100644 index 00000000..c2af173d --- /dev/null +++ b/spring-geode/src/test/java/org/springframework/geode/data/json/converter/JsonToPdxArrayConverterUnitTests.java @@ -0,0 +1,68 @@ +/* + * 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.anyString; +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 static org.mockito.Mockito.verifyNoInteractions; + +import org.junit.Test; + +import org.apache.geode.pdx.PdxInstance; + +import org.springframework.data.gemfire.util.ArrayUtils; +import org.springframework.geode.data.json.converter.support.JacksonJsonToPdxConverter; + +/** + * Unit Tests for {@link JsonToPdxArrayConverter}. + * + * @author John Blum + * @see org.junit.Test + * @see org.mockito.Mockito + * @see org.apache.geode.pdx.PdxInstance + * @see org.springframework.geode.data.json.converter.JsonToPdxArrayConverter + * @since 1.3.0 + */ +public class JsonToPdxArrayConverterUnitTests { + + @Test + public void convertJsonByteArrayToPdxArray() { + + String json = "[{ \"name\": \"Jon Doe\" }, { \"name\": \"Jane Doe\" }]"; + + PdxInstance mockPdxInstanceOne = mock(PdxInstance.class); + PdxInstance mockPdxInstanceTwo = mock(PdxInstance.class); + + PdxInstance[] pdxArray = ArrayUtils.asArray(mockPdxInstanceOne, mockPdxInstanceTwo); + + JacksonJsonToPdxConverter converter = mock(JacksonJsonToPdxConverter.class); + + doCallRealMethod().when(converter).convert(any(byte[].class)); + doReturn(pdxArray).when(converter).convert(anyString()); + + assertThat(converter.convert(json.getBytes())).isEqualTo(pdxArray); + + verify(converter, times(1)).convert(eq(json)); + verifyNoInteractions(mockPdxInstanceOne, mockPdxInstanceTwo); + } +}