/** * Creates a protobuf (key, value) pair from unencoded data. if the value is null, it will be * unset in the BasicTypes.Entry. * * The key MUST NOT be null. * * @param serializationService - object which knows how to encode objects for the protobuf * protocol {@link ProtobufSerializationService} * @param unencodedKey - the unencoded key for the entry * @param unencodedValue - the unencoded value for the entry * @return a protobuf Entry containing the encoded key and value * @throws EncodingException - The key or value passed doesn't have a corresponding * SerializationType */ public static BasicTypes.Entry createEntry(ProtobufSerializationService serializationService, Object unencodedKey, Object unencodedValue) throws EncodingException { return createEntry(serializationService.encode(unencodedKey), serializationService.encode(unencodedValue)); }
public static ClientProtocol.Message makeGetRequestMessage( ProtobufSerializationService serializationService, Object requestKey, String requestRegion) throws Exception { return ProtobufRequestUtilities.createGetRequest(requestRegion, serializationService.encode(requestKey)); }
public static ClientProtocol.Message makePutRequestMessage( ProtobufSerializationService serializationService, String requestKey, String requestValue, String requestRegion) throws EncodingException { BasicTypes.Entry entry = ProtobufUtilities.createEntry(serializationService.encode(requestKey), serializationService.encode(requestValue)); return ProtobufRequestUtilities.createPutRequest(requestRegion, entry); }
private RegionAPI.GetAllRequest generateTestRequest(boolean addKeys, boolean useInvalid) throws EncodingException { HashSet<BasicTypes.EncodedValue> testKeys = new HashSet<>(); if (addKeys) { testKeys.add(serializationService.encode(TEST_KEY1)); testKeys.add(serializationService.encode(TEST_KEY2)); testKeys.add(serializationService.encode(TEST_KEY3)); if (useInvalid) { testKeys.add(serializationService.encode(TEST_INVALID_KEY)); } } return ProtobufRequestUtilities.createGetAllRequest(TEST_REGION, testKeys); }
private BasicTypes.EncodedValueList.Builder encodeStruct( final ProtobufSerializationService serializationService, final Struct row) throws EncodingException { BasicTypes.EncodedValueList.Builder structBuilder = BasicTypes.EncodedValueList.newBuilder(); for (Object element : row.getFieldValues()) { structBuilder.addElement(serializationService.encode(element)); } return structBuilder; }
private ClientProtocol.Message generateTestRequest(boolean missingRegion, boolean missingKey) throws EncodingException { String region = missingRegion ? MISSING_REGION : TEST_REGION; String key = missingKey ? MISSING_KEY : TEST_KEY; BasicTypes.EncodedValue testKey = serializationService.encode(key); return ProtobufRequestUtilities.createRemoveRequest(region, testKey); } }
private RegionAPI.PutAllRequest generateTestRequest(boolean addInvalidKey, boolean addValidKeys) throws EncodingException { Set<BasicTypes.Entry> entries = new HashSet<>(); if (addInvalidKey) { entries.add(ProtobufUtilities.createEntry(serializationService.encode(TEST_INVALID_KEY), serializationService.encode(TEST_INVALID_VALUE))); } if (addValidKeys) { entries.add(ProtobufUtilities.createEntry(serializationService.encode(TEST_KEY1), serializationService.encode(TEST_VALUE1))); entries.add(ProtobufUtilities.createEntry(serializationService.encode(TEST_KEY2), serializationService.encode(TEST_VALUE2))); entries.add(ProtobufUtilities.createEntry(serializationService.encode(TEST_KEY3), serializationService.encode(TEST_VALUE3))); } return ProtobufRequestUtilities.createPutAllRequest(TEST_REGION, entries).getPutAllRequest(); } }
private RegionAPI.PutRequest generateTestRequest() throws EncodingException { BasicTypes.EncodedValue testKey = serializationService.encode(TEST_KEY); BasicTypes.EncodedValue testValue = serializationService.encode(TEST_VALUE); BasicTypes.Entry testEntry = ProtobufUtilities.createEntry(testKey, testValue); return ProtobufRequestUtilities.createPutRequest(TEST_REGION, testEntry).getPutRequest(); } }
private void addException(ProtobufSerializationService serializationService, RegionAPI.GetAllResponse.Builder responseBuilder, Object key, Object exception) { logger.warn("Failure in protobuf getAll operation for key: " + key, exception); BasicTypes.EncodedValue encodedKey = serializationService.encode(key); BasicTypes.KeyedError failure = BasicTypes.KeyedError.newBuilder().setKey(encodedKey).setError( BasicTypes.Error.newBuilder().setErrorCode(SERVER_ERROR).setMessage(exception.toString())) .build(); responseBuilder.addFailures(failure); }
private RegionAPI.GetRequest generateTestRequest(boolean missingRegion, boolean missingKey, boolean nulledKey) throws EncodingException { String region = missingRegion ? MISSING_REGION : TEST_REGION; String key = missingKey ? MISSING_KEY : (nulledKey ? NULLED_KEY : TEST_KEY); BasicTypes.EncodedValue testKey = serializationService.encode(key); return ProtobufRequestUtilities.createGetRequest(region, testKey).getGetRequest(); } }
@Override public Result<RegionAPI.PutAllResponse> process(ProtobufSerializationService serializationService, RegionAPI.PutAllRequest putAllRequest, MessageExecutionContext messageExecutionContext) throws InvalidExecutionContextException, DecodingException { String regionName = putAllRequest.getRegionName(); RegionAPI.PutAllResponse.Builder builder = RegionAPI.PutAllResponse.newBuilder(); SecureCache cache = messageExecutionContext.getSecureCache(); Map<Object, Object> entries = new HashMap<>(putAllRequest.getEntryList().size()); putAllRequest.getEntryList() .forEach(entry -> entries.put(serializationService.decode(entry.getKey()), serializationService.decode(entry.getValue()))); cache.putAll(regionName, entries, (key, exception) -> addError(builder, serializationService.encode(key), exception)); return Success.of(builder.build()); }
private RegionAPI.PutIfAbsentRequest generateTestRequest(boolean includeRegion, boolean includeEntry) throws EncodingException { RegionAPI.PutIfAbsentRequest.Builder builder = RegionAPI.PutIfAbsentRequest.newBuilder(); if (includeRegion) { builder.setRegionName(TEST_REGION); } if (includeEntry) { BasicTypes.EncodedValue testKey = serializationService.encode(TEST_KEY); BasicTypes.EncodedValue testValue = serializationService.encode(TEST_VALUE); BasicTypes.Entry testEntry = ProtobufUtilities.createEntry(testKey, testValue); builder.setEntry(testEntry); } return builder.build(); }
@Test public void queryForSingleObject() throws ConnectionStateException, DecodingException, InvalidExecutionContextException, EncodingException, NameResolutionException, TypeMismatchException, QueryInvocationTargetException, FunctionDomainException { Query query = mock(DefaultQuery.class); when(queryService.newQuery(eq(SELECT_STAR_QUERY))).thenReturn(query); when(query.execute((Object[]) any())).thenReturn(STRING_RESULT_1); final OQLQueryRequest request = OQLQueryRequest.newBuilder().setQuery(SELECT_STAR_QUERY).build(); final Result<OQLQueryResponse> result = operationHandler.process(serializationService, request, TestExecutionContext.getNoAuthCacheExecutionContext(cacheStub)); assertEquals(serializationService.encode(STRING_RESULT_1), result.getMessage().getSingleResult()); }
@Test public void queryWithBindParameters() throws ConnectionStateException, DecodingException, InvalidExecutionContextException, EncodingException, NameResolutionException, TypeMismatchException, QueryInvocationTargetException, FunctionDomainException { checkResults("select ID,status from /region where ID=$1", new EncodedValue[] {new ProtobufSerializationService().encode(0)}, new String[] {"ID", "status"}, new Object[] {0, "active"}); }
private void checkResults(final String query, final Object value) throws InvalidExecutionContextException, ConnectionStateException, EncodingException, DecodingException { ProtobufSerializationService serializer = new ProtobufSerializationService(); final Result<OQLQueryResponse> results = invokeHandler(query, new EncodedValue[] {}, serializer); assertEquals(serializer.encode(value), results.getMessage().getSingleResult()); }
@Override public Result<RegionAPI.GetResponse> process(ProtobufSerializationService serializationService, RegionAPI.GetRequest request, MessageExecutionContext messageExecutionContext) throws InvalidExecutionContextException, EncodingException, DecodingException { String regionName = request.getRegionName(); Object decodedKey = serializationService.decode(request.getKey()); if (decodedKey == null) { return Failure.of(BasicTypes.ErrorCode.INVALID_REQUEST, "Performing a get on a NULL key."); } SecureCache secureCache = messageExecutionContext.getSecureCache(); Object resultValue = secureCache.get(regionName, decodedKey); BasicTypes.EncodedValue encodedValue = serializationService.encode(resultValue); return Success.of(RegionAPI.GetResponse.newBuilder().setResult(encodedValue).build()); } }
@Test(expected = DecodingException.class) public void processThrowsExceptionWhenUnableToDecode() throws Exception { String exceptionText = "unsupported type!"; Exception exception = new DecodingException(exceptionText); ProtobufSerializationService serializationServiceStub = mock(ProtobufSerializationService.class); when(serializationServiceStub.decode(any())).thenThrow(exception); BasicTypes.EncodedValue encodedKey = BasicTypes.EncodedValue.newBuilder() .setJsonObjectResult("{\"someKey\":\"someValue\"}").build(); PutRequestOperationHandler operationHandler = new PutRequestOperationHandler(); BasicTypes.EncodedValue testValue = serializationService.encode(TEST_VALUE); BasicTypes.Entry testEntry = ProtobufUtilities.createEntry(encodedKey, testValue); RegionAPI.PutRequest putRequest = ProtobufRequestUtilities.createPutRequest(TEST_REGION, testEntry).getPutRequest(); operationHandler.process(serializationServiceStub, putRequest, TestExecutionContext.getNoAuthCacheExecutionContext(cacheStub)); }
@Override public Result<RegionAPI.PutIfAbsentResponse> process( ProtobufSerializationService serializationService, RegionAPI.PutIfAbsentRequest request, MessageExecutionContext messageExecutionContext) throws InvalidExecutionContextException, ConnectionStateException, EncodingException, DecodingException { final String regionName = request.getRegionName(); final BasicTypes.Entry entry = request.getEntry(); Object decodedValue = serializationService.decode(entry.getValue()); Object decodedKey = serializationService.decode(entry.getKey()); final Object oldValue = messageExecutionContext.getSecureCache().putIfAbsent(regionName, decodedKey, decodedValue); return Success.of(RegionAPI.PutIfAbsentResponse.newBuilder() .setOldValue(serializationService.encode(oldValue)).build()); } }
private <T> void createAndVerifyEncodedValue(T testObj) throws EncodingException { BasicTypes.EncodedValue encodedValue = protobufSerializationService.encode(testObj); if (testObj instanceof byte[]) { try { assertArrayEquals((byte[]) testObj, (byte[]) protobufSerializationService.decode(encodedValue)); } catch (org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.DecodingException e) { e.printStackTrace(); } } else { try { assertEquals(testObj, protobufSerializationService.decode(encodedValue)); } catch (org.apache.geode.internal.protocol.protobuf.v1.serialization.exception.DecodingException e) { e.printStackTrace(); } } } }
@Test public void singleNullKey() throws Exception { HashSet<BasicTypes.EncodedValue> testKeys = new HashSet<>(); testKeys.add(serializationService.encode(NO_VALUE_PRESENT_FOR_THIS_KEY)); RegionAPI.GetAllRequest getAllRequest = ProtobufRequestUtilities.createGetAllRequest(TEST_REGION, testKeys); Result result = operationHandler.process(serializationService, getAllRequest, TestExecutionContext.getNoAuthCacheExecutionContext(cacheStub)); assertTrue(result instanceof Success); RegionAPI.GetAllResponse message = (RegionAPI.GetAllResponse) result.getMessage(); assertEquals(1, message.getEntriesCount()); assertEquals(null, serializationService.decode(message.getEntries(0).getValue())); assertEquals(NO_VALUE_PRESENT_FOR_THIS_KEY, message.getEntries(0).getKey().getStringResult()); verify(regionStub, times(1)).get(NO_VALUE_PRESENT_FOR_THIS_KEY); }