From 8cb59be29ccb4d489b9859e19c180d091d09020a Mon Sep 17 00:00:00 2001 From: John Blum Date: Mon, 4 May 2020 19:40:04 -0700 Subject: [PATCH] Add JsonToPdxConverter interface specifying a contract to convert JSON to PDX. The JSON may be expressed as a byte[] or a String. Resolves gh-67. --- .../json/converter/JsonToPdxConverter.java | 47 +++++++++++++++++ .../JsonToPdxConverterUnitTests.java | 51 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 spring-geode/src/main/java/org/springframework/geode/data/json/converter/JsonToPdxConverter.java create mode 100644 spring-geode/src/test/java/org/springframework/geode/data/json/converter/JsonToPdxConverterUnitTests.java diff --git a/spring-geode/src/main/java/org/springframework/geode/data/json/converter/JsonToPdxConverter.java b/spring-geode/src/main/java/org/springframework/geode/data/json/converter/JsonToPdxConverter.java new file mode 100644 index 00000000..70ae07ca --- /dev/null +++ b/spring-geode/src/main/java/org/springframework/geode/data/json/converter/JsonToPdxConverter.java @@ -0,0 +1,47 @@ +/* + * 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 a {@literal JSON} {@link String} to a {@link PdxInstance}. + * + * @author John Blum + * @see java.lang.FunctionalInterface + * @see org.apache.geode.pdx.PdxInstance + * @see org.springframework.core.convert.converter.Converter + * @since 1.3.0 + */ +@FunctionalInterface +public interface JsonToPdxConverter extends Converter { + + /** + * Converts the array of {@link Byte#TYPE bytes} containing {@literal JSON} into a {@link PdxInstance}. + * + * @param json array of {@link Byte#TYPE bytes} containing {@literal JSON} + * to convert into a {@link PdxInstance}; must not be {@literal null}. + * @return a {@link PdxInstance} converted from the array of {@link Byte#TYPE bytes} containing {@literal JSON}. + * @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/JsonToPdxConverterUnitTests.java b/spring-geode/src/test/java/org/springframework/geode/data/json/converter/JsonToPdxConverterUnitTests.java new file mode 100644 index 00000000..887bdf14 --- /dev/null +++ b/spring-geode/src/test/java/org/springframework/geode/data/json/converter/JsonToPdxConverterUnitTests.java @@ -0,0 +1,51 @@ +/* + * 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.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import org.junit.Test; + +/** + * Unit Tests for {@link JsonToPdxConverter} + * + * @author John Blum + * @see org.junit.Test + * @see org.mockito.Mockito + * @see org.springframework.geode.data.json.converter.JsonToPdxConverter + * @since 1.3.0 + */ +public class JsonToPdxConverterUnitTests { + + @Test + public void convertByteArrayCallsConvertJsonString() { + + JsonToPdxConverter converter = mock(JsonToPdxConverter.class); + + doCallRealMethod().when(converter).convert(any(byte[].class)); + + String json = "{ \"name\": \"Jon Doe\"}"; + + converter.convert(json.getBytes()); + + verify(converter, times(1)).convert(eq(json)); + } +}