Add JsonToPdxArrayConverter interface and Spring Converter extension specifying a contract to convert JSON into PDX, as an array of PdxInstance objects.

Resolves gh-69.
This commit is contained in:
John Blum
2020-05-06 00:16:21 -07:00
parent bbef166670
commit e67c383de8
2 changed files with 118 additions and 0 deletions

View File

@@ -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<String, PdxInstance[]> {
/**
* 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));
}
}

View File

@@ -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);
}
}