GEODE-3129 - Added error messages to protobuf protocol. This now closes #621
added a new ErrorResponse type to ClientProtocol removed success field from several RegionAPI response objects and refactored operation handlers to instead return ErrorResponses made all op handlers take ClientProtocol.Requests and return ClientProtocol.Responses instead of operation specific types moved the protobuf specific response building code from operation handlers to ProtobufResponseUtilities moved the request building functions from MessageUtil (under Test) to ProtobufRequestUtilities moved all utility classes to ...protocol.protobuf.utilities and added javadoc comments throughout changed GetRegions to GetRegionNames, returns strings instead of Regions replaced logging through the cache's LogWriter with log4j logging updated all imports to be in the correct order for the new geode style guide Signed-off-by: Brian Rowe <br...@pivotal.io> Signed-off-by: Hitesh Khamesra <hitesh...@yahoo.com> Signed-off-by: Brian Rowe <br...@pivotal.io> Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/0ce62710 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/0ce62710 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/0ce62710 Branch: refs/heads/feature/GEODE-3109 Commit: 0ce62710d7b85e24ea57f11b9064a774735f182f Parents: 9d5cf8e Author: Brian Rowe <br...@pivotal.io> Authored: Mon Jul 3 16:01:56 2017 -0700 Committer: Udo Kohlmeyer <ukohlme...@pivotal.io> Committed: Fri Jul 7 13:29:44 2017 -0700 ---------------------------------------------------------------------- .../registry/OperationsHandlerRegistry.java | 1 - .../protobuf/EncodingTypeTranslator.java | 1 - .../protocol/protobuf/ProtobufOpsProcessor.java | 41 +----- .../protobuf/ProtobufSerializationService.java | 3 + .../protobuf/ProtobufStreamProcessor.java | 18 +-- .../protocol/protobuf/ProtobufUtilities.java | 45 ------- .../GetRegionNamesRequestOperationHandler.java | 34 +++++ .../GetRegionsRequestOperationHandler.java | 48 ------- .../operations/GetRequestOperationHandler.java | 72 ++++------ .../operations/PutRequestOperationHandler.java | 62 +++++---- .../serializer/ProtobufProtocolSerializer.java | 2 +- .../utilities/ProtobufRequestUtilities.java | 62 +++++++++ .../utilities/ProtobufResponseUtilities.java | 116 ++++++++++++++++ .../protobuf/utilities/ProtobufUtilities.java | 135 +++++++++++++++++++ .../geode/serialization/codec/IntCodec.java | 1 - .../src/main/proto/clientProtocol.proto | 12 +- geode-protobuf/src/main/proto/region_API.proto | 13 +- .../geode/protocol/IntegrationJUnitTest.java | 16 +-- .../org/apache/geode/protocol/MessageUtil.java | 86 +++--------- .../RoundTripCacheConnectionJUnitTest.java | 86 +++++++++--- .../OperationsHandlerRegistryJUnitTest.java | 8 +- ...eToSerializationTypeTranslatorJUnitTest.java | 6 +- .../protobuf/ProtobufOpsProcessorJUnitTest.java | 16 +-- ...onNamesRequestOperationHandlerJUnitTest.java | 109 +++++++++++++++ ...tRegionRequestOperationHandlerJUnitTest.java | 109 --------------- .../GetRequestOperationHandlerJUnitTest.java | 76 ++++++----- .../PutRequestOperationHandlerJUnitTest.java | 68 +++++----- .../ProtobufProtocolSerializerJUnitTest.java | 2 +- .../codec/BinaryFormatJUnitTest.java | 6 +- 29 files changed, 738 insertions(+), 516 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java index 2b9f525..b2021af 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java @@ -19,7 +19,6 @@ import org.apache.geode.protocol.operations.registry.exception.OperationHandlerA import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; import java.util.HashMap; -import java.util.ServiceLoader; /** * This class tracks which operation handlers are expected to handle which types of operations. http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java index edb7c7e..da43f4b 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java @@ -16,7 +16,6 @@ package org.apache.geode.protocol.protobuf; import org.apache.geode.pdx.JSONFormatter; import org.apache.geode.pdx.PdxInstance; -import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.serialization.SerializationType; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java index 4318fb4..13f2b00 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java @@ -41,45 +41,6 @@ public class ProtobufOpsProcessor { OperationHandler opsHandler = opsHandlerRegistry.getOperationHandlerForOperationId(requestType.getNumber()); - Object responseMessage = - opsHandler.process(serializationService, getRequestForOperationTypeID(request), cache); - return wrapResponseForOperationTypeID(requestType, responseMessage); - } - - // package visibility for testing - static Object getRequestForOperationTypeID(ClientProtocol.Request request) - throws InvalidProtocolMessageException { - switch (request.getRequestAPICase()) { - case PUTREQUEST: - return request.getPutRequest(); - case GETREQUEST: - return request.getGetRequest(); - case PUTALLREQUEST: - return request.getPutAllRequest(); - case GETREGIONSREQUEST: - return request.getGetRegionsRequest(); - default: - throw new InvalidProtocolMessageException( - "Unknown request type: " + request.getRequestAPICase().getNumber()); - } - } - - static ClientProtocol.Response wrapResponseForOperationTypeID( - ClientProtocol.Request.RequestAPICase requestType, Object response) - throws InvalidProtocolMessageException { - ClientProtocol.Response.Builder builder = ClientProtocol.Response.newBuilder(); - switch (requestType) { - case PUTREQUEST: - return builder.setPutResponse((RegionAPI.PutResponse) response).build(); - case GETREQUEST: - return builder.setGetResponse((RegionAPI.GetResponse) response).build(); - case PUTALLREQUEST: - return builder.setPutAllResponse((RegionAPI.PutAllResponse) response).build(); - case GETREGIONSREQUEST: - return builder.setGetRegionsResponse((RegionAPI.GetRegionsResponse) response).build(); - default: - throw new InvalidProtocolMessageException( - "Unknown request type: " + requestType.getNumber()); - } + return (ClientProtocol.Response) opsHandler.process(serializationService, request, cache); } } http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufSerializationService.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufSerializationService.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufSerializationService.java index fc76d5e..38bf56a 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufSerializationService.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufSerializationService.java @@ -37,6 +37,9 @@ public class ProtobufSerializationService implements SerializationService<BasicT @Override public Object decode(BasicTypes.EncodingType encodingTypeValue, byte[] value) throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + if (encodingTypeValue == BasicTypes.EncodingType.INVALID) { + return null; + } TypeCodec codecForType = getTypeCodecForProtobufType(encodingTypeValue); return codecForType.decode(value); } http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java index 4e76de4..ebd5c6a 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java @@ -18,13 +18,14 @@ import org.apache.geode.cache.Cache; import org.apache.geode.internal.cache.InternalCache; import org.apache.geode.internal.cache.tier.sockets.ClientProtocolMessageHandler; import org.apache.geode.protocol.exception.InvalidProtocolMessageException; -import org.apache.geode.protocol.protobuf.operations.GetRegionsRequestOperationHandler; -import org.apache.geode.protocol.protobuf.operations.GetRequestOperationHandler; -import org.apache.geode.protocol.protobuf.operations.PutRequestOperationHandler; -import org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer; import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; import org.apache.geode.protocol.operations.registry.exception.OperationHandlerAlreadyRegisteredException; import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; +import org.apache.geode.protocol.protobuf.operations.GetRegionNamesRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.GetRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.PutRequestOperationHandler; +import org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer; +import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; import java.io.IOException; @@ -60,8 +61,8 @@ public class ProtobufStreamProcessor implements ClientProtocolMessageHandler { ClientProtocol.Request.RequestAPICase.PUTREQUEST.getNumber(), new PutRequestOperationHandler()); registry.registerOperationHandlerForOperationId( - ClientProtocol.Request.RequestAPICase.GETREGIONSREQUEST.getNumber(), - new GetRegionsRequestOperationHandler()); + ClientProtocol.Request.RequestAPICase.GETREGIONNAMESREQUEST.getNumber(), + new GetRegionNamesRequestOperationHandler()); } public void processOneMessage(InputStream inputStream, OutputStream outputStream, Cache cache) @@ -70,9 +71,10 @@ public class ProtobufStreamProcessor implements ClientProtocolMessageHandler { ClientProtocol.Request request = message.getRequest(); ClientProtocol.Response response = protobufOpsProcessor.process(request, cache); - + ClientProtocol.MessageHeader responseHeader = + ProtobufUtilities.createMessageHeaderForRequest(message); ClientProtocol.Message responseMessage = - ProtobufUtilities.wrapResponseWithDefaultHeader(response); + ProtobufUtilities.createProtobufResponse(responseHeader, response); protobufProtocolSerializer.serialize(responseMessage, outputStream); } http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufUtilities.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufUtilities.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufUtilities.java deleted file mode 100644 index c92da67..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufUtilities.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You 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 - * - * http://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.apache.geode.protocol.protobuf; - -import com.google.protobuf.ByteString; - -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.serialization.SerializationService; -import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; -import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; - -public abstract class ProtobufUtilities { - public static BasicTypes.EncodedValue getEncodedValue(BasicTypes.EncodingType resultEncodingType, - byte[] resultEncodedValue) { - return BasicTypes.EncodedValue.newBuilder().setEncodingType(resultEncodingType) - .setValue(ByteString.copyFrom(resultEncodedValue)).build(); - } - - public static ClientProtocol.Message wrapResponseWithDefaultHeader( - ClientProtocol.Response response) { - return ClientProtocol.Message.newBuilder() - .setMessageHeader(ClientProtocol.MessageHeader.newBuilder()).setResponse(response).build(); - } - - public static Object decodeValue(SerializationService serializationService, - BasicTypes.EncodedValue encodedValue) - throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { - BasicTypes.EncodingType encoding = encodedValue.getEncodingType(); - byte[] bytes = encodedValue.getValue().toByteArray(); - return serializationService.decode(encoding, bytes); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandler.java new file mode 100644 index 0000000..8befdd7 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandler.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 + * + * http://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.apache.geode.protocol.protobuf.operations; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.Region; +import org.apache.geode.protocol.operations.OperationHandler; +import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; +import org.apache.geode.serialization.SerializationService; + +import java.util.Set; + +public class GetRegionNamesRequestOperationHandler + implements OperationHandler<ClientProtocol.Request, ClientProtocol.Response> { + @Override + public ClientProtocol.Response process(SerializationService serializationService, + ClientProtocol.Request request, Cache cache) { + Set<Region<?, ?>> regions = cache.rootRegions(); + return ProtobufResponseUtilities.createGetRegionNamesResponse(regions); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionsRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionsRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionsRequestOperationHandler.java deleted file mode 100644 index dc1d8ac..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionsRequestOperationHandler.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You 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 - * - * http://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.apache.geode.protocol.protobuf.operations; - -import org.apache.geode.cache.Cache; -import org.apache.geode.cache.Region; -import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.EncodingTypeTranslator; -import org.apache.geode.protocol.protobuf.ProtobufUtilities; -import org.apache.geode.protocol.protobuf.RegionAPI; -import org.apache.geode.serialization.SerializationService; -import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; -import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; - -import sun.reflect.generics.tree.BaseType; - -import java.util.Set; - -public class GetRegionsRequestOperationHandler - implements OperationHandler<RegionAPI.GetRegionsRequest, RegionAPI.GetRegionsResponse> { - - @Override - public RegionAPI.GetRegionsResponse process(SerializationService serializationService, - RegionAPI.GetRegionsRequest request, Cache cache) { - Set<Region<?, ?>> regions = cache.rootRegions(); - - RegionAPI.GetRegionsResponse.Builder builder = RegionAPI.GetRegionsResponse.newBuilder(); - - for (Region region : regions) { - builder.addRegions(BasicTypes.Region.newBuilder().setName(region.getName())); - } - builder.setSuccess(true); - return builder.build(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java index 95026e8..13b156f 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java @@ -14,69 +14,55 @@ */ package org.apache.geode.protocol.protobuf.operations; -import org.apache.geode.protocol.protobuf.ProtobufUtilities; import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.*; +import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; +import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.SerializationService; -import org.apache.geode.protocol.protobuf.EncodingTypeTranslator; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class GetRequestOperationHandler - implements OperationHandler<RegionAPI.GetRequest, RegionAPI.GetResponse> { + implements OperationHandler<ClientProtocol.Request, ClientProtocol.Response> { + private static Logger logger = LogManager.getLogger(); @Override - public RegionAPI.GetResponse process(SerializationService serializationService, - RegionAPI.GetRequest request, Cache cache) { - String regionName = request.getRegionName(); - try { - Object decodedKey = ProtobufUtilities.decodeValue(serializationService, request.getKey()); - - - Region region = cache.getRegion(regionName); + public ClientProtocol.Response process(SerializationService serializationService, + ClientProtocol.Request request, Cache cache) { + if (request.getRequestAPICase() != ClientProtocol.Request.RequestAPICase.GETREQUEST) { + return ProtobufResponseUtilities.createAndLogErrorResponse(false, false, + "Improperly formatted get request message.", logger, null); + } + RegionAPI.GetRequest getRequest = request.getGetRequest(); - if (region == null) { - return buildGetResponseFailure(); - } + String regionName = getRequest.getRegionName(); + Region region = cache.getRegion(regionName); + if (region == null) { + return ProtobufResponseUtilities.createErrorResponse(false, false, "Region not found"); + } + try { + Object decodedKey = ProtobufUtilities.decodeValue(serializationService, getRequest.getKey()); Object resultValue = region.get(decodedKey); if (resultValue == null) { - return buildGetResponseKeyNotFound(); + return ProtobufResponseUtilities.createNullGetResponse(); } - return buildGetResponseSuccess(serializationService, resultValue); + BasicTypes.EncodedValue encodedValue = + ProtobufUtilities.createEncodedValue(serializationService, resultValue); + return ProtobufResponseUtilities.createGetResponse(encodedValue); } catch (UnsupportedEncodingTypeException ex) { // can be thrown by encoding or decoding. - cache.getLogger().error("encoding not supported ", ex); + return ProtobufResponseUtilities.createAndLogErrorResponse(false, false, + "Encoding not supported.", logger, ex); } catch (CodecNotRegisteredForTypeException ex) { - cache.getLogger().error("codec error in protobuf deserialization ", ex); + return ProtobufResponseUtilities.createAndLogErrorResponse(true, false, + "Codec error in protobuf deserialization.", logger, ex); } - return buildGetResponseFailure(); - } - - private RegionAPI.GetResponse buildGetResponseKeyNotFound() { - return RegionAPI.GetResponse.newBuilder().setSuccess(true).setKeyExists(false).build(); - } - - private RegionAPI.GetResponse buildGetResponseFailure() { - return RegionAPI.GetResponse.newBuilder().setSuccess(false).build(); - } - - // throws if the object in the cache is not of a class that be serialized via the protobuf - // protocol. - private RegionAPI.GetResponse buildGetResponseSuccess(SerializationService serializationService, - Object resultValue) - throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { - BasicTypes.EncodingType resultEncodingType = - EncodingTypeTranslator.getEncodingTypeForObject(resultValue); - byte[] resultEncodedValue = serializationService.encode(resultEncodingType, resultValue); - - return RegionAPI.GetResponse.newBuilder().setSuccess(true).setKeyExists(true) - .setResult(ProtobufUtilities.getEncodedValue(resultEncodingType, resultEncodedValue)) - .build(); } } http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandler.java index f375244..fecf01d 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandler.java @@ -17,46 +17,56 @@ package org.apache.geode.protocol.protobuf.operations; import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.protocol.protobuf.ProtobufUtilities; -import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.*; +import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; +import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; public class PutRequestOperationHandler - implements OperationHandler<RegionAPI.PutRequest, RegionAPI.PutResponse> { + implements OperationHandler<ClientProtocol.Request, ClientProtocol.Response> { + private static Logger logger = LogManager.getLogger(); @Override - public RegionAPI.PutResponse process(SerializationService serializationService, - RegionAPI.PutRequest request, Cache cache) { + public ClientProtocol.Response process(SerializationService serializationService, + ClientProtocol.Request request, Cache cache) { + if (request.getRequestAPICase() != ClientProtocol.Request.RequestAPICase.PUTREQUEST) { + return ProtobufResponseUtilities.createAndLogErrorResponse(false, false, + "Improperly formatted put request message.", logger, null); + } + RegionAPI.PutRequest putRequest = request.getPutRequest(); + + String regionName = putRequest.getRegionName(); + Region region = cache.getRegion(regionName); + if (region == null) { + return ProtobufResponseUtilities.createAndLogErrorResponse(false, false, + "Region passed by client did not exist: " + regionName, logger, null); + } + try { - String regionName = request.getRegionName(); - BasicTypes.Entry entry = request.getEntry(); + BasicTypes.Entry entry = putRequest.getEntry(); Object decodedValue = ProtobufUtilities.decodeValue(serializationService, entry.getValue()); Object decodedKey = ProtobufUtilities.decodeValue(serializationService, entry.getKey()); - - Region region = cache.getRegion(regionName); - if (region == null) { - cache.getLogger().error("Region passed by client did not exist:" + region); - } else { - try { - region.put(decodedKey, decodedValue); - return RegionAPI.PutResponse.newBuilder().setSuccess(true).build(); - } catch (ClassCastException ex) { - cache.getLogger() - .error("invalid key or value type for region " + regionName + ",passed key: " - + entry.getKey().getEncodingType() + " value: " - + entry.getValue().getEncodingType(), ex); - } + try { + region.put(decodedKey, decodedValue); + return ProtobufResponseUtilities.createPutResponse(); + } catch (ClassCastException ex) { + return ProtobufResponseUtilities.createAndLogErrorResponse(false, false, + "invalid key or value type for region " + regionName + ",passed key: " + + entry.getKey().getEncodingType() + " value: " + + entry.getValue().getEncodingType(), + logger, ex); } } catch (UnsupportedEncodingTypeException ex) { - cache.getLogger().error("encoding not supported ", ex); + return ProtobufResponseUtilities.createAndLogErrorResponse(false, false, + "encoding not supported ", logger, ex); } catch (CodecNotRegisteredForTypeException ex) { - cache.getLogger().error("codec error in protobuf deserialization ", ex); + return ProtobufResponseUtilities.createAndLogErrorResponse(true, false, + "codec error in protobuf deserialization ", logger, ex); } - return RegionAPI.PutResponse.newBuilder().setSuccess(false).build(); } } http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java index 683e42f..566464c 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java @@ -15,8 +15,8 @@ package org.apache.geode.protocol.protobuf.serializer; import org.apache.geode.protocol.exception.InvalidProtocolMessageException; -import org.apache.geode.protocol.serializer.ProtocolSerializer; import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.serializer.ProtocolSerializer; import java.io.IOException; import java.io.InputStream; http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufRequestUtilities.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufRequestUtilities.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufRequestUtilities.java new file mode 100644 index 0000000..b96f478 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufRequestUtilities.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 + * + * http://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.apache.geode.protocol.protobuf.utilities; + +import org.apache.geode.protocol.protobuf.*; + +/** + * This class contains helper functions for generating ClientProtocol.Request objects + * + * Response building helpers can be found in {@link ProtobufResponseUtilities}, while more general + * purpose helpers can be found in {@link ProtobufUtilities} + */ +public abstract class ProtobufRequestUtilities { + /** + * Creates a request object containing a RegionAPI.GetRequest + * + * @param regionName - Name of the region being fetched from + * @param key - Encoded key, see createEncodedValue in {@link ProtobufRequestUtilities} + * @return Request object containing the passed params. + */ + public static ClientProtocol.Request createGetRequest(String regionName, + BasicTypes.EncodedValue key) { + RegionAPI.GetRequest getRequest = + RegionAPI.GetRequest.newBuilder().setRegionName(regionName).setKey(key).build(); + return ClientProtocol.Request.newBuilder().setGetRequest(getRequest).build(); + } + + /** + * Creates a request object containing a RegionAPI.GetRegionNamesRequest + * + * @return Request object for a getRegionNames operation + */ + public static ClientProtocol.Request createGetRegionNamesRequest() { + return ClientProtocol.Request.newBuilder() + .setGetRegionNamesRequest(RegionAPI.GetRegionNamesRequest.newBuilder()).build(); + } + + /** + * Creates a request object containing a RegionAPI.PutRequest + * + * @param region - Name of the region to put data in + * @param entry - Encoded key,value pair, see createEntry in {@link ProtobufRequestUtilities} + * @return Request object containing the passed params. + */ + public static ClientProtocol.Request createPutRequest(String region, BasicTypes.Entry entry) { + RegionAPI.PutRequest putRequest = + RegionAPI.PutRequest.newBuilder().setRegionName(region).setEntry(entry).build(); + return ClientProtocol.Request.newBuilder().setPutRequest(putRequest).build(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufResponseUtilities.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufResponseUtilities.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufResponseUtilities.java new file mode 100644 index 0000000..2114fdb --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufResponseUtilities.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 + * + * http://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.apache.geode.protocol.protobuf.utilities; + +import org.apache.geode.cache.Region; +import org.apache.geode.protocol.protobuf.BasicTypes; +import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.logging.log4j.Logger; + +import java.util.Set; + +/** + * This class contains helper functions for generating ClientProtocol.Response objects. + * + * Request building helpers can be found in {@link ProtobufRequestUtilities}, while more general + * purpose helpers can be found in {@link ProtobufUtilities} + */ +public abstract class ProtobufResponseUtilities { + /** + * This creates response object containing a ClientProtocol.ErrorResponse + * + * @param serverInternal - is this error internal to the server + * @param retriable - can the operation be retried with a potentially different result + * @param errorMessage - description of the error + * @return An error response containing the above parameters + */ + public static ClientProtocol.Response createErrorResponse(boolean serverInternal, + boolean retriable, String errorMessage) { + ClientProtocol.ErrorResponse error = + ClientProtocol.ErrorResponse.newBuilder().setInternalServerError(serverInternal) + .setRetriable(retriable).setMessage(errorMessage).build(); + return ClientProtocol.Response.newBuilder().setErrorResponse(error).build(); + } + + /** + * This creates response object containing a ClientProtocol.ErrorResponse, and also logs the + * passed error message and exception (if present) to the provided logger. + * + * @param serverInternal - is this error internal to the server + * @param retriable - can the operation be retried with a potentially different result + * @param errorMessage - description of the error + * @param logger - logger to write the error message to + * @param ex - exception which should be logged + * @return An error response containing the first three parameters. + */ + public static ClientProtocol.Response createAndLogErrorResponse(boolean serverInternal, + boolean retriable, String errorMessage, Logger logger, Exception ex) { + if (ex != null) { + logger.error(errorMessage, ex); + } else { + logger.error(errorMessage); + } + return createErrorResponse(serverInternal, retriable, errorMessage); + } + + /** + * This creates a response object containing a RegionAPI.GetResponse + * + * @param resultValue - the encoded result value, see createEncodedValue in + * {@link ProtobufUtilities} + * @return A response indicating the passed value was found for a incoming GetRequest + */ + public static ClientProtocol.Response createGetResponse(BasicTypes.EncodedValue resultValue) { + RegionAPI.GetResponse getResponse = + RegionAPI.GetResponse.newBuilder().setResult(resultValue).build(); + return ClientProtocol.Response.newBuilder().setGetResponse(getResponse).build(); + } + + /** + * This creates a response object containing a RegionAPI.GetResponse + * + * @return A response indicating a failure to find a requested key or value + */ + public static ClientProtocol.Response createNullGetResponse() { + return ClientProtocol.Response.newBuilder().setGetResponse(RegionAPI.GetResponse.newBuilder()) + .build(); + } + + /** + * This creates a response object containing a RegionAPI.GetRegionNamesResponse + * + * @param regionSet - A set of regions + * @return A response object containing the names of the regions in the passed regionSet + */ + public static ClientProtocol.Response createGetRegionNamesResponse(Set<Region<?, ?>> regionSet) { + RegionAPI.GetRegionNamesResponse.Builder builder = + RegionAPI.GetRegionNamesResponse.newBuilder(); + for (Region region : regionSet) { + builder.addRegions(region.getName()); + } + return ClientProtocol.Response.newBuilder().setGetRegionNamesResponse(builder).build(); + } + + /** + * This creates a response object containing a RegionAPI.PutResponse + * + * @return A response object indicating a successful put + */ + public static ClientProtocol.Response createPutResponse() { + return ClientProtocol.Response.newBuilder().setPutResponse(RegionAPI.PutResponse.newBuilder()) + .build(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufUtilities.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufUtilities.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufUtilities.java new file mode 100644 index 0000000..9249793 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/utilities/ProtobufUtilities.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 + * + * http://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.apache.geode.protocol.protobuf.utilities; + +import com.google.protobuf.ByteString; +import org.apache.geode.protocol.protobuf.*; +import org.apache.geode.protocol.protobuf.utilities.ProtobufRequestUtilities; +import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; +import org.apache.geode.serialization.SerializationService; +import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; +import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; + +/** + * This class contains helper functions for assistance in creating protobuf objects. This class is + * mainly focused on helper functions which can be used in building BasicTypes for use in other + * messages or those used to create the top level Message objects. + * + * Helper functions specific to creating ClientProtocol.Responses can be found at + * {@link ProtobufResponseUtilities} Helper functions specific to creating ClientProtocol.Requests + * can be found at {@link ProtobufRequestUtilities} + */ +public abstract class ProtobufUtilities { + /** + * Creates a object containing the type and value encoding of a piece of data + * + * @param serializationService - object which knows how to encode objects for the protobuf + * protocol {@link ProtobufSerializationService} + * @param unencodedValue - the value object which is to be encoded + * @return a protobuf EncodedValue object + * @throws UnsupportedEncodingTypeException - The object passed doesn't have a corresponding + * SerializationType + * @throws CodecNotRegisteredForTypeException - There isn't a protobuf codec for the + * SerializationType of the passed object + */ + public static BasicTypes.EncodedValue createEncodedValue( + SerializationService serializationService, Object unencodedValue) + throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + BasicTypes.EncodingType resultEncodingType = + EncodingTypeTranslator.getEncodingTypeForObject(unencodedValue); + byte[] encodedValue = serializationService.encode(resultEncodingType, unencodedValue); + return BasicTypes.EncodedValue.newBuilder().setEncodingType(resultEncodingType) + .setValue(ByteString.copyFrom(encodedValue)).build(); + } + + /** + * Creates a protobuf key,value pair + * + * @param key - an EncodedValue containing the key of the entry + * @param value - an EncodedValue containing the value of the entry + * @return a protobuf Entry object containing the passed key and value + */ + public static BasicTypes.Entry createEntry(BasicTypes.EncodedValue key, + BasicTypes.EncodedValue value) { + return BasicTypes.Entry.newBuilder().setKey(key).setValue(value).build(); + } + + /** + * This creates a protobuf message containing a ClientProtocol.Response + * + * @param messageHeader - The header for the message + * @param response - The response for the message + * @return a protobuf Message containing the above parameters + */ + public static ClientProtocol.Message createProtobufResponse( + ClientProtocol.MessageHeader messageHeader, ClientProtocol.Response response) { + return ClientProtocol.Message.newBuilder().setMessageHeader(messageHeader).setResponse(response) + .build(); + } + + /** + * This creates a protobuf message containing a ClientProtocol.Request + * + * @param messageHeader - The header for the message + * @param request - The request for the message + * @return a protobuf Message containing the above parameters + */ + public static ClientProtocol.Message createProtobufRequest( + ClientProtocol.MessageHeader messageHeader, ClientProtocol.Request request) { + return ClientProtocol.Message.newBuilder().setMessageHeader(messageHeader).setRequest(request) + .build(); + } + + /** + * This builds the MessageHeader for a response which matches an incoming request + * + * @param request - The request message that we're responding to. + * @return the MessageHeader the response to the passed request + */ + public static ClientProtocol.MessageHeader createMessageHeaderForRequest( + ClientProtocol.Message request) { + return createMessageHeader(request.getMessageHeader().getCorrelationId()); + } + + /** + * This creates a MessageHeader + * + * @param correlationId - An identifier used to correlate requests and responses + * @return a MessageHeader containing the above parameters + */ + public static ClientProtocol.MessageHeader createMessageHeader(int correlationId) { + return ClientProtocol.MessageHeader.newBuilder().setCorrelationId(correlationId).build(); + } + + /** + * This will return the object encoded in a protobuf EncodedValue + * + * @param serializationService - object which knows how to encode objects for the protobuf + * protocol {@link ProtobufSerializationService} + * @param encodedValue - The value to be decoded + * @return the object encoded in the passed encodedValue + * @throws UnsupportedEncodingTypeException - There isn't a SerializationType matching the + * encodedValues type + * @throws CodecNotRegisteredForTypeException - There isn't a protobuf codec for the + * SerializationType matching the encodedValues type + */ + public static Object decodeValue(SerializationService serializationService, + BasicTypes.EncodedValue encodedValue) + throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + BasicTypes.EncodingType encoding = encodedValue.getEncodingType(); + byte[] bytes = encodedValue.getValue().toByteArray(); + return serializationService.decode(encoding, bytes); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/IntCodec.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/IntCodec.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/IntCodec.java index 6bd2b5c..04dadeb 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/IntCodec.java +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/codec/IntCodec.java @@ -18,7 +18,6 @@ import org.apache.geode.serialization.SerializationType; import org.apache.geode.serialization.TypeCodec; import java.nio.ByteBuffer; -import java.nio.charset.Charset; public class IntCodec implements TypeCodec<Integer> { @Override http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/proto/clientProtocol.proto ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/proto/clientProtocol.proto b/geode-protobuf/src/main/proto/clientProtocol.proto index 0c19295..d94c0f3 100644 --- a/geode-protobuf/src/main/proto/clientProtocol.proto +++ b/geode-protobuf/src/main/proto/clientProtocol.proto @@ -50,7 +50,7 @@ message Request { PingRequest pingRequest = 41; GetServersRequest getServersRequest = 42; - GetRegionsRequest getRegionsRequest = 43; + GetRegionNamesRequest getRegionNamesRequest = 43; } } @@ -65,15 +65,23 @@ message Response { RemoveAllResponse removeAllResponse = 7; ListKeysResponse listKeysResponse = 8; + ErrorResponse errorResponse = 13; + CreateRegionResponse createRegionResponse = 20; DestroyRegionResponse destroyRegionResponse = 21; PingResponse pingResponse = 41; GetServersResponse getServersResponse = 42; - GetRegionsResponse getRegionsResponse = 43; + GetRegionNamesResponse getRegionNamesResponse = 43; } } +message ErrorResponse { + bool internalServerError = 1; + bool retriable = 2; + string message = 3; +} + message ResponseHeader { oneof reponseType { int32 responseTypeID = 1; http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/main/proto/region_API.proto ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/proto/region_API.proto b/geode-protobuf/src/main/proto/region_API.proto index d3af17a..40c124a 100644 --- a/geode-protobuf/src/main/proto/region_API.proto +++ b/geode-protobuf/src/main/proto/region_API.proto @@ -24,7 +24,7 @@ message PutRequest { } message PutResponse { - bool success = 1; + // message presence indicates success. } message GetRequest { @@ -33,9 +33,7 @@ message GetRequest { } message GetResponse { - bool success = 1; - bool keyExists = 2; - EncodedValue result = 3; + EncodedValue result = 1; } message PutAllRequest { @@ -99,11 +97,10 @@ message DestroyRegionResponse { bool success = 1; } -message GetRegionsRequest { +message GetRegionNamesRequest { } -message GetRegionsResponse { - bool success = 1; - repeated Region regions = 2; +message GetRegionNamesResponse { + repeated string regions = 1; } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/test/java/org/apache/geode/protocol/IntegrationJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/IntegrationJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/IntegrationJUnitTest.java index 42cc7b3..3c484cb 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/IntegrationJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/IntegrationJUnitTest.java @@ -15,15 +15,10 @@ package org.apache.geode.protocol; import com.google.protobuf.ByteString; - import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; -import org.apache.geode.protocol.protobuf.ProtobufStreamProcessor; -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.*; import org.apache.geode.serialization.codec.StringCodec; -import org.apache.geode.protocol.protobuf.EncodingTypeTranslator; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.SerializationCodecRegistry; import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; @@ -34,16 +29,13 @@ import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.*; + @Category(UnitTest.class) public class IntegrationJUnitTest { http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/test/java/org/apache/geode/protocol/MessageUtil.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/MessageUtil.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/MessageUtil.java index dc89724..fee9448 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/MessageUtil.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/MessageUtil.java @@ -14,20 +14,14 @@ */ package org.apache.geode.protocol; -import com.google.protobuf.ByteString; - -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.protocol.protobuf.EncodingTypeTranslator; -import org.apache.geode.protocol.protobuf.RegionAPI; -import org.apache.geode.serialization.codec.StringCodec; +import org.apache.geode.protocol.protobuf.*; +import org.apache.geode.protocol.protobuf.utilities.ProtobufRequestUtilities; +import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; +import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; -import org.apache.geode.serialization.registry.SerializationCodecRegistry; import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; -import java.nio.ByteBuffer; - public class MessageUtil { public static ClientProtocol.Message createGetRequestMessage() { ClientProtocol.Message.Builder messageBuilder = ClientProtocol.Message.newBuilder(); @@ -38,58 +32,27 @@ public class MessageUtil { return messageBuilder.build(); } - public static RegionAPI.PutRequest makePutRequest(String requestKey, String requestValue, - String requestRegion) throws CodecNotRegisteredForTypeException, - UnsupportedEncodingTypeException, CodecAlreadyRegisteredForTypeException { - StringCodec stringCodec = getStringCodec(); - BasicTypes.EncodedValue.Builder key = - BasicTypes.EncodedValue.newBuilder().setEncodingType(BasicTypes.EncodingType.STRING) - .setValue(ByteString.copyFrom(stringCodec.encode(requestKey))); - BasicTypes.EncodedValue.Builder value = - BasicTypes.EncodedValue.newBuilder().setEncodingType(BasicTypes.EncodingType.STRING) - .setValue(ByteString.copyFrom(stringCodec.encode(requestValue))); - BasicTypes.Entry.Builder entry = BasicTypes.Entry.newBuilder().setKey(key).setValue(value); - RegionAPI.PutRequest.Builder putRequestBuilder = RegionAPI.PutRequest.newBuilder(); - putRequestBuilder.setRegionName(requestRegion).setEntry(entry); - - return putRequestBuilder.build(); - } - - public static ClientProtocol.Message makePutRequestMessage(String requestKey, String requestValue, + public static ClientProtocol.Message makePutRequestMessage( + SerializationService serializationService, String requestKey, String requestValue, String requestRegion, ClientProtocol.MessageHeader header) throws CodecNotRegisteredForTypeException, UnsupportedEncodingTypeException, CodecAlreadyRegisteredForTypeException { - ClientProtocol.Request request = ClientProtocol.Request.newBuilder() - .setPutRequest(makePutRequest(requestKey, requestValue, requestRegion)).build(); - return ClientProtocol.Message.newBuilder().setMessageHeader(header).setRequest(request).build(); - } - - public static RegionAPI.GetRequest makeGetRequest(String requestKey, String requestRegion) - throws CodecNotRegisteredForTypeException, UnsupportedEncodingTypeException, - CodecAlreadyRegisteredForTypeException { - StringCodec stringCodec = getStringCodec(); - RegionAPI.GetRequest.Builder getRequestBuilder = RegionAPI.GetRequest.newBuilder(); - getRequestBuilder.setRegionName(requestRegion) - .setKey(BasicTypes.EncodedValue.newBuilder().setEncodingType(BasicTypes.EncodingType.STRING) - .setValue(ByteString.copyFrom(stringCodec.encode(requestKey)))); + BasicTypes.Entry entry = ProtobufUtilities.createEntry( + ProtobufUtilities.createEncodedValue(serializationService, requestKey), + ProtobufUtilities.createEncodedValue(serializationService, requestValue)); - return getRequestBuilder.build(); - } - - public static ClientProtocol.Message makeGetRequestMessage(String requestKey, - String requestRegion, ClientProtocol.MessageHeader header) - throws CodecAlreadyRegisteredForTypeException, UnsupportedEncodingTypeException, - CodecNotRegisteredForTypeException { - ClientProtocol.Request request = ClientProtocol.Request.newBuilder() - .setGetRequest(makeGetRequest(requestKey, requestRegion)).build(); - return ClientProtocol.Message.newBuilder().setMessageHeader(header).setRequest(request).build(); + ClientProtocol.Request request = + ProtobufRequestUtilities.createPutRequest(requestRegion, entry); + return ProtobufUtilities.createProtobufRequest(header, request); } - public static StringCodec getStringCodec() throws CodecAlreadyRegisteredForTypeException, - CodecNotRegisteredForTypeException, UnsupportedEncodingTypeException { - SerializationCodecRegistry serializationCodecRegistry = new SerializationCodecRegistry(); - return (StringCodec) serializationCodecRegistry.getCodecForType( - EncodingTypeTranslator.getSerializationTypeForEncodingType(BasicTypes.EncodingType.STRING)); + public static ClientProtocol.Message makeGetRequestMessage( + SerializationService serializationService, String requestKey, String requestRegion, + ClientProtocol.MessageHeader header) throws CodecAlreadyRegisteredForTypeException, + UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + ClientProtocol.Request request = ProtobufRequestUtilities.createGetRequest(requestRegion, + ProtobufUtilities.createEncodedValue(serializationService, requestKey)); + return ProtobufUtilities.createProtobufRequest(header, request); } private static ClientProtocol.Request.Builder getRequestBuilder() { @@ -103,15 +66,4 @@ public class MessageUtil { private static ClientProtocol.MessageHeader.Builder getMessageHeaderBuilder() { return ClientProtocol.MessageHeader.newBuilder(); } - - public static RegionAPI.GetRegionsRequest makeGetRegionsRequest() { - return RegionAPI.GetRegionsRequest.newBuilder().build(); - } - - public static ClientProtocol.Message makeGetRegionsRequestMessage( - ClientProtocol.MessageHeader header) { - ClientProtocol.Request request = - ClientProtocol.Request.newBuilder().setGetRegionsRequest(makeGetRegionsRequest()).build(); - return ClientProtocol.Message.newBuilder().setMessageHeader(header).setRequest(request).build(); - } } http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/test/java/org/apache/geode/protocol/RoundTripCacheConnectionJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/RoundTripCacheConnectionJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/RoundTripCacheConnectionJUnitTest.java index 77b984f..31a8736 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/RoundTripCacheConnectionJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/RoundTripCacheConnectionJUnitTest.java @@ -15,10 +15,6 @@ package org.apache.geode.protocol; -import static org.junit.Assert.assertEquals; - -import com.google.protobuf.ByteString; - import org.apache.geode.cache.Cache; import org.apache.geode.cache.CacheFactory; import org.apache.geode.cache.Region; @@ -32,7 +28,9 @@ import org.apache.geode.protocol.protobuf.ClientProtocol; import org.apache.geode.protocol.protobuf.ProtobufSerializationService; import org.apache.geode.protocol.protobuf.RegionAPI; import org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer; -import org.apache.geode.serialization.codec.StringCodec; +import org.apache.geode.protocol.protobuf.utilities.ProtobufRequestUtilities; +import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; +import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; @@ -50,6 +48,9 @@ import java.io.OutputStream; import java.net.Socket; import java.util.concurrent.TimeUnit; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + /** * Test that switching on the header byte makes instances of * {@link GenericProtocolServerConnection}. @@ -59,15 +60,18 @@ public class RoundTripCacheConnectionJUnitTest { public static final String TEST_KEY = "testKey"; public static final String TEST_VALUE = "testValue"; public static final String TEST_REGION = "testRegion"; + public static final int TEST_PUT_CORRELATION_ID = 574; + public static final int TEST_GET_CORRELATION_ID = 68451; private Cache cache; private int cacheServerPort; + private SerializationService serializationService; @Rule public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties(); @Before - public void setup() throws IOException { + public void setup() throws Exception { CacheFactory cacheFactory = new CacheFactory(); cacheFactory.set("mcast-port", "0"); // sometimes it isn't due to other tests. cache = cacheFactory.create(); @@ -79,6 +83,7 @@ public class RoundTripCacheConnectionJUnitTest { RegionFactory<Object, Object> regionFactory = cache.createRegionFactory(); Region<Object, Object> testRegion = regionFactory.create(TEST_REGION); + serializationService = new ProtobufSerializationService(); } @After @@ -96,19 +101,20 @@ public class RoundTripCacheConnectionJUnitTest { outputStream.write(110); ProtobufProtocolSerializer protobufProtocolSerializer = new ProtobufProtocolSerializer(); - ClientProtocol.Message putMessage = MessageUtil.makePutRequestMessage(TEST_KEY, TEST_VALUE, - TEST_REGION, ClientProtocol.MessageHeader.newBuilder().build()); + ClientProtocol.Message putMessage = + MessageUtil.makePutRequestMessage(serializationService, TEST_KEY, TEST_VALUE, TEST_REGION, + ProtobufUtilities.createMessageHeader(TEST_PUT_CORRELATION_ID)); protobufProtocolSerializer.serialize(putMessage, outputStream); validatePutResponse(socket, protobufProtocolSerializer); - ClientProtocol.Message getMessage = MessageUtil.makeGetRequestMessage(TEST_KEY, TEST_REGION, - ClientProtocol.MessageHeader.newBuilder().build()); + ClientProtocol.Message getMessage = MessageUtil.makeGetRequestMessage(serializationService, + TEST_KEY, TEST_REGION, ProtobufUtilities.createMessageHeader(TEST_GET_CORRELATION_ID)); protobufProtocolSerializer.serialize(getMessage, outputStream); validateGetResponse(socket, protobufProtocolSerializer); } @Test - public void testNewProtocolGetRegionCallSucceeds() throws Exception { + public void testNullResponse() throws Exception { System.setProperty("geode.feature-protobuf-protocol", "true"); Socket socket = new Socket("localhost", cacheServerPort); @@ -116,34 +122,56 @@ public class RoundTripCacheConnectionJUnitTest { OutputStream outputStream = socket.getOutputStream(); outputStream.write(110); - + // Get request without any data set must return a null ProtobufProtocolSerializer protobufProtocolSerializer = new ProtobufProtocolSerializer(); - ClientProtocol.Message getRegionsMessage = - MessageUtil.makeGetRegionsRequestMessage(ClientProtocol.MessageHeader.newBuilder().build()); - protobufProtocolSerializer.serialize(getRegionsMessage, outputStream); + ClientProtocol.Message getMessage = MessageUtil.makeGetRequestMessage(serializationService, + TEST_KEY, TEST_REGION, ProtobufUtilities.createMessageHeader(TEST_GET_CORRELATION_ID)); + protobufProtocolSerializer.serialize(getMessage, outputStream); ClientProtocol.Message message = protobufProtocolSerializer.deserialize(socket.getInputStream()); + assertEquals(TEST_GET_CORRELATION_ID, message.getMessageHeader().getCorrelationId()); assertEquals(ClientProtocol.Message.MessageTypeCase.RESPONSE, message.getMessageTypeCase()); ClientProtocol.Response response = message.getResponse(); - assertEquals(ClientProtocol.Response.ResponseAPICase.GETREGIONSRESPONSE, + assertEquals(ClientProtocol.Response.ResponseAPICase.GETRESPONSE, response.getResponseAPICase()); - RegionAPI.GetRegionsResponse getRegionsResponse = response.getGetRegionsResponse(); - assertEquals(true, getRegionsResponse.getSuccess()); - assertEquals(1, getRegionsResponse.getRegionsCount()); - assertEquals(TEST_REGION, getRegionsResponse.getRegions(0).getName()); + RegionAPI.GetResponse getResponse = response.getGetResponse(); + + // All the following ways of checking a null response are valid. + assertFalse(getResponse.hasResult()); + assertEquals(BasicTypes.EncodingType.INVALID, getResponse.getResult().getEncodingType()); + assertEquals(null, + ProtobufUtilities.decodeValue(serializationService, getResponse.getResult())); + } + + @Test + public void testNewProtocolGetRegionNamesCallSucceeds() throws Exception { + System.setProperty("geode.feature-protobuf-protocol", "true"); + + Socket socket = new Socket("localhost", cacheServerPort); + Awaitility.await().atMost(5, TimeUnit.SECONDS).until(socket::isConnected); + OutputStream outputStream = socket.getOutputStream(); + outputStream.write(110); + int correlationId = TEST_GET_CORRELATION_ID; // reuse this value for this test + + ProtobufProtocolSerializer protobufProtocolSerializer = new ProtobufProtocolSerializer(); + ClientProtocol.Message getRegionsMessage = ProtobufUtilities.createProtobufRequest( + ProtobufUtilities.createMessageHeader(correlationId), + ProtobufRequestUtilities.createGetRegionNamesRequest()); + protobufProtocolSerializer.serialize(getRegionsMessage, outputStream); + validateGetRegionNamesResponse(socket, correlationId, protobufProtocolSerializer); } private void validatePutResponse(Socket socket, ProtobufProtocolSerializer protobufProtocolSerializer) throws Exception { ClientProtocol.Message message = protobufProtocolSerializer.deserialize(socket.getInputStream()); + assertEquals(TEST_PUT_CORRELATION_ID, message.getMessageHeader().getCorrelationId()); assertEquals(ClientProtocol.Message.MessageTypeCase.RESPONSE, message.getMessageTypeCase()); ClientProtocol.Response response = message.getResponse(); assertEquals(ClientProtocol.Response.ResponseAPICase.PUTRESPONSE, response.getResponseAPICase()); RegionAPI.PutResponse putResponse = response.getPutResponse(); - assertEquals(true, putResponse.getSuccess()); } private void validateGetResponse(Socket socket, @@ -152,6 +180,7 @@ public class RoundTripCacheConnectionJUnitTest { CodecNotRegisteredForTypeException, CodecAlreadyRegisteredForTypeException { ClientProtocol.Message message = protobufProtocolSerializer.deserialize(socket.getInputStream()); + assertEquals(TEST_GET_CORRELATION_ID, message.getMessageHeader().getCorrelationId()); assertEquals(ClientProtocol.Message.MessageTypeCase.RESPONSE, message.getMessageTypeCase()); ClientProtocol.Response response = message.getResponse(); assertEquals(ClientProtocol.Response.ResponseAPICase.GETRESPONSE, @@ -162,4 +191,19 @@ public class RoundTripCacheConnectionJUnitTest { assertEquals(TEST_VALUE, new ProtobufSerializationService().decode(result.getEncodingType(), result.getValue().toByteArray())); } + + private void validateGetRegionNamesResponse(Socket socket, int correlationId, + ProtobufProtocolSerializer protobufProtocolSerializer) + throws InvalidProtocolMessageException, IOException { + ClientProtocol.Message message = + protobufProtocolSerializer.deserialize(socket.getInputStream()); + assertEquals(correlationId, message.getMessageHeader().getCorrelationId()); + assertEquals(ClientProtocol.Message.MessageTypeCase.RESPONSE, message.getMessageTypeCase()); + ClientProtocol.Response response = message.getResponse(); + assertEquals(ClientProtocol.Response.ResponseAPICase.GETREGIONNAMESRESPONSE, + response.getResponseAPICase()); + RegionAPI.GetRegionNamesResponse getRegionsResponse = response.getGetRegionNamesResponse(); + assertEquals(1, getRegionsResponse.getRegionsCount()); + assertEquals(TEST_REGION, getRegionsResponse.getRegions(0)); + } } http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistryJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistryJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistryJUnitTest.java index 612e6a7..def54b5 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistryJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistryJUnitTest.java @@ -14,11 +14,6 @@ */ package org.apache.geode.protocol.operations.registry; -import static io.codearte.catchexception.shade.mockito.Mockito.mock; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; - import org.apache.geode.protocol.operations.OperationHandler; import org.apache.geode.protocol.operations.registry.exception.OperationHandlerAlreadyRegisteredException; import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; @@ -27,6 +22,9 @@ import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; +import static io.codearte.catchexception.shade.mockito.Mockito.mock; +import static org.junit.Assert.*; + @Category(UnitTest.class) public class OperationsHandlerRegistryJUnitTest { public static final int DUMMY_OPERATION_CODE = 999; http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/EncodingTypeToSerializationTypeTranslatorJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/EncodingTypeToSerializationTypeTranslatorJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/EncodingTypeToSerializationTypeTranslatorJUnitTest.java index 8e6f66a..782701f 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/EncodingTypeToSerializationTypeTranslatorJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/EncodingTypeToSerializationTypeTranslatorJUnitTest.java @@ -14,16 +14,14 @@ */ package org.apache.geode.protocol.protobuf; -import static org.junit.Assert.assertSame; - -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.EncodingTypeTranslator; import org.apache.geode.serialization.SerializationType; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.test.junit.categories.UnitTest; import org.junit.Test; import org.junit.experimental.categories.Category; +import static org.junit.Assert.assertSame; + @Category(UnitTest.class) public class EncodingTypeToSerializationTypeTranslatorJUnitTest { http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessorJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessorJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessorJUnitTest.java index c51be5c..f2a76b9 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessorJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessorJUnitTest.java @@ -15,9 +15,6 @@ package org.apache.geode.protocol.protobuf; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - import org.apache.geode.cache.Cache; import org.apache.geode.protocol.exception.InvalidProtocolMessageException; import org.apache.geode.protocol.operations.OperationHandler; @@ -29,6 +26,9 @@ import org.junit.Assert; import org.junit.Test; import org.junit.experimental.categories.Category; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + @Category(UnitTest.class) public class ProtobufOpsProcessorJUnitTest { @Test @@ -43,17 +43,17 @@ public class ProtobufOpsProcessorJUnitTest { ClientProtocol.Request messageRequest = ClientProtocol.Request.newBuilder() .setGetRequest(RegionAPI.GetRequest.newBuilder()).build(); - RegionAPI.GetResponse expectedResponse = RegionAPI.GetResponse.newBuilder().build(); + ClientProtocol.Response expectedResponse = ClientProtocol.Response.newBuilder() + .setGetResponse((RegionAPI.GetResponse.newBuilder())).build(); when(opsHandlerRegistryStub.getOperationHandlerForOperationId(operationID)) .thenReturn(operationHandlerStub); - when(operationHandlerStub.process(serializationServiceStub, - ProtobufOpsProcessor.getRequestForOperationTypeID(messageRequest), dummyCache)) - .thenReturn(expectedResponse); + when(operationHandlerStub.process(serializationServiceStub, messageRequest, dummyCache)) + .thenReturn(expectedResponse); ProtobufOpsProcessor processor = new ProtobufOpsProcessor(opsHandlerRegistryStub, serializationServiceStub); ClientProtocol.Response response = processor.process(messageRequest, dummyCache); - Assert.assertEquals(expectedResponse, response.getGetResponse()); + Assert.assertEquals(expectedResponse, response); } } http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandlerJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandlerJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandlerJUnitTest.java new file mode 100644 index 0000000..d8b97da --- /dev/null +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRegionNamesRequestOperationHandlerJUnitTest.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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 + * + * http://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.apache.geode.protocol.protobuf.operations; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.Region; +import org.apache.geode.protocol.protobuf.BasicTypes; +import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.utilities.ProtobufRequestUtilities; +import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.serialization.SerializationService; +import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; +import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; +import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; +import org.apache.geode.test.dunit.Assert; +import org.apache.geode.test.junit.categories.UnitTest; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@Category(UnitTest.class) +public class GetRegionNamesRequestOperationHandlerJUnitTest { + public static final String TEST_REGION1 = "test region 1"; + public static final String TEST_REGION2 = "test region 2"; + public static final String TEST_REGION3 = "test region 3"; + public Cache cacheStub; + public SerializationService serializationServiceStub; + private GetRegionNamesRequestOperationHandler operationHandler; + + @Before + public void setUp() throws Exception { + serializationServiceStub = mock(SerializationService.class); + when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_REGION1)) + .thenReturn(TEST_REGION1.getBytes(Charset.forName("UTF-8"))); + when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_REGION2)) + .thenReturn(TEST_REGION2.getBytes(Charset.forName("UTF-8"))); + when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_REGION3)) + .thenReturn(TEST_REGION3.getBytes(Charset.forName("UTF-8"))); + + Region<String, String> region1Stub = mock(Region.class); + when(region1Stub.getName()).thenReturn(TEST_REGION1); + Region<String, String> region2Stub = mock(Region.class); + when(region2Stub.getName()).thenReturn(TEST_REGION2); + Region<String, String> region3Stub = mock(Region.class); + when(region3Stub.getName()).thenReturn(TEST_REGION3); + + cacheStub = mock(Cache.class); + when(cacheStub.rootRegions()).thenReturn(Collections.unmodifiableSet( + new HashSet<Region<String, String>>(Arrays.asList(region1Stub, region2Stub, region3Stub)))); + operationHandler = new GetRegionNamesRequestOperationHandler(); + } + + @Test + public void processReturnsCacheRegions() throws CodecAlreadyRegisteredForTypeException, + UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + ClientProtocol.Response response = operationHandler.process(serializationServiceStub, + ProtobufRequestUtilities.createGetRegionNamesRequest(), cacheStub); + Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETREGIONNAMESRESPONSE, + response.getResponseAPICase()); + + RegionAPI.GetRegionNamesResponse getRegionsResponse = response.getGetRegionNamesResponse(); + Assert.assertEquals(3, getRegionsResponse.getRegionsCount()); + + // There's no guarantee for what order we receive the regions in from the response + String name1 = getRegionsResponse.getRegions(0); + String name2 = getRegionsResponse.getRegions(1); + String name3 = getRegionsResponse.getRegions(2); + Assert.assertTrue("The same region was returned multiple times", + name1 != name2 && name1 != name3 && name2 != name3); + Assert.assertTrue(name1 == TEST_REGION1 || name1 == TEST_REGION2 || name1 == TEST_REGION3); + Assert.assertTrue(name2 == TEST_REGION1 || name2 == TEST_REGION2 || name2 == TEST_REGION3); + Assert.assertTrue(name3 == TEST_REGION1 || name3 == TEST_REGION2 || name3 == TEST_REGION3); + } + + @Test + public void processReturnsNoCacheRegions() throws CodecAlreadyRegisteredForTypeException, + UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + Cache emptyCache = mock(Cache.class);; + when(emptyCache.rootRegions()) + .thenReturn(Collections.unmodifiableSet(new HashSet<Region<String, String>>())); + ClientProtocol.Response response = operationHandler.process(serializationServiceStub, + ProtobufRequestUtilities.createGetRegionNamesRequest(), emptyCache); + Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETREGIONNAMESRESPONSE, + response.getResponseAPICase()); + + RegionAPI.GetRegionNamesResponse getRegionsResponse = response.getGetRegionNamesResponse(); + Assert.assertEquals(0, getRegionsResponse.getRegionsCount()); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandlerJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandlerJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandlerJUnitTest.java deleted file mode 100644 index e8f1e65..0000000 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandlerJUnitTest.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You 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 - * - * http://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.apache.geode.protocol.protobuf.operations; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.apache.geode.LogWriter; -import org.apache.geode.cache.Cache; -import org.apache.geode.cache.Region; -import org.apache.geode.protocol.MessageUtil; -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.RegionAPI; -import org.apache.geode.serialization.SerializationService; -import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; -import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; -import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; -import org.apache.geode.test.dunit.Assert; -import org.apache.geode.test.junit.categories.UnitTest; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import java.nio.charset.Charset; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -@Category(UnitTest.class) -public class GetRegionRequestOperationHandlerJUnitTest { - public static final String TEST_REGION1 = "test region 1"; - public static final String TEST_REGION2 = "test region 2"; - public static final String TEST_REGION3 = "test region 3"; - public Cache cacheStub; - public SerializationService serializationServiceStub; - private GetRegionsRequestOperationHandler operationHandler; - - @Before - public void setUp() throws Exception { - serializationServiceStub = mock(SerializationService.class); - when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_REGION1)) - .thenReturn(TEST_REGION1.getBytes(Charset.forName("UTF-8"))); - when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_REGION2)) - .thenReturn(TEST_REGION2.getBytes(Charset.forName("UTF-8"))); - when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_REGION3)) - .thenReturn(TEST_REGION3.getBytes(Charset.forName("UTF-8"))); - - Region<String, String> region1Stub = mock(Region.class); - when(region1Stub.getName()).thenReturn(TEST_REGION1); - Region<String, String> region2Stub = mock(Region.class); - when(region2Stub.getName()).thenReturn(TEST_REGION2); - Region<String, String> region3Stub = mock(Region.class); - when(region3Stub.getName()).thenReturn(TEST_REGION3); - - cacheStub = mock(Cache.class); - when(cacheStub.rootRegions()).thenReturn(Collections.unmodifiableSet( - new HashSet<Region<String, String>>(Arrays.asList(region1Stub, region2Stub, region3Stub)))); - operationHandler = new GetRegionsRequestOperationHandler(); - } - - @Test - public void processReturnsCacheRegions() throws CodecAlreadyRegisteredForTypeException, - UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { - RegionAPI.GetRegionsResponse response = operationHandler.process(serializationServiceStub, - MessageUtil.makeGetRegionsRequest(), cacheStub); - - Assert.assertEquals(true, response.getSuccess()); - Assert.assertEquals(3, response.getRegionsCount()); - - // There's no guarantee for what order we receive the regions in from the response - String name1 = response.getRegions(0).getName(); - String name2 = response.getRegions(1).getName(); - String name3 = response.getRegions(2).getName(); - Assert.assertTrue("The same region was returned multiple times", - name1 != name2 && name1 != name3 && name2 != name3); - Assert.assertTrue(name1 == TEST_REGION1 || name1 == TEST_REGION2 || name1 == TEST_REGION3); - Assert.assertTrue(name2 == TEST_REGION1 || name2 == TEST_REGION2 || name2 == TEST_REGION3); - Assert.assertTrue(name3 == TEST_REGION1 || name3 == TEST_REGION2 || name3 == TEST_REGION3); - } - - @Test - public void processReturnsNoCacheRegions() throws CodecAlreadyRegisteredForTypeException, - UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { - Cache emptyCache = mock(Cache.class);; - when(emptyCache.rootRegions()) - .thenReturn(Collections.unmodifiableSet(new HashSet<Region<String, String>>())); - RegionAPI.GetRegionsResponse response = operationHandler.process(serializationServiceStub, - MessageUtil.makeGetRegionsRequest(), emptyCache); - - Assert.assertEquals(true, response.getSuccess()); - Assert.assertEquals(0, response.getRegionsCount()); - } -}