Here are the examples of the java api org.apache.geode.pdx.PdxInstance taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
99 Examples
19
Source : JsonCacheDataImporterExporterIntegrationTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void exampleRegionContainsJonDoe() {
resourceLocationSupplier = () -> "clreplacedpath:data-#{#regionName}-jondoe.json";
Region<?, ?> example = getExampleRegion(newApplicationContext(TestGeodeConfiguration.clreplaced));
replacedertThat(example).hreplacedize(1);
Object value = example.values().stream().findFirst().orElse(null);
replacedertThat(value).isInstanceOf(PdxInstance.clreplaced);
PdxInstance pdxInstance = (PdxInstance) value;
replacedertThat(pdxInstance.getField("id")).isEqualTo((byte) 1);
replacedertThat(pdxInstance.getField("name")).isEqualTo("Jon Doe");
replacedertThat(pdxInstance.getField("@type")).isEqualTo(Customer.clreplaced.getName());
Customer jonDoe = (Customer) pdxInstance.getObject();
replacedertThat(jonDoe).isNotNull();
replacedertThat(jonDoe.getId()).isEqualTo(1);
replacedertThat(jonDoe.getName()).isEqualTo("Jon Doe");
}
19
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test(expected = DataRetrievalFailureException.clreplaced)
public void decorateJsonThrowsDataRetrievalFailureException() throws JsonProcessingException {
String json = "{ \"name\": \"Jon Doe\" }";
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
ObjectMapper mockObjectMapper = mock(ObjectMapper.clreplaced);
JSONFormatterPdxToJsonConverter converter = spy(new JSONFormatterPdxToJsonConverter());
doReturn(false).when(mockPdxInstance).hasField(anyString());
doReturn(mockObjectMapper).when(converter).newObjectMapper(eq(json));
doThrow(new JsonGenerationException("TEST", (JsonGenerator) null)).when(mockObjectMapper).readTree(eq(json));
doReturn(JSONFormatter.JSON_CLreplacedNAME).when(mockPdxInstance).getClreplacedName();
try {
converter.decorate(mockPdxInstance, json);
} catch (DataRetrievalFailureException expected) {
replacedertThat(expected).hasMessageStartingWith("Failed to parse JSON [%s]", json);
replacedertThat(expected).hasCauseInstanceOf(JsonGenerationException.clreplaced);
replacedertThat(expected.getCause()).hasMessage("TEST");
replacedertThat(expected.getCause()).hasNoCause();
throw expected;
} finally {
verify(mockPdxInstance, times(1)).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verify(converter, times(1)).newObjectMapper(eq(json));
verify(mockObjectMapper, times(1)).readTree(eq(json));
verify(mockPdxInstance, times(1)).getClreplacedName();
verifyNoMoreInteractions(mockPdxInstance, mockObjectMapper);
}
}
19
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test(expected = IllegalArgumentException.clreplaced)
public void decoratePdxInstanceWithInvalidClreplacedName() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(false).when(mockPdxInstance).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
doReturn(JSONFormatter.JSON_CLreplacedNAME).when(mockPdxInstance).getClreplacedName();
JSONFormatterPdxToJsonConverter converter = spy(new JSONFormatterPdxToJsonConverter());
try {
converter.decorate(mockPdxInstance);
} catch (IllegalArgumentException expected) {
replacedertThat(expected).hasMessage("Clreplaced name [%s] is required and cannot be equal to [%s]", JSONFormatter.JSON_CLreplacedNAME);
replacedertThat(expected).hasNoCause();
throw expected;
} finally {
verify(mockPdxInstance, times(1)).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verify(mockPdxInstance, times(2)).getClreplacedName();
verify(converter, never()).newPdxInstanceBuilder();
}
}
19
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
private void testHasValidClreplacedNameWithPdxInstanceHavingInvalidClreplacedName(String clreplacedName) {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(clreplacedName).when(mockPdxInstance).getClreplacedName();
replacedertThat(new JSONFormatterPdxToJsonConverter().hasValidClreplacedName(mockPdxInstance)).isFalse();
verify(mockPdxInstance, times(1)).getClreplacedName();
verifyNoMoreInteractions(mockPdxInstance);
}
19
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test(expected = IllegalArgumentException.clreplaced)
public void decoratePdxInstanceWithNoClreplacedName() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(false).when(mockPdxInstance).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
doReturn(null).when(mockPdxInstance).getClreplacedName();
JSONFormatterPdxToJsonConverter converter = spy(new JSONFormatterPdxToJsonConverter());
try {
converter.decorate(mockPdxInstance);
} catch (IllegalArgumentException expected) {
replacedertThat(expected).hasMessage("Clreplaced name [null] is required and cannot be equal to [%s]", JSONFormatter.JSON_CLreplacedNAME);
replacedertThat(expected).hasNoCause();
throw expected;
} finally {
verify(mockPdxInstance, times(1)).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verify(mockPdxInstance, times(2)).getClreplacedName();
verify(converter, never()).newPdxInstanceBuilder();
}
}
19
Source : ObjectPdxInstanceAdapter.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
/**
* Null-safe factory method used to unwrap the given {@link PdxInstance}, returning the underlying, target
* {@link PdxInstance#getObject() Object} upon which this {@link PdxInstance} is based.
*
* @param pdxInstance {@link PdxInstance} to unwrap.
* @return the underlying, target {@link PdxInstance#getObject() Object} from the given {@link PdxInstance}.
* @see org.apache.geode.pdx.PdxInstance
*/
@Nullable
public static Object unwrap(@Nullable PdxInstance pdxInstance) {
return pdxInstance instanceof ObjectPdxInstanceAdapter ? pdxInstance.getObject() : pdxInstance;
}
19
Source : JsonCacheDataImporterExporter.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
/**
* Post processes the given {{@link PdxInstance}.
*
* @param pdxInstance {@link PdxInstance} to process.
* @return the {@link PdxInstance}.
* @see org.apache.geode.pdx.PdxInstance
*/
protected PdxInstance postProcess(PdxInstance pdxInstance) {
return pdxInstance;
}
19
Source : JSONFormatterPdxToJsonConverter.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
private boolean isMissingObjectTypeMetadata(@Nullable PdxInstance pdxInstance) {
return pdxInstance != null && !pdxInstance.hasField(AT_TYPE_METADATA_PROPERTY_NAME);
}
19
Source : JSONFormatterPdxToJsonConverter.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
private boolean isDecorationRequired(@Nullable PdxInstance pdxInstance, @Nullable String json) {
return isMissingObjectTypeMetadata(pdxInstance) && isValidJson(json);
}
19
Source : PdxInstanceWrapper.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
/**
* The {@link PdxInstanceWrapper} clreplaced is an implementation of the {@link PdxInstance} interface
* wrapping an existing {@link PdxInstance} object and decorating the functionality.
*
* @author John Blum
* @see java.util.function.Function
* @see com.fasterxml.jackson.databind.ObjectMapper
* @see org.apache.geode.pdx.JSONFormatter
* @see org.apache.geode.pdx.PdxInstance
* @see org.apache.geode.pdx.WritablePdxInstance
* @since 1.3.0
*/
public clreplaced PdxInstanceWrapper implements PdxInstance, Sendable {
public static final String AT_IDENTIFIER_FIELD_NAME = "@identifier";
public static final String AT_TYPE_FIELD_NAME = "@type";
public static final String CLreplaced_NAME_PROPERTY = "clreplacedName";
public static final String ID_FIELD_NAME = "id";
protected static final String NO_FIELD_NAME = "";
protected static final String ARRAY_BEGIN = "[";
protected static final String ARRAY_END = "]";
protected static final String COMMA = ",";
protected static final String EMPTY_STRING = "";
protected static final String FIELD_TYPE_VALUE = "\"%1$s\"(%2$s): \"%3$s\"";
protected static final String INDENT_STRING = "\t";
protected static final String NEW_LINE = "\n";
protected static final String COMMA_NEW_LINE = "," + NEW_LINE;
protected static final String COMMA_SPACE = COMMA + " ";
protected static final String OBJECT_BEGIN = "{";
protected static final String OBJECT_END = "}";
/**
* Smart, {@literal null-safe} factory method used to evaluate the given {@link Object} and wrap the {@link Object}
* in a new instance of {@link PdxInstanceWrapper} if the {@link Object} is an instance of {@link PdxInstance}
* or return the given {@link Object} as is.
*
* @param target {@link Object} to evaluate
* @return the {@link Object} wrapped in a new instance of {@link PdxInstanceWrapper} if {@link Object}
* is an instance of {@link PdxInstance}, otherwise returns the given {@link Object}.
* @see org.apache.geode.pdx.PdxInstance
* @see java.lang.Object
* @see #from(PdxInstance)
*/
public static Object from(Object target) {
return target instanceof PdxInstance ? from((PdxInstance) target) : target;
}
/**
* Factory method used to construct a new instance of {@link PdxInstanceWrapper} initialized with the given,
* required {@link PdxInstance} used to back the wrapper.
*
* @param pdxInstance {@link PdxInstance} object used to back this wrapper; must not be {@literal null}.
* @return a new instance of {@link PdxInstanceWrapper} initialized with the given {@link PdxInstance}.
* @throws IllegalArgumentException if {@link PdxInstance} is {@literal null}.
* @see org.apache.geode.pdx.PdxInstance
* @see #PdxInstanceWrapper(PdxInstance)
*/
public static PdxInstanceWrapper from(PdxInstance pdxInstance) {
return pdxInstance instanceof PdxInstanceWrapper ? (PdxInstanceWrapper) pdxInstance : new PdxInstanceWrapper(pdxInstance);
}
/**
* Null-safe factory method used to unwrap the given {@link PdxInstance}.
*
* If the given {@link PdxInstance} is an instance of {@link PdxInstanceWrapper} then this factory method will
* unwrap the {@link PdxInstanceWrapper} returning the underlying, {@link PdxInstanceWrapper#getDelegate() delegate}
* {@link PdxInstance}. Otherwise, the given {@link PdxInstance} is returned.
*
* @param pdxInstance {@link PdxInstance} to unwrap; may be {@literal null}.
* @return the unwrapped {@link PdxInstance}.
* @see org.apache.geode.pdx.PdxInstance
* @see #getDelegate()
*/
public static PdxInstance unwrap(PdxInstance pdxInstance) {
return pdxInstance instanceof PdxInstanceWrapper ? ((PdxInstanceWrapper) pdxInstance).getDelegate() : pdxInstance;
}
private final PdxInstance delegate;
/**
* Constructs a new instance of {@link PdxInstanceWrapper} initialized with the given, required {@link PdxInstance}
* object used to back this wrapper.
*
* @param pdxInstance {@link PdxInstance} object used to back this wrapper; must not be {@literal null}.
* @throws IllegalArgumentException if {@link PdxInstance} is {@literal null}.
* @see org.apache.geode.pdx.PdxInstance
*/
public PdxInstanceWrapper(PdxInstance pdxInstance) {
replacedertThat(pdxInstance).isNotNull();
this.delegate = pdxInstance;
}
/**
* Returns a reference to the configured, underlying {@link PdxInstance} backing this wrapper.
*
* @return a reference to the configured, underlying {@link PdxInstance} backing this wrapper;
* never {@literal null}.
* @see org.apache.geode.pdx.PdxInstance
*/
public PdxInstance getDelegate() {
return this.delegate;
}
/**
* Returns an {@link Optional} reference to a configured Jackson {@link ObjectMapper} used to
* deserialize the {@link String JSON} generated from {@link PdxInstance PDX} back into an {@link Object}.
*
* This method is meant ot be overridden by {@link Clreplaced subclreplacedes}.
*
* @return an {@link Optional} {@link ObjectMapper}.
* @see com.fasterxml.jackson.databind.ObjectMapper
* @see java.util.Optional
*/
protected Optional<ObjectMapper> getObjectMapper() {
ObjectMapper objectMapper = newObjectMapper().configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true).findAndRegisterModules();
return Optional.of(objectMapper);
}
/**
* Constructs a new instance of Jackson's {@link ObjectMapper}.
*
* @return a new instance of Jackson's {@link ObjectMapper}; never {@literal null}.
* @see com.fasterxml.jackson.databind.ObjectMapper
*/
ObjectMapper newObjectMapper() {
return new ObjectMapper();
}
/**
* @inheritDoc
*/
@Override
public String getClreplacedName() {
return getDelegate().getClreplacedName();
}
/**
* @inheritDoc
*/
@Override
public boolean isDeserializable() {
return getDelegate().isDeserializable();
}
/**
* @inheritDoc
*/
@Override
public boolean isEnum() {
return getDelegate().isEnum();
}
/**
* @inheritDoc
*/
@Override
public Object getField(String fieldName) {
return getDelegate().getField(fieldName);
}
/**
* @inheritDoc
*/
@Override
public List<String> getFieldNames() {
return getDelegate().getFieldNames();
}
/**
* Determines the {@link Object identifier} for, or {@link PdxInstance#isIdenreplacedyField(String) idenreplacedy} of,
* this {@link PdxInstance}.
*
* @return the {@link Object identifier} for this {@link PdxInstance}; never {@literal null}.
* @throws IllegalStateException if the {@link PdxInstance} does not have an id.
* @see #isIdenreplacedyField(String)
* @see #getField(String)
* @see #getFieldNames()
* @see #getId()
*/
public Object getIdentifier() {
Optional<String> idenreplacedyFieldName = nullSafeList(getFieldNames()).stream().filter(this::hasText).filter(this::isIdenreplacedyField).findFirst();
return idenreplacedyFieldName.map(this::getField).orElseGet(this::getId);
}
/**
* Searches for a PDX {@link String field name} called {@literal id} on this {@link PdxInstance}
* and returns its {@link Object value} as the {@link Object identifier} for,
* or {@link PdxInstance#isIdenreplacedyField(String) idenreplacedy} of, this {@link PdxInstance}.
*
* @return the {@link Object value} of the {@literal id} {@link String field} on this {@link PdxInstance}.
* @throws IllegalStateException if this {@link PdxInstance} does not have an id.
* @see #getAtIdentifier()
* @see #getField(String)
* @see #hasField(String)
*/
protected Object getId() {
return hasField(ID_FIELD_NAME) ? getField(ID_FIELD_NAME) : getAtIdentifier();
}
/**
* Searches for a PDX {@link String field} declared by the {@literal @identifier} metadata {@link String field}
* on this {@link PdxInstance} and returns the {@link Object value} of this {@link String field}
* as the {@link Object identifier} for, or {@link PdxInstance#isIdenreplacedyField(String) idenreplacedy} of,
* this {@link PdxInstance}.
*
* @return the {@link Object value} of the {@link String field} declared in the {@literal @identifier} metadata
* {@link String field} on this {@link PdxInstance}.
* @throws IllegalStateException if the {@link PdxInstance} does not have an id.
* @see org.apache.geode.pdx.PdxInstance
*/
protected Object getAtIdentifier() {
return Optional.of(AT_IDENTIFIER_FIELD_NAME).filter(this::hasField).map(this::getField).map(String::valueOf).filter(this::hasField).map(this::getField).orElseThrow(() -> new IllegalStateException(String.format("PdxInstance for type [%1$s] has no %2$s", getClreplacedName(), resolveMessageForIdentifierError(this))));
}
private String resolveMessageForIdentifierError(PdxInstance pdxInstance) {
String message = "declared identifier";
if (pdxInstance.hasField(ID_FIELD_NAME)) {
message = "id";
} else if (pdxInstance.hasField(AT_IDENTIFIER_FIELD_NAME)) {
Object atIdentifierFieldValue = pdxInstance.getField(AT_IDENTIFIER_FIELD_NAME);
String resolvedIdentifierFieldName = Objects.nonNull(atIdentifierFieldValue) ? atIdentifierFieldValue.toString().trim() : NO_FIELD_NAME;
boolean identifierFieldNameWasDeclaredAndIsValid = pdxInstance.hasField(resolvedIdentifierFieldName);
Object identifier = identifierFieldNameWasDeclaredAndIsValid ? pdxInstance.getField(resolvedIdentifierFieldName) : null;
String ifMessage = "value [%s] for field [%s] declared in [%s]";
String elseMessage = "field [%s] declared in [%s]";
message = identifierFieldNameWasDeclaredAndIsValid ? String.format(ifMessage, identifier, resolvedIdentifierFieldName, AT_IDENTIFIER_FIELD_NAME) : String.format(elseMessage, resolvedIdentifierFieldName, AT_IDENTIFIER_FIELD_NAME);
}
return message;
}
/**
* @inheritDoc
*/
@Override
public boolean isIdenreplacedyField(String fieldName) {
return getDelegate().isIdenreplacedyField(fieldName);
}
/**
* Materializes an {@link Object} from the PDX bytes described by this {@link PdxInstance}.
*
* If these PDX bytes describe an {@link Object} parsed from JSON, then the JSON is reconstructed from
* this {@link PdxInstance} and mapped to an instance of the {@link Clreplaced type} identified by
* the {@literal @type} metadata PDX {@link String field} using Jackson's {@link ObjectMapper}.
*
* @return an {@link Object} constructed from the PDX bytes described by this {@link PdxInstance}.
* @see com.fasterxml.jackson.databind.ObjectMapper
* @see java.lang.Object
* @see #getObjectMapper()
*/
@Override
public Object getObject() {
return getObjectMapper().filter(objectMapper -> JSONFormatter.JSON_CLreplacedNAME.equals(getClreplacedName())).filter(objectMapper -> hasField(AT_TYPE_FIELD_NAME)).<Object>map(objectMapper -> {
try {
String typeName = String.valueOf(getField(AT_TYPE_FIELD_NAME));
Clreplaced<?> type = Clreplaced.forName(typeName);
String json = jsonFormatterToJson(getDelegate());
return objectMapper.readValue(json, type);
} catch (Throwable ignore) {
// TODO Log Throwable?
return null;
}
}).orElseGet(() -> getDelegate().getObject());
}
/**
* Calls {@link JSONFormatter#toJSON(PdxInstance)} to convert the {@link PdxInstance} into {@link String JSON}.
*
* @param pdxInstance {@link PdxInstance} to convert to {@link String JSON}.
* @return {@link String JSON} generated from the given {@link PdxInstance}.
* @see org.apache.geode.pdx.JSONFormatter#toJSON(PdxInstance)
* @see org.apache.geode.pdx.PdxInstance
*/
String jsonFormatterToJson(PdxInstance pdxInstance) {
return JSONFormatter.toJSON(pdxInstance);
}
/**
* @inheritDoc
*/
@Override
public WritablePdxInstance createWriter() {
return getDelegate().createWriter();
}
/**
* @inheritDoc
*/
@Override
public boolean hasField(String fieldName) {
return getDelegate().hasField(fieldName);
}
/**
* @inheritDoc
*/
@Override
public void sendTo(DataOutput out) throws IOException {
PdxInstance delegate = getDelegate();
if (delegate instanceof Sendable) {
((Sendable) delegate).sendTo(out);
}
}
/**
* Returns a {@link String} representation of this {@link PdxInstance}.
*
* @return a {@link String} representation of this {@link PdxInstance}.
* @see java.lang.String
*/
@Override
public String toString() {
// return getDelegate().toString();
return toString(this);
}
private String toString(PdxInstance pdx) {
return toString(pdx, "");
}
private String toString(PdxInstance pdx, String indent) {
if (Objects.nonNull(pdx)) {
StringBuilder buffer = new StringBuilder(OBJECT_BEGIN).append(NEW_LINE);
String fieldIndent = indent + INDENT_STRING;
buffer.append(fieldIndent).append(formatFieldValue(CLreplaced_NAME_PROPERTY, pdx.getClreplacedName()));
for (String fieldName : nullSafeList(pdx.getFieldNames())) {
Object fieldValue = pdx.getField(fieldName);
String valueString = toStringObject(fieldValue, fieldIndent);
buffer.append(COMMA_NEW_LINE);
buffer.append(fieldIndent).append(formatFieldValue(fieldName, valueString));
}
buffer.append(NEW_LINE).append(indent).append(OBJECT_END);
return buffer.toString();
} else {
return null;
}
}
private String toStringArray(Object value, String indent) {
Object[] array = (Object[]) value;
StringBuilder buffer = new StringBuilder(ARRAY_BEGIN);
boolean addComma = false;
for (Object element : array) {
buffer.append(addComma ? COMMA_SPACE : EMPTY_STRING);
buffer.append(toStringObject(element, indent));
addComma = true;
}
buffer.append(ARRAY_END);
return buffer.toString();
}
private String toStringObject(Object value, String indent) {
return isPdxInstance(value) ? toString((PdxInstance) value, indent) : isArray(value) ? toStringArray(value, indent) : String.valueOf(value);
}
private String formatFieldValue(String fieldName, Object fieldValue) {
return String.format(FIELD_TYPE_VALUE, fieldName, nullSafeType(fieldValue), fieldValue);
}
private boolean hasText(String value) {
return value != null && !value.trim().isEmpty();
}
private boolean isArray(Object value) {
return Objects.nonNull(value) && value.getClreplaced().isArray();
}
private boolean isPdxInstance(Object value) {
return value instanceof PdxInstance;
}
private <T> List<T> nullSafeList(List<T> list) {
return list != null ? list : Collections.emptyList();
}
private Clreplaced<?> nullSafeType(Object value) {
return value != null ? value.getClreplaced() : Object.clreplaced;
}
}
19
Source : PdxInstanceWrapper.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
/**
* Null-safe factory method used to unwrap the given {@link PdxInstance}.
*
* If the given {@link PdxInstance} is an instance of {@link PdxInstanceWrapper} then this factory method will
* unwrap the {@link PdxInstanceWrapper} returning the underlying, {@link PdxInstanceWrapper#getDelegate() delegate}
* {@link PdxInstance}. Otherwise, the given {@link PdxInstance} is returned.
*
* @param pdxInstance {@link PdxInstance} to unwrap; may be {@literal null}.
* @return the unwrapped {@link PdxInstance}.
* @see org.apache.geode.pdx.PdxInstance
* @see #getDelegate()
*/
public static PdxInstance unwrap(PdxInstance pdxInstance) {
return pdxInstance instanceof PdxInstanceWrapper ? ((PdxInstanceWrapper) pdxInstance).getDelegate() : pdxInstance;
}
19
Source : PdxInstanceWrapper.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
/**
* Calls {@link JSONFormatter#toJSON(PdxInstance)} to convert the {@link PdxInstance} into {@link String JSON}.
*
* @param pdxInstance {@link PdxInstance} to convert to {@link String JSON}.
* @return {@link String JSON} generated from the given {@link PdxInstance}.
* @see org.apache.geode.pdx.JSONFormatter#toJSON(PdxInstance)
* @see org.apache.geode.pdx.PdxInstance
*/
String jsonFormatterToJson(PdxInstance pdxInstance) {
return JSONFormatter.toJSON(pdxInstance);
}
19
Source : PdxInstanceWrapper.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
/**
* Factory method used to construct a new instance of {@link PdxInstanceWrapper} initialized with the given,
* required {@link PdxInstance} used to back the wrapper.
*
* @param pdxInstance {@link PdxInstance} object used to back this wrapper; must not be {@literal null}.
* @return a new instance of {@link PdxInstanceWrapper} initialized with the given {@link PdxInstance}.
* @throws IllegalArgumentException if {@link PdxInstance} is {@literal null}.
* @see org.apache.geode.pdx.PdxInstance
* @see #PdxInstanceWrapper(PdxInstance)
*/
public static PdxInstanceWrapper from(PdxInstance pdxInstance) {
return pdxInstance instanceof PdxInstanceWrapper ? (PdxInstanceWrapper) pdxInstance : new PdxInstanceWrapper(pdxInstance);
}
19
Source : PdxInstanceWrapper.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
private String toString(PdxInstance pdx) {
return toString(pdx, "");
}
19
Source : JsonLoader.java
with Apache License 2.0
from polypheny
with Apache License 2.0
from polypheny
void loadMapList(List<Map<String, Object>> mapList) {
int key = 0;
for (Map<String, Object> jsonMap : mapList) {
PdxInstance pdxInstance = mapToPdx(rootPackage, jsonMap);
region.put(key++, pdxInstance);
}
}
18
Source : PdxInstanceWrapperRegionAspectUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void regionGetAdviceWrapsPdx() throws Throwable {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
ProceedingJoinPoint mockJointPoint = mock(ProceedingJoinPoint.clreplaced);
doReturn(mockPdxInstance).when(mockJointPoint).proceed();
Object pdx = this.aspect.regionGetAdvice(mockJointPoint);
replacedertThat(pdx).isInstanceOf(PdxInstanceWrapper.clreplaced);
replacedertThat(((PdxInstanceWrapper) pdx).getDelegate()).isEqualTo(mockPdxInstance);
verify(mockJointPoint, times(1)).proceed();
verifyNoInteractions(mockPdxInstance);
}
18
Source : PdxInstanceWrapperRegionAspectUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void regionSelectValueAdviceWrapsPdx() throws Throwable {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
ProceedingJoinPoint mockJointPoint = mock(ProceedingJoinPoint.clreplaced);
doReturn(mockPdxInstance).when(mockJointPoint).proceed();
Object pdx = this.aspect.regionSelectValueAdvice(mockJointPoint);
replacedertThat(pdx).isInstanceOf(PdxInstanceWrapper.clreplaced);
replacedertThat(((PdxInstanceWrapper) pdx).getDelegate()).isEqualTo(mockPdxInstance);
verify(mockJointPoint, times(1)).proceed();
verifyNoInteractions(mockPdxInstance);
}
18
Source : ObjectPdxInstanceAdapterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void unwrapPdxInstanceReturnsPdxInstance() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
replacedertThat(ObjectPdxInstanceAdapter.unwrap(mockPdxInstance)).isSameAs(mockPdxInstance);
}
18
Source : JsonCacheDataImporterExporterIntegrationTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
// APACHE GEODE BUG 3!!!
@Test(expected = PdxSerializationException.clreplaced)
public void geodePdxInstanceObjectMapperCannotDeserializeJava8Types() {
try {
Cache peerCache = newApplicationContext(TestGeodeConfiguration.clreplaced).getBean(Cache.clreplaced);
ObjectMapper objectMapper = newObjectMapper();
TimedType value = TimedType.create().with(LocalDate.now());
ObjectNode objectNode = objectMapper.valueToTree(value);
objectNode.put("@type", value.getClreplaced().getName());
String json = objectNode.toString();
PdxInstance pdx = JSONFormatter.fromJSON(json);
// BOOM!
pdx.getObject();
} catch (PdxSerializationException expected) {
// Caused because the PdxInstance ObjectMapper is not properly configured (to findAndRegisterModules()
// or Jackson Module Extensions on the clreplacedpath)!
replacedertThat(expected).hasMessageStartingWith("Could not deserialize as java clreplaced '%s'", TimedType.clreplaced.getName());
replacedertThat(expected.getCause()).isInstanceOf(InvalidDefinitionException.clreplaced);
replacedertThat(expected.getCause()).hasMessageStartingWith("Java 8 date/time type `java.time.LocalDate` not supported by default:" + " add Module \"com.fasterxml.jackson.datatype:jackson-datatype-jsr310\" to enable handling");
replacedertThat(expected.getCause()).hasNoCause();
throw expected;
}
}
18
Source : JsonCacheDataImporterExporterIntegrationTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void exportImportWithRegionContainingObjectsAndPdxInstances() throws IOException {
// EXPORT
StringWriter writer = new StringWriter();
exportEnabledSupplier = () -> true;
importEnabledSupplier = () -> false;
resourceWriterSupplier = () -> writer;
Region<Object, Object> example = getExampleRegion(newApplicationContext(TestGeodeConfiguration.clreplaced));
replacedertExampleRegion(example);
replacedertThat(example).isEmpty();
Customer jonDoe = Customer.newCustomer(1L, "Jon Doe");
Customer janeDoe = Customer.newCustomer(2L, "JaneDoe");
PdxInstance janeDoePdx = serializeToPdx(example.getRegionService(), janeDoe);
replacedertThat(example.put(jonDoe.getId(), jonDoe)).isNull();
replacedertThat(example.put(janeDoe.getId(), janeDoePdx)).isNull();
replacedertThat(example).hreplacedize(2);
replacedertThat(example.get(jonDoe.getId())).isEqualTo(jonDoe);
replacedertThat(example.get(janeDoe.getId())).isInstanceOf(PdxInstance.clreplaced);
closeApplicationContext();
String json = writer.toString();
replacedertThat(json).isNotEmpty();
// IMPORT
Resource mockResource = mock(Resource.clreplaced, withSettings().lenient());
doReturn("MOCK").when(mockResource).getDescription();
doReturn(new ByteArrayInputStream(json.getBytes())).when(mockResource).getInputStream();
exportEnabledSupplier = () -> false;
importEnabledSupplier = () -> true;
importResourceResolverSupplier = () -> region -> Optional.of(mockResource);
example = getExampleRegion(newApplicationContext(TestGeodeConfiguration.clreplaced, TestImportResourceResolverGeodeConfiguration.clreplaced));
replacedertExampleRegion(example);
replacedertThat(example).hreplacedize(2);
for (Customer doe : Arrays.asList(jonDoe, janeDoe)) {
Object value = example.get(doe.getId().byteValue());
replacedertThat(value).isInstanceOf(PdxInstance.clreplaced);
replacedertThat(((PdxInstance) value).getObject()).isEqualTo(doe);
}
}
18
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void decorateJsonIsUnnecessary() {
String json = "{ \"name\": \"Jon Doe\" }";
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(true).when(mockPdxInstance).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
JSONFormatterPdxToJsonConverter converter = spy(new JSONFormatterPdxToJsonConverter());
replacedertThat(converter.decorate(mockPdxInstance, json)).isEqualTo(json);
verify(mockPdxInstance, times(1)).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verify(converter, never()).newObjectMapper(any());
verifyNoMoreInteractions(mockPdxInstance);
}
18
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void decorateJsonForReal() {
String json = "{ \"name\": \"Jon Doe\" }";
String expectedJson = String.format("{\n \"name\" : \"Jon Doe\",\n \"%s\" : \"%s\"\n}", JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME, Customer.clreplaced.getName());
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(Customer.clreplaced.getName()).when(mockPdxInstance).getClreplacedName();
doReturn(false).when(mockPdxInstance).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
JSONFormatterPdxToJsonConverter converter = new JSONFormatterPdxToJsonConverter();
replacedertThat(converter.decorate(mockPdxInstance, json)).isEqualTo(expectedJson);
verify(mockPdxInstance, times(1)).getClreplacedName();
verify(mockPdxInstance, times(1)).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verifyNoMoreInteractions(mockPdxInstance);
}
18
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void decoratePdxInstance() {
PdxInstance mockPdxInstanceSource = mock(PdxInstance.clreplaced);
PdxInstance mockPdxInstanceTarget = mock(PdxInstance.clreplaced);
PdxInstanceBuilder mockPdxInstanceBuilder = mock(PdxInstanceBuilder.clreplaced);
PdxInstanceFactory mockPdxInstanceFactory = mock(PdxInstanceFactory.clreplaced);
doReturn(false).when(mockPdxInstanceSource).hasField(anyString());
doReturn("example.app.test.model.Type").when(mockPdxInstanceSource).getClreplacedName();
doReturn(mockPdxInstanceFactory).when(mockPdxInstanceBuilder).copy(eq(mockPdxInstanceSource));
doReturn(mockPdxInstanceFactory).when(mockPdxInstanceFactory).writeString(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME), eq("example.app.test.model.Type"));
doReturn(mockPdxInstanceTarget).when(mockPdxInstanceFactory).create();
JSONFormatterPdxToJsonConverter converter = spy(new JSONFormatterPdxToJsonConverter());
doReturn(mockPdxInstanceBuilder).when(converter).newPdxInstanceBuilder();
replacedertThat(converter.decorate(mockPdxInstanceSource)).isEqualTo(mockPdxInstanceTarget);
verify(mockPdxInstanceSource, times(1)).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verify(mockPdxInstanceSource, times(2)).getClreplacedName();
verify(converter, times(1)).newPdxInstanceBuilder();
verify(mockPdxInstanceBuilder, times(1)).copy(eq(mockPdxInstanceSource));
verify(mockPdxInstanceFactory, times(1)).writeString(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME), eq("example.app.test.model.Type"));
verify(mockPdxInstanceFactory, times(1)).create();
verifyNoInteractions(mockPdxInstanceTarget);
}
18
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void hasValidClreplacedNameWithPdxInstanceHavingNonExistingClreplacedType() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn("non.existing.clreplaced.Type").when(mockPdxInstance).getClreplacedName();
replacedertThat(new JSONFormatterPdxToJsonConverter().hasValidClreplacedName(mockPdxInstance)).isTrue();
verify(mockPdxInstance, times(1)).getClreplacedName();
verifyNoMoreInteractions(mockPdxInstance);
}
18
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void convertsPdxToJson() {
String json = "{ \"name\": \"Jon Doe\" }";
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
JSONFormatterPdxToJsonConverter converter = spy(new JSONFormatterPdxToJsonConverter());
doReturn(json).when(converter).convertPdxToJson(eq(mockPdxInstance));
replacedertThat(converter.convert(mockPdxInstance)).isEqualTo(json);
verify(converter, times(1)).convertPdxToJson(eq(mockPdxInstance));
verify(converter, never()).convertPojoToJson(any());
verifyNoInteractions(mockPdxInstance);
}
18
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void convertPdxToJsonCallsDecorateAndJsonFormatterToJson() {
String json = "{ \"name\": \"Jon Doe\" }";
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
JSONFormatterPdxToJsonConverter converter = spy(new JSONFormatterPdxToJsonConverter());
doReturn(json).when(converter).jsonFormatterToJson(eq(mockPdxInstance));
doReturn(json).when(converter).decorate(eq(mockPdxInstance), eq(json));
replacedertThat(converter.convertPdxToJson(mockPdxInstance)).isEqualTo(json);
verify(converter, times(1)).jsonFormatterToJson(eq(mockPdxInstance));
verify(converter, times(1)).decorate(eq(mockPdxInstance), eq(json));
verifyNoInteractions(mockPdxInstance);
}
18
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
@SuppressWarnings("all")
public void decorateJson() throws JsonProcessingException {
String sourceJson = "{ \"name\": \"Jon Doe\" }";
String targetJson = String.format("{ \"%s\": \"%s\"\"name\": \"Jon Doe\" }", JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME, Customer.clreplaced.getName());
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
ObjectMapper mockObjectMapper = mock(ObjectMapper.clreplaced);
JsonNode mockJsonNode = mock(ObjectNode.clreplaced);
JSONFormatterPdxToJsonConverter converter = spy(JSONFormatterPdxToJsonConverter.clreplaced);
doReturn(Customer.clreplaced.getName()).when(mockPdxInstance).getClreplacedName();
doReturn(false).when(mockPdxInstance).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
doReturn(mockObjectMapper).when(converter).newObjectMapper(eq(sourceJson));
doReturn(mockJsonNode).when(mockObjectMapper).readTree(eq(sourceJson));
doReturn(false).when(mockJsonNode).has(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
doReturn(targetJson).when(mockObjectMapper).writeValuereplacedtring(eq(mockJsonNode));
replacedertThat(converter.decorate(mockPdxInstance, sourceJson)).isEqualTo(targetJson);
verify(mockPdxInstance, times(1)).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verify(converter, times(1)).newObjectMapper(eq(sourceJson));
verify(mockObjectMapper, times(1)).readTree(eq(sourceJson));
verify(mockJsonNode, times(1)).has(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verify(mockPdxInstance, times(1)).getClreplacedName();
verify((ObjectNode) mockJsonNode, times(1)).put(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME), eq(Customer.clreplaced.getName()));
verify(mockObjectMapper, times(1)).writeValuereplacedtring(eq(mockJsonNode));
}
18
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void decorateJsonNodeIsUnnecessary() throws JsonProcessingException {
String json = "{ \"name\": \"Jon Doe\" }";
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
ObjectMapper mockObjectMapper = mock(ObjectMapper.clreplaced);
JsonNode mockJsonNode = mock(ObjectNode.clreplaced);
JSONFormatterPdxToJsonConverter converter = spy(new JSONFormatterPdxToJsonConverter());
doReturn(false).when(mockPdxInstance).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
doReturn(mockObjectMapper).when(converter).newObjectMapper(eq(json));
doReturn(mockJsonNode).when(mockObjectMapper).readTree(eq(json));
doReturn(true).when(mockJsonNode).has(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
replacedertThat(converter.decorate(mockPdxInstance, json)).isEqualTo(json);
verify(mockPdxInstance, times(1)).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verify(converter, times(1)).newObjectMapper(eq(json));
verify(mockObjectMapper, times(1)).readTree(eq(json));
verify(mockJsonNode, times(1)).has(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verifyNoMoreInteractions(mockPdxInstance, mockObjectMapper, mockJsonNode);
}
18
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void hasValidClreplacedNameWithPdxInstanceHavingValidClreplacedName() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(Customer.clreplaced.getName()).when(mockPdxInstance).getClreplacedName();
replacedertThat(new JSONFormatterPdxToJsonConverter().hasValidClreplacedName(mockPdxInstance)).isTrue();
verify(mockPdxInstance, times(1)).getClreplacedName();
verifyNoMoreInteractions(mockPdxInstance);
}
18
Source : JSONFormatterPdxToJsonConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void decoratePdxInstanceIsUnnecessary() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(true).when(mockPdxInstance).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
JSONFormatterPdxToJsonConverter converter = new JSONFormatterPdxToJsonConverter();
replacedertThat(converter.decorate(mockPdxInstance)).isSameAs(mockPdxInstance);
verify(mockPdxInstance, times(1)).hasField(eq(JSONFormatterPdxToJsonConverter.AT_TYPE_METADATA_PROPERTY_NAME));
verifyNoMoreInteractions(mockPdxInstance);
}
18
Source : JSONFormatterJsonToPdxConverterUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void convertCallsConvertJsonToPdx() {
String json = "{ \"name\": \"Jon Doe\" }";
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
JSONFormatterJsonToPdxConverter converter = spy(new JSONFormatterJsonToPdxConverter());
doReturn(mockPdxInstance).when(converter).jsonFormatterFromJson(json);
PdxInstance pdx = converter.convert(json);
replacedertThat(pdx).isInstanceOf(PdxInstanceWrapper.clreplaced);
replacedertThat(((PdxInstanceWrapper) pdx).getDelegate()).isEqualTo(mockPdxInstance);
verify(converter, times(1)).convertJsonToPdx(eq(json));
verify(converter, times(1)).jsonFormatterFromJson(eq(json));
verify(converter, times(1)).wrap(eq(mockPdxInstance));
}
18
Source : ObjectUtilsUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void asPdxInstanceType() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(new B()).when(mockPdxInstance).getObject();
replacedertThat(ObjectUtils.asType(mockPdxInstance, B.clreplaced)).isInstanceOf(B.clreplaced);
verify(mockPdxInstance, times(1)).getObject();
}
18
Source : ObjectUtilsUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void asPdxInstanceTypeReturningNull() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(null).when(mockPdxInstance).getObject();
replacedertThat(ObjectUtils.asType(mockPdxInstance, A.clreplaced)).isNull();
verify(mockPdxInstance, times(1)).getObject();
}
18
Source : ObjectUtilsUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test(expected = IllegalArgumentException.clreplaced)
public void asPdxInstanceTypeReturningNonMatchingType() {
C source = new C();
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(source).when(mockPdxInstance).getObject();
try {
ObjectUtils.asType(mockPdxInstance, A.clreplaced);
} catch (IllegalArgumentException expected) {
replacedertThat(expected).hasMessage("Object [%s] is not an instance of type [%s]", source.getClreplaced().getName(), A.clreplaced.getName());
replacedertThat(expected).hasNoCause();
throw expected;
} finally {
verify(mockPdxInstance, times(1)).getObject();
}
}
18
Source : JsonCacheDataImporterExporter.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
/**
* The {@link JsonCacheDataImporterExporter} clreplaced is a {@link CacheDataImporter} and {@link CacheDataExporter}
* implementation that can export/import JSON data to/from a {@link Resource} given a target {@link Region}.
*
* @author John Blum
* @see org.apache.geode.cache.Region
* @see org.apache.geode.pdx.PdxInstance
* @see org.springframework.core.io.Resource
* @see org.springframework.geode.data.CacheDataExporter
* @see org.springframework.geode.data.CacheDataImporter
* @see org.springframework.geode.data.json.converter.JsonToPdxArrayConverter
* @see org.springframework.geode.data.json.converter.ObjectArrayToJsonConverter
* @see org.springframework.geode.data.json.converter.support.JacksonJsonToPdxConverter
* @see org.springframework.geode.data.support.ResourceCapableCacheDataImporterExporter
* @see org.springframework.geode.pdx.ObjectPdxInstanceAdapter
* @see org.springframework.geode.pdx.PdxInstanceWrapper
* @see org.springframework.stereotype.Component
* @since 1.3.0
*/
@Component
@SuppressWarnings("rawtypes")
public clreplaced JsonCacheDataImporterExporter extends ResourceCapableCacheDataImporterExporter {
protected static final PdxInstance[] EMPTY_PDX_INSTANCE_ARRAY = {};
@Autowired(required = false)
private JsonToPdxArrayConverter jsonToPdxArrayConverter;
private final RegionValuesToJsonConverter regionValuesToJsonConverter = new RegionValuesToJsonConverter();
/**
* Determines whether the given array is empty or not. An array is not empty if the array reference
* is not {@literal null} and contains at least 1 element.
*
* @param <T> {@link Clreplaced type} of the array elements.
* @param array {@link Object} array to evaluate.
* @return a boolean value indicating whether the array is empty or not.
*/
@SuppressWarnings("unchecked")
private static <T> boolean isNotEmpty(T... array) {
return array != null && array.length > 0;
}
/**
* Initializes the JSON to PDX (array) converter.
*
* @see #newJsonToPdxArrayConverter()
*/
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
this.jsonToPdxArrayConverter = this.jsonToPdxArrayConverter != null ? this.jsonToPdxArrayConverter : newJsonToPdxArrayConverter();
}
@NonNull
private JsonToPdxArrayConverter newJsonToPdxArrayConverter() {
return new JacksonJsonToPdxConverter();
}
/**
* Returns a reference to the configured {@link JsonToPdxArrayConverter}.
*
* @return a reference to the configured {@link JsonToPdxArrayConverter}.
* @see org.springframework.geode.data.json.converter.JsonToPdxArrayConverter
*/
@NonNull
protected JsonToPdxArrayConverter getJsonToPdxArrayConverter() {
return this.jsonToPdxArrayConverter;
}
/**
* @inheritDoc
*/
@NonNull
@Override
public Region doExportFrom(@NonNull Region region) {
replacedert.notNull(region, "Region must not be null");
getExportResourceResolver().resolve(region).ifPresent(resource -> {
String json = toJson(region);
getLogger().debug("Saving JSON [{}] from Region [{}]", json, region.getName());
getResourceWriter().write(resource, json.getBytes());
});
return region;
}
/**
* @inheritDoc
*/
@NonNull
@Override
public Region doImportInto(@NonNull Region region) {
replacedert.notNull(region, "Region must not be null");
getImportResourceResolver().resolve(region).map(this.getResourceReader()::read).map(this::toPdx).ifPresent(pdxInstances -> regionPutPdx(region, pdxInstances));
return region;
}
/**
* Puts all PDX data from the {@link PdxInstance} array into the target {@link Region} mapped to
* the PDX {@link PdxInstance#isIdenreplacedyField(String) identifier} as the {@literal key}.
*
* @param region target {@link Region} to store the PDX data; must not be {@literal null}
* @param pdx {@link PdxInstance} array containing the PDX data to store in the target {@link Region}.
* @see org.apache.geode.cache.Region
* @see org.apache.geode.cache.Region#put(Object, Object)
* @see org.apache.geode.pdx.PdxInstance
*/
@SuppressWarnings("unchecked")
void regionPutPdx(@NonNull Region region, @Nullable PdxInstance[] pdx) {
Arrays.stream(ArrayUtils.nullSafeArray(pdx, PdxInstance.clreplaced)).forEach(pdxInstance -> region.put(resolveKey(pdxInstance), resolveValue(pdxInstance)));
}
/**
* Post processes the given {{@link PdxInstance}.
*
* @param pdxInstance {@link PdxInstance} to process.
* @return the {@link PdxInstance}.
* @see org.apache.geode.pdx.PdxInstance
*/
protected PdxInstance postProcess(PdxInstance pdxInstance) {
return pdxInstance;
}
/**
* Resolves the {@link Object key} used to map the given {@link PdxInstance} as the {@link Object value}
* for the {@code Region.Entry} stored in the {@link Region}.
*
* @param pdxInstance {@link PdxInstance} used to resolve the {@link Object key}.
* @return the resolved {@link Object key}.
* @see org.springframework.geode.pdx.PdxInstanceWrapper#getIdentifier()
* @see org.apache.geode.pdx.PdxInstance
*/
@NonNull
protected Object resolveKey(@NonNull PdxInstance pdxInstance) {
return PdxInstanceWrapper.from(pdxInstance).getIdentifier();
}
/**
* Resolves the {@link Object value} to store in the {@link Region} from the given {@link PdxInstance}.
*
* If the given {@link PdxInstance} is an instance of {@link PdxInstanceWrapper} then this method will return
* the underlying, {@link PdxInstanceWrapper#getDelegate() delegate} {@link PdxInstance}.
*
* If the given {@link PdxInstance} is an instance of {@link ObjectPdxInstanceAdapter} then this method will return
* the underlying, {@link ObjectPdxInstanceAdapter#getObject() Object}.
*
* Otherwise, the given {@link PdxInstance} is returned.
*
* @param pdxInstance {@link PdxInstance} to unwrap.
* @return the resolved {@link Object value}.
* @see org.springframework.geode.pdx.ObjectPdxInstanceAdapter#unwrap(PdxInstance)
* @see org.springframework.geode.pdx.PdxInstanceWrapper#unwrap(PdxInstance)
* @see org.apache.geode.pdx.PdxInstance
* @see #postProcess(PdxInstance)
*/
@Nullable
protected Object resolveValue(@Nullable PdxInstance pdxInstance) {
return ObjectPdxInstanceAdapter.unwrap(PdxInstanceWrapper.unwrap(postProcess(pdxInstance)));
}
/**
* Convert {@link Object values} contained in the {@link Region} to {@link String JSON}.
*
* @param region {@link Region} to process; must not be {@literal null}.
* @return {@link String JSON} containing the {@link Object values} from the given {@link Region}.
* @see org.apache.geode.cache.Region
*/
@SuppressWarnings("unchecked")
@NonNull
protected String toJson(@NonNull Region region) {
return this.regionValuesToJsonConverter.convert(region);
}
/**
* Converts the array of {@link Byte#TYPE bytes} containing multiple {@link String JSON} objects
* into an array of {@link PdxInstance PdxInstances}.
*
* @param json array of {@link Byte#TYPE bytes} containing the {@link String JSON} to convert to PDX.
* @return an array of {@link PdxInstance PdxInstances} for each {@link String JSON} object.
* @see org.apache.geode.pdx.PdxInstance
* @see #getJsonToPdxArrayConverter()
*/
@NonNull
protected PdxInstance[] toPdx(@NonNull byte[] json) {
return isNotEmpty(json) ? getJsonToPdxArrayConverter().convert(json) : EMPTY_PDX_INSTANCE_ARRAY;
}
/**
* Converts all {@link Region#values() values} in the targeted {@link Region} into {@literal JSON}.
*
* The converter is capable of handling both {@link Object Objects} and PDX.
*
* @see org.springframework.geode.data.json.converter.AbstractObjectArrayToJsonConverter
*/
static clreplaced RegionValuesToJsonConverter extends AbstractObjectArrayToJsonConverter {
@NonNull
<K, V> String convert(@NonNull Region<K, V> region) {
replacedert.notNull(region, "Region must not be null");
return super.convert(CollectionUtils.nullSafeCollection(CacheUtils.collectValues(region)));
}
}
}
18
Source : JSONFormatterPdxToJsonConverter.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
/**
* Converts the given {@link PdxInstance PDX} to {@link String JSON}.
*
* @param pdxInstance {@link PdxInstance} to convert to JSON; must not be {@literal null}.
* @return JSON generated from the given {@link PdxInstance}.
* @see org.apache.geode.pdx.JSONFormatter#toJSON(PdxInstance)
* @see org.apache.geode.pdx.PdxInstance
* @see #jsonFormatterToJson(PdxInstance)
*/
@NonNull
protected String convertPdxToJson(@NonNull PdxInstance pdxInstance) {
return decorate(pdxInstance, jsonFormatterToJson(pdxInstance));
}
18
Source : JSONFormatterPdxToJsonConverter.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
/**
* Converts {@link PdxInstance PDX} into {@link String JSON} using {@link JSONFormatter#toJSON(PdxInstance)}.
*
* @param pdxInstance {@link PdxInstance PDX} to convert to {@link String JSON}; must not be {@literal null}.
* @return {@link String JSON} generated from the given, required {@link PdxInstance PDX}; never {@literal null}.
* @see org.apache.geode.pdx.JSONFormatter#toJSON(PdxInstance)
* @see org.apache.geode.pdx.PdxInstance
*/
@NonNull
String jsonFormatterToJson(@NonNull PdxInstance pdxInstance) {
return JSONFormatter.toJSON(pdxInstance);
}
18
Source : JSONFormatterPdxToJsonConverter.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
/**
* Null-safe method to determine whether the given {@link PdxInstance}
* has a valid {@link Clreplaced#getName() Clreplaced Name}.
*
* @param pdxInstance {@link PdxInstance} to evaluate;
* @return a boolean value indicating whether the {@link PdxInstance}
* has a valid {@link Clreplaced#getName() Clreplaced Name}.
* @see org.apache.geode.pdx.PdxInstance
*/
boolean hasValidClreplacedName(@Nullable PdxInstance pdxInstance) {
return Optional.ofNullable(pdxInstance).map(PdxInstance::getClreplacedName).filter(StringUtils::hasText).filter(clreplacedName -> !JSONFormatter.JSON_CLreplacedNAME.equals(clreplacedName)).isPresent();
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void getIdFromPdxInstanceWithNoIdFieldCallsGetAtIdentifier() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
PdxInstanceWrapper wrapper = spy(new PdxInstanceWrapper(mockPdxInstance));
doReturn(false).when(mockPdxInstance).hasField(any());
doReturn(99).when(wrapper).getAtIdentifier();
replacedertThat(wrapper.getId()).isEqualTo(99);
verify(mockPdxInstance, times(1)).hasField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
verify(mockPdxInstance, never()).getField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
verify(wrapper, times(1)).getAtIdentifier();
verifyNoMoreInteractions(mockPdxInstance);
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void getObjectCallsPdxInstanceGetObjectWhenObjectMapperIsNotPresent() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn("non.existing.clreplaced.Name").when(mockPdxInstance).getClreplacedName();
doReturn("MOCK").when(mockPdxInstance).getObject();
PdxInstanceWrapper wrapper = spy(PdxInstanceWrapper.from(mockPdxInstance));
replacedertThat(wrapper).isNotNull();
replacedertThat(wrapper.getDelegate()).isEqualTo(mockPdxInstance);
doReturn(Optional.empty()).when(wrapper).getObjectMapper();
replacedertThat(wrapper.getObject()).isEqualTo("MOCK");
verify(wrapper, atLeastOnce()).getDelegate();
verify(wrapper, times(1)).getObjectMapper();
verify(wrapper, never()).jsonFormatterToJson(any());
verify(mockPdxInstance, times(1)).getObject();
verifyNoMoreInteractions(mockPdxInstance);
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test(expected = IllegalStateException.clreplaced)
public void getAtIdentifierFromPdxInstanceWithNoDeclaredIdenreplacedy() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
PdxInstanceWrapper wrapper = spy(new PdxInstanceWrapper(mockPdxInstance));
doReturn(Account.clreplaced.getName()).when(mockPdxInstance).getClreplacedName();
doReturn(false).when(mockPdxInstance).hasField(any());
try {
wrapper.getAtIdentifier();
} catch (IllegalStateException expected) {
replacedertThat(expected).hasMessage("PdxInstance for type [%s] has no declared identifier", Account.clreplaced.getName());
replacedertThat(expected).hasNoCause();
throw expected;
} finally {
verify(mockPdxInstance, times(1)).getClreplacedName();
verify(mockPdxInstance, times(2)).hasField(eq(PdxInstanceWrapper.AT_IDENTIFIER_FIELD_NAME));
verify(mockPdxInstance, times(1)).hasField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
verify(mockPdxInstance, never()).getField(anyString());
verifyNoMoreInteractions(mockPdxInstance);
}
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test(expected = IllegalStateException.clreplaced)
public void getAtIdentifierFromPdxInstanceWithNoId() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
PdxInstanceWrapper wrapper = spy(new PdxInstanceWrapper(mockPdxInstance));
doReturn(Account.clreplaced.getName()).when(mockPdxInstance).getClreplacedName();
doReturn(true).when(mockPdxInstance).hasField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
try {
wrapper.getAtIdentifier();
} catch (IllegalStateException expected) {
replacedertThat(expected).hasMessage("PdxInstance for type [%s] has no id", Account.clreplaced.getName());
replacedertThat(expected).hasNoCause();
throw expected;
} finally {
verify(mockPdxInstance, times(1)).getClreplacedName();
verify(mockPdxInstance, times(1)).hasField(eq(PdxInstanceWrapper.AT_IDENTIFIER_FIELD_NAME));
verify(mockPdxInstance, times(1)).hasField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
verify(mockPdxInstance, never()).getField(any());
verifyNoMoreInteractions(mockPdxInstance);
}
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void isIdenreplacedyFieldCallsPdxInstanceIsIdenreplacedyField() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(false).when(mockPdxInstance).isIdenreplacedyField(anyString());
doReturn(true).when(mockPdxInstance).isIdenreplacedyField("id");
PdxInstanceWrapper wrapper = PdxInstanceWrapper.from(mockPdxInstance);
replacedertThat(wrapper.isIdenreplacedyField("id")).isTrue();
replacedertThat(wrapper.isIdenreplacedyField("randomField")).isFalse();
verify(mockPdxInstance, times(1)).isIdenreplacedyField(eq("id"));
verify(mockPdxInstance, times(1)).isIdenreplacedyField(eq("randomField"));
verifyNoMoreInteractions(mockPdxInstance);
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test(expected = IllegalStateException.clreplaced)
public void getIdentifierFromPdxInstanceWithNoIdentifier() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
PdxInstanceWrapper wrapper = spy(new PdxInstanceWrapper(mockPdxInstance));
doReturn(Collections.singletonList("name")).when(mockPdxInstance).getFieldNames();
doReturn(false).when(mockPdxInstance).isIdenreplacedyField(anyString());
doThrow(new IllegalStateException("NO ID")).when(wrapper).getId();
try {
wrapper.getIdentifier();
} catch (IllegalStateException expected) {
replacedertThat(expected).hasMessage("NO ID");
replacedertThat(expected).hasNoCause();
throw expected;
} finally {
verify(mockPdxInstance, times(1)).getFieldNames();
verify(mockPdxInstance, times(1)).isIdenreplacedyField(eq("name"));
verify(mockPdxInstance, never()).getField(anyString());
verify(wrapper, times(1)).getId();
verifyNoMoreInteractions(mockPdxInstance);
}
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void unwrapPdxInstanceReturnsPdxInstance() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
replacedertThat(PdxInstanceWrapper.unwrap(mockPdxInstance)).isEqualTo(mockPdxInstance);
verifyNoInteractions(mockPdxInstance);
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void fromPdxInstanceIsWrapper() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
PdxInstanceWrapper wrapper = PdxInstanceWrapper.from(mockPdxInstance);
replacedertThat(wrapper).isNotNull();
replacedertThat(wrapper.getDelegate()).isEqualTo(mockPdxInstance);
verifyNoInteractions(mockPdxInstance);
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test(expected = IllegalStateException.clreplaced)
public void getAtIdentifierFromPdxInstanceWithAtIdentifierReferringToInvalidIdentifierField() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
PdxInstanceWrapper wrapper = spy(new PdxInstanceWrapper(mockPdxInstance));
doReturn(Person.clreplaced.getName()).when(mockPdxInstance).getClreplacedName();
doReturn(true).when(mockPdxInstance).hasField(eq(PdxInstanceWrapper.AT_IDENTIFIER_FIELD_NAME));
doReturn(false).when(mockPdxInstance).hasField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
doReturn(false).when(mockPdxInstance).hasField(eq("ssn"));
doReturn("ssn").when(mockPdxInstance).getField(eq(PdxInstanceWrapper.AT_IDENTIFIER_FIELD_NAME));
try {
wrapper.getAtIdentifier();
} catch (IllegalStateException expected) {
replacedertThat(expected).hasMessage("PdxInstance for type [%s] has no field [ssn] declared in [%s]", Person.clreplaced.getName(), PdxInstanceWrapper.AT_IDENTIFIER_FIELD_NAME);
replacedertThat(expected).hasNoCause();
throw expected;
} finally {
verify(mockPdxInstance, times(1)).getClreplacedName();
verify(mockPdxInstance, times(2)).hasField(eq(PdxInstanceWrapper.AT_IDENTIFIER_FIELD_NAME));
verify(mockPdxInstance, times(1)).hasField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
verify(mockPdxInstance, times(2)).hasField(eq("ssn"));
verify(mockPdxInstance, times(2)).getField(eq(PdxInstanceWrapper.AT_IDENTIFIER_FIELD_NAME));
verify(mockPdxInstance, never()).getField(eq("ssn"));
verifyNoMoreInteractions(mockPdxInstance);
}
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void isDeserializaleCallsPdxInstanceIsDeserializable() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
doReturn(true).when(mockPdxInstance).isDeserializable();
replacedertThat(PdxInstanceWrapper.from(mockPdxInstance).isDeserializable()).isTrue();
verify(mockPdxInstance, times(1)).isDeserializable();
verifyNoMoreInteractions(mockPdxInstance);
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void unwrapPdxInstanceWrapperReturnsPdxInstanceDelegate() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
PdxInstanceWrapper wrapper = PdxInstanceWrapper.from(mockPdxInstance);
replacedertThat(wrapper).isNotNull();
replacedertThat(wrapper.getDelegate()).isEqualTo(mockPdxInstance);
replacedertThat(PdxInstanceWrapper.unwrap(wrapper)).isEqualTo(mockPdxInstance);
verifyNoInteractions(mockPdxInstance);
}
18
Source : PdxInstanceWrapperUnitTests.java
with Apache License 2.0
from spring-projects
with Apache License 2.0
from spring-projects
@Test
public void getIdFromPdxInstanceHavingIdField() {
PdxInstance mockPdxInstance = mock(PdxInstance.clreplaced);
PdxInstanceWrapper wrapper = spy(new PdxInstanceWrapper(mockPdxInstance));
doReturn(true).when(mockPdxInstance).hasField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
doReturn(42).when(mockPdxInstance).getField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
replacedertThat(wrapper.getId()).isEqualTo(42);
verify(mockPdxInstance, times(1)).hasField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
verify(mockPdxInstance, times(1)).getField(eq(PdxInstanceWrapper.ID_FIELD_NAME));
verify(wrapper, never()).getAtIdentifier();
verifyNoMoreInteractions(mockPdxInstance);
}
See More Examples