Repository: geode Updated Branches: refs/heads/feature/GEODE-3141 [created] 0f0fa0c08
GEODE-3141: GetRegion Operation implemented Added OperationHandlerJUnitTest.java as parents class of all OperationHandler tests. General clean up of all `public static final` fields Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/0f0fa0c0 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/0f0fa0c0 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/0f0fa0c0 Branch: refs/heads/feature/GEODE-3141 Commit: 0f0fa0c08b5a66200055a5fc1a008881f5be95ab Parents: cd1a323 Author: Udo Kohlmeyer <[email protected]> Authored: Wed Jul 12 17:22:55 2017 -0700 Committer: Udo Kohlmeyer <[email protected]> Committed: Wed Jul 12 17:22:55 2017 -0700 ---------------------------------------------------------------------- .../protocol/operations/OperationHandler.java | 1 + .../protobuf/ProtobufStreamProcessor.java | 20 +++- .../GetRegionNamesRequestOperationHandler.java | 2 +- .../GetRegionRequestOperationHandler.java | 46 ++++++++ .../operations/GetRequestOperationHandler.java | 2 +- .../operations/PutRequestOperationHandler.java | 2 +- .../RemoveRequestOperationHandler.java | 2 +- .../utilities/ProtobufResponseUtilities.java | 9 ++ .../protobuf/utilities/ProtobufUtilities.java | 54 ++++++---- geode-protobuf/src/main/proto/basicTypes.proto | 7 +- .../src/main/proto/clientProtocol.proto | 3 + geode-protobuf/src/main/proto/region_API.proto | 12 ++- .../org/apache/geode/protocol/MessageUtil.java | 12 +++ .../RoundTripCacheConnectionJUnitTest.java | 25 +++++ .../GetAllRequestOperationHandlerJUnitTest.java | 12 +-- ...onNamesRequestOperationHandlerJUnitTest.java | 21 ++-- ...tRegionRequestOperationHandlerJUnitTest.java | 106 +++++++++++++++++++ .../GetRequestOperationHandlerJUnitTest.java | 41 +++---- .../operations/OperationHandlerJUnitTest.java | 39 +++++++ .../PutAllRequestOperationHandlerJUnitTest.java | 48 +++++---- .../PutRequestOperationHandlerJUnitTest.java | 14 ++- .../RemoveRequestOperationHandlerJUnitTest.java | 37 ++++--- 22 files changed, 397 insertions(+), 118 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java index 90fe177..8a064a8 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java @@ -15,6 +15,7 @@ package org.apache.geode.protocol.operations; import org.apache.geode.cache.Cache; +import org.apache.geode.protocol.protobuf.ClientProtocol; import org.apache.geode.protocol.protobuf.ProtobufOpsProcessor; import org.apache.geode.serialization.SerializationService; http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 ef4affa..ad50bae 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 @@ -21,7 +21,13 @@ import org.apache.geode.protocol.exception.InvalidProtocolMessageException; 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.*; +import org.apache.geode.protocol.protobuf.operations.GetAllRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.GetRegionNamesRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.GetRegionRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.GetRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.PutAllRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.PutRequestOperationHandler; +import org.apache.geode.protocol.protobuf.operations.RemoveRequestOperationHandler; import org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer; import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; @@ -36,15 +42,16 @@ import java.io.OutputStream; * and then pushes it to the output stream. */ public class ProtobufStreamProcessor implements ClientProtocolMessageHandler { - ProtobufProtocolSerializer protobufProtocolSerializer; - OperationsHandlerRegistry registry; - ProtobufSerializationService protobufSerializationService; - ProtobufOpsProcessor protobufOpsProcessor; + private ProtobufProtocolSerializer protobufProtocolSerializer; + private OperationsHandlerRegistry registry; + private ProtobufSerializationService protobufSerializationService; + private ProtobufOpsProcessor protobufOpsProcessor; public ProtobufStreamProcessor() throws OperationHandlerAlreadyRegisteredException, CodecAlreadyRegisteredForTypeException { protobufProtocolSerializer = new ProtobufProtocolSerializer(); registry = new OperationsHandlerRegistry(); + addOperationHandlers(registry); protobufSerializationService = new ProtobufSerializationService(); protobufOpsProcessor = new ProtobufOpsProcessor(registry, protobufSerializationService); @@ -70,6 +77,9 @@ public class ProtobufStreamProcessor implements ClientProtocolMessageHandler { registry.registerOperationHandlerForOperationId( ClientProtocol.Request.RequestAPICase.REMOVEREQUEST.getNumber(), new RemoveRequestOperationHandler()); + registry.registerOperationHandlerForOperationId( + ClientProtocol.Request.RequestAPICase.GETREGIONREQUEST.getNumber(), + new GetRegionRequestOperationHandler()); } public void processOneMessage(InputStream inputStream, OutputStream outputStream, Cache cache) http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 index 8befdd7..b48cb80 100644 --- 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 @@ -27,7 +27,7 @@ public class GetRegionNamesRequestOperationHandler implements OperationHandler<ClientProtocol.Request, ClientProtocol.Response> { @Override public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { + ClientProtocol.Request request, Cache cache) { Set<Region<?, ?>> regions = cache.rootRegions(); return ProtobufResponseUtilities.createGetRegionNamesResponse(regions); } http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandler.java new file mode 100644 index 0000000..23645bb --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandler.java @@ -0,0 +1,46 @@ +/* + * 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.ClientProtocol; +import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.utilities.ProtobufResponseUtilities; +import org.apache.geode.protocol.protobuf.utilities.ProtobufUtilities; +import org.apache.geode.serialization.SerializationService; + +public class GetRegionRequestOperationHandler + implements OperationHandler<ClientProtocol.Request, ClientProtocol.Response> { + + @Override + public ClientProtocol.Response process(SerializationService serializationService, + ClientProtocol.Request request, Cache cache) { + + RegionAPI.GetRegionRequest regionRequest = request.getGetRegionRequest(); + String regionName = regionRequest.getRegionName(); + + Region region = cache.getRegion(regionName); + if (region == null) { + return ProtobufResponseUtilities.createErrorResponse("No region exists for name: "+regionName); + } + + BasicTypes.Region protoRegion = ProtobufUtilities.createRegionMessageFromRegion(region); + + return ProtobufResponseUtilities.createGetRegionResponse(protoRegion); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 d5bcfb9..950baa5 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 @@ -32,7 +32,7 @@ public class GetRequestOperationHandler @Override public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { + ClientProtocol.Request request, Cache cache) { if (request.getRequestAPICase() != ClientProtocol.Request.RequestAPICase.GETREQUEST) { return ProtobufResponseUtilities .createAndLogErrorResponse("Improperly formatted get request message.", logger, null); http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 195aa7a..da8f74c 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 @@ -32,7 +32,7 @@ public class PutRequestOperationHandler @Override public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { + ClientProtocol.Request request, Cache cache) { if (request.getRequestAPICase() != ClientProtocol.Request.RequestAPICase.PUTREQUEST) { return ProtobufResponseUtilities .createAndLogErrorResponse("Improperly formatted put request message.", logger, null); http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandler.java index 725a338..16d8374 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandler.java @@ -33,7 +33,7 @@ public class RemoveRequestOperationHandler @Override public ClientProtocol.Response process(SerializationService serializationService, - ClientProtocol.Request request, Cache cache) { + ClientProtocol.Request request, Cache cache) { if (request.getRequestAPICase() != ClientProtocol.Request.RequestAPICase.REMOVEREQUEST) { return ProtobufResponseUtilities .createAndLogErrorResponse("Improperly formatted get request message.", logger, null); http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 index 9a1a9ce..4347977 100644 --- 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 @@ -140,4 +140,13 @@ public abstract class ProtobufResponseUtilities { return ClientProtocol.Response.newBuilder() .setPutAllResponse(RegionAPI.PutAllResponse.newBuilder()).build(); } + + /** + * This creates a ClientProtocol.Response object containing a RegionAPI.GetRegionResponse + * + * @return A ClientProtocol.Response object indicating a successful getRegionRequest + */ + public static ClientProtocol.Response createGetRegionResponse(BasicTypes.Region protoRegion) { + return ClientProtocol.Response.newBuilder().setGetRegionResponse(RegionAPI.GetRegionResponse.newBuilder().setRegion(protoRegion)).build(); + } } http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 index b632037..d5b4908 100644 --- 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 @@ -15,7 +15,13 @@ package org.apache.geode.protocol.protobuf.utilities; import com.google.protobuf.ByteString; -import org.apache.geode.protocol.protobuf.*; + +import org.apache.geode.cache.Region; +import org.apache.geode.cache.RegionAttributes; +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.ProtobufSerializationService; import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; @@ -25,22 +31,21 @@ import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTy * 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} + * 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} + * 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 + * SerializationType * @throws CodecNotRegisteredForTypeException - There isn't a protobuf codec for the - * SerializationType of the passed object + * SerializationType of the passed object */ public static BasicTypes.EncodedValue createEncodedValue( SerializationService serializationService, Object unencodedValue) @@ -60,7 +65,7 @@ public abstract class ProtobufUtilities { * @return a protobuf Entry object containing the passed key and value */ public static BasicTypes.Entry createEntry(BasicTypes.EncodedValue key, - BasicTypes.EncodedValue value) { + BasicTypes.EncodedValue value) { return BasicTypes.Entry.newBuilder().setKey(key).setValue(value).build(); } @@ -86,7 +91,6 @@ public abstract class ProtobufUtilities { /** * 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 @@ -99,7 +103,6 @@ public abstract class ProtobufUtilities { /** * 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 @@ -112,7 +115,6 @@ public abstract class ProtobufUtilities { /** * 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 */ @@ -123,7 +125,6 @@ public abstract class ProtobufUtilities { /** * This creates a MessageHeader - * * @param correlationId - An identifier used to correlate requests and responses * @return a MessageHeader containing the above parameters */ @@ -133,21 +134,38 @@ public abstract class ProtobufUtilities { /** * 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} + * 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 + * encodedValues type * @throws CodecNotRegisteredForTypeException - There isn't a protobuf codec for the - * SerializationType matching the encodedValues type + * SerializationType matching the encodedValues type */ public static Object decodeValue(SerializationService serializationService, - BasicTypes.EncodedValue encodedValue) + BasicTypes.EncodedValue encodedValue) throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { BasicTypes.EncodingType encoding = encodedValue.getEncodingType(); byte[] bytes = encodedValue.getValue().toByteArray(); return serializationService.decode(encoding, bytes); } + + public static BasicTypes.Region createRegionMessageFromRegion(Region region) { + RegionAttributes regionAttributes = region.getAttributes(); + BasicTypes.Region.Builder protoRegionBuilder = BasicTypes.Region.newBuilder(); + + protoRegionBuilder.setName(region.getName()); + protoRegionBuilder.setSize(region.size()); + + protoRegionBuilder.setPersisted(regionAttributes.getDataPolicy().withPersistence()); + protoRegionBuilder.setKeyConstraint(regionAttributes.getKeyConstraint() == null ? "" + : regionAttributes.getKeyConstraint().toString()); + protoRegionBuilder.setValueConstraint(regionAttributes.getValueConstraint() == null ? "" + : regionAttributes.getValueConstraint().toString()); + + protoRegionBuilder.setScope(regionAttributes.getScope().toString()); + protoRegionBuilder.setType(regionAttributes.getDataPolicy().toString()); + return protoRegionBuilder.build(); + } } http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/geode-protobuf/src/main/proto/basicTypes.proto ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/proto/basicTypes.proto b/geode-protobuf/src/main/proto/basicTypes.proto index 502e28e..987c2ed 100644 --- a/geode-protobuf/src/main/proto/basicTypes.proto +++ b/geode-protobuf/src/main/proto/basicTypes.proto @@ -52,7 +52,12 @@ message CallbackArguments { message Region { string name = 1; - // TODO: key, value types? + string type = 2; + string scope = 3; + string keyConstraint = 4; + string valueConstraint = 5; + bool persisted = 6; + int64 size = 7; } message Server { http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 42de37c..2c3bed0 100644 --- a/geode-protobuf/src/main/proto/clientProtocol.proto +++ b/geode-protobuf/src/main/proto/clientProtocol.proto @@ -51,6 +51,8 @@ message Request { PingRequest pingRequest = 41; GetServersRequest getServersRequest = 42; GetRegionNamesRequest getRegionNamesRequest = 43; + GetRegionRequest getRegionRequest = 44; + } } @@ -72,6 +74,7 @@ message Response { PingResponse pingResponse = 41; GetServersResponse getServersResponse = 42; GetRegionNamesResponse getRegionNamesResponse = 43; + GetRegionResponse getRegionResponse = 44; } } http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 b8f410d..0cf11dc 100644 --- a/geode-protobuf/src/main/proto/region_API.proto +++ b/geode-protobuf/src/main/proto/region_API.proto @@ -102,4 +102,14 @@ message GetRegionNamesRequest { message GetRegionNamesResponse { repeated string regions = 1; -} \ No newline at end of file +} + +/* does a region exist? */ +message GetRegionRequest { + string regionName = 1; +} + +/* success will be true if the region exists */ +message GetRegionResponse { + Region region = 1; +} http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 174ccfa..20216ad 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 @@ -23,6 +23,18 @@ import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredF import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; public class MessageUtil { + + public static RegionAPI.GetRegionRequest makeGetRegionRequest(String requestRegion) { + return RegionAPI.GetRegionRequest.newBuilder().setRegionName(requestRegion).build(); + } + + public static ClientProtocol.Message makeGetRegionRequestMessage(String requestRegion, + ClientProtocol.MessageHeader header) { + ClientProtocol.Request request = + ClientProtocol.Request.newBuilder().setGetRegionRequest(makeGetRegionRequest(requestRegion)).build(); + return ClientProtocol.Message.newBuilder().setMessageHeader(header).setRequest(request).build(); + } + public static ClientProtocol.Message createGetRequestMessage() { ClientProtocol.Message.Builder messageBuilder = ClientProtocol.Message.newBuilder(); messageBuilder.setMessageHeader(getMessageHeaderBuilder()); http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 74c1b0e..b240552 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 @@ -278,6 +278,31 @@ public class RoundTripCacheConnectionJUnitTest { testNewProtocolHeaderLeadsToNewProtocolServerConnection(); } + @Test + public void testNewProtocolGetRegionCallSucceeds() 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); + + + ProtobufProtocolSerializer protobufProtocolSerializer = new ProtobufProtocolSerializer(); + ClientProtocol.Message getRegionMessage = + MessageUtil.makeGetRegionRequestMessage(TEST_REGION, ClientProtocol.MessageHeader.newBuilder().build()); + protobufProtocolSerializer.serialize(getRegionMessage, outputStream); + + ClientProtocol.Message message = + protobufProtocolSerializer.deserialize(socket.getInputStream()); + assertEquals(ClientProtocol.Message.MessageTypeCase.RESPONSE, message.getMessageTypeCase()); + ClientProtocol.Response response = message.getResponse(); + assertEquals(ClientProtocol.Response.ResponseAPICase.GETREGIONRESPONSE, + response.getResponseAPICase()); + response.getGetRegionResponse(); + //TODO add some assertions for Region data + } + private void validatePutResponse(Socket socket, ProtobufProtocolSerializer protobufProtocolSerializer) throws Exception { ClientProtocol.Message message = http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetAllRequestOperationHandlerJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetAllRequestOperationHandlerJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetAllRequestOperationHandlerJUnitTest.java index 0e716fb..c745ffc 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetAllRequestOperationHandlerJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetAllRequestOperationHandlerJUnitTest.java @@ -39,7 +39,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @Category(UnitTest.class) -public class GetAllRequestOperationHandlerJUnitTest { +public class GetAllRequestOperationHandlerJUnitTest extends OperationHandlerJUnitTest{ private static final String TEST_KEY1 = "my key1"; private static final String TEST_VALUE1 = "my value1"; private static final String TEST_KEY2 = "my key2"; @@ -47,14 +47,11 @@ public class GetAllRequestOperationHandlerJUnitTest { private static final String TEST_KEY3 = "my key3"; private static final String TEST_VALUE3 = "my value3"; private static final String TEST_REGION = "test region"; - private Cache cacheStub; - private SerializationService serializationServiceStub; - private GetAllRequestOperationHandler operationHandler; private StringCodec stringDecoder; @Before public void setUp() throws Exception { - serializationServiceStub = mock(SerializationService.class); + super.setUp(); addStringMockEncoding(serializationServiceStub, TEST_KEY1, true, true); addStringMockEncoding(serializationServiceStub, TEST_KEY2, true, true); addStringMockEncoding(serializationServiceStub, TEST_KEY3, true, true); @@ -77,7 +74,6 @@ public class GetAllRequestOperationHandlerJUnitTest { } }); - cacheStub = mock(Cache.class); when(cacheStub.getRegion(TEST_REGION)).thenReturn(regionStub); operationHandler = new GetAllRequestOperationHandler(); stringDecoder = new StringCodec(); @@ -101,7 +97,7 @@ public class GetAllRequestOperationHandlerJUnitTest { CodecNotRegisteredForTypeException { ClientProtocol.Request getRequest = generateTestRequest(true); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, getRequest, cacheStub); + (ClientProtocol.Response) operationHandler.process(serializationServiceStub, getRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETALLRESPONSE, response.getResponseAPICase()); @@ -121,7 +117,7 @@ public class GetAllRequestOperationHandlerJUnitTest { throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { ClientProtocol.Request getRequest = generateTestRequest(false); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, getRequest, cacheStub); + (ClientProtocol.Response) operationHandler.process(serializationServiceStub, getRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETALLRESPONSE, response.getResponseAPICase()); http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 index d8b97da..9798522 100644 --- 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 @@ -39,17 +39,15 @@ 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; +public class GetRegionNamesRequestOperationHandlerJUnitTest extends OperationHandlerJUnitTest{ + private final String TEST_REGION1 = "test region 1"; + private final String TEST_REGION2 = "test region 2"; + private final String TEST_REGION3 = "test region 3"; @Before public void setUp() throws Exception { - serializationServiceStub = mock(SerializationService.class); + super.setUp(); + when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_REGION1)) .thenReturn(TEST_REGION1.getBytes(Charset.forName("UTF-8"))); when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_REGION2)) @@ -64,16 +62,15 @@ public class GetRegionNamesRequestOperationHandlerJUnitTest { 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)))); + new HashSet<>(Arrays.asList(region1Stub, region2Stub, region3Stub)))); operationHandler = new GetRegionNamesRequestOperationHandler(); } @Test public void processReturnsCacheRegions() throws CodecAlreadyRegisteredForTypeException, UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { - ClientProtocol.Response response = operationHandler.process(serializationServiceStub, + ClientProtocol.Response response = (ClientProtocol.Response) operationHandler.process(serializationServiceStub, ProtobufRequestUtilities.createGetRegionNamesRequest(), cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETREGIONNAMESRESPONSE, response.getResponseAPICase()); @@ -98,7 +95,7 @@ public class GetRegionNamesRequestOperationHandlerJUnitTest { Cache emptyCache = mock(Cache.class);; when(emptyCache.rootRegions()) .thenReturn(Collections.unmodifiableSet(new HashSet<Region<String, String>>())); - ClientProtocol.Response response = operationHandler.process(serializationServiceStub, + ClientProtocol.Response response = (ClientProtocol.Response) operationHandler.process(serializationServiceStub, ProtobufRequestUtilities.createGetRegionNamesRequest(), emptyCache); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETREGIONNAMESRESPONSE, response.getResponseAPICase()); http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/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 new file mode 100644 index 0000000..ec9d50b --- /dev/null +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRegionRequestOperationHandlerJUnitTest.java @@ -0,0 +1,106 @@ +/* + * 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.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.DataPolicy; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.RegionAttributes; +import org.apache.geode.cache.Scope; +import org.apache.geode.protocol.MessageUtil; +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.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; + +@Category(UnitTest.class) +public class GetRegionRequestOperationHandlerJUnitTest extends OperationHandlerJUnitTest { + private final String TEST_REGION1 = "test region 1"; + private Region region1Stub; + + @Before + public void setUp() throws Exception { + super.setUp(); + + when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_REGION1)) + .thenReturn(TEST_REGION1.getBytes(Charset.forName("UTF-8"))); + + region1Stub = mock(Region.class); + when(region1Stub.getName()).thenReturn(TEST_REGION1); + + operationHandler = new GetRegionRequestOperationHandler(); + } + + @Test + public void processReturnsCacheRegions() throws CodecAlreadyRegisteredForTypeException, + UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + + RegionAttributes regionAttributesStub = mock(RegionAttributes.class); + when(cacheStub.getRegion(TEST_REGION1)).thenReturn(region1Stub); + when(region1Stub.getName()).thenReturn(TEST_REGION1); + when(region1Stub.size()).thenReturn(10); + when(region1Stub.getAttributes()).thenReturn(regionAttributesStub); + when(regionAttributesStub.getDataPolicy()).thenReturn(DataPolicy.PERSISTENT_REPLICATE); + when(regionAttributesStub.getKeyConstraint()).thenReturn(String.class); + when(regionAttributesStub.getValueConstraint()).thenReturn(Integer.class); + when(regionAttributesStub.getScope()).thenReturn(Scope.DISTRIBUTED_ACK); + + ClientProtocol.Response response = + (ClientProtocol.Response) operationHandler.process(serializationServiceStub, createRequestMessage( + MessageUtil.makeGetRegionRequest(TEST_REGION1)), cacheStub); + BasicTypes.Region region = response.getGetRegionResponse().getRegion(); + Assert.assertEquals(TEST_REGION1, region.getName()); + Assert.assertEquals(String.class.toString(), region.getKeyConstraint()); + Assert.assertEquals(Scope.DISTRIBUTED_ACK.toString(), region.getScope()); + Assert.assertEquals(DataPolicy.PERSISTENT_REPLICATE.toString(), region.getType()); + Assert.assertEquals(Integer.class.toString(), region.getValueConstraint()); + Assert.assertEquals(true, region.getPersisted()); + Assert.assertEquals(10, region.getSize()); + } + + private ClientProtocol.Request createRequestMessage(RegionAPI.GetRegionRequest getRegionRequest) { + return ClientProtocol.Request.newBuilder().setGetRegionRequest(getRegionRequest).build(); + } + + @Test + public void processReturnsNoCacheRegions() throws CodecAlreadyRegisteredForTypeException, + UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + Cache emptyCache = mock(Cache.class); + when(emptyCache.rootRegions()) + .thenReturn(Collections.unmodifiableSet(new HashSet<Region<String, String>>())); + String unknownRegionName = "UNKNOWN_REGION"; + ClientProtocol.Response response = (ClientProtocol.Response) operationHandler.process(serializationServiceStub, + createRequestMessage(MessageUtil.makeGetRegionRequest(unknownRegionName)), emptyCache); + + Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE.getNumber(), + response.getResponseAPICase().getNumber()); + Assert.assertEquals("No region exists for name: " + unknownRegionName,response.getErrorResponse().getMessage()); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandlerJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandlerJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandlerJUnitTest.java index 2f2e47e..a3d9d91 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandlerJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandlerJUnitTest.java @@ -14,6 +14,9 @@ */ package org.apache.geode.protocol.protobuf.operations; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.protobuf.BasicTypes; @@ -34,25 +37,20 @@ import org.junit.experimental.categories.Category; import java.nio.charset.Charset; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - @Category(UnitTest.class) -public class GetRequestOperationHandlerJUnitTest { - public static final String TEST_KEY = "my key"; - public static final String TEST_VALUE = "my value"; - public static final String TEST_REGION = "test region"; - public static final String MISSING_REGION = "missing region"; - public static final String MISSING_KEY = "missing key"; - public static final String NULLED_KEY = "nulled key"; - public Cache cacheStub; - public SerializationService serializationServiceStub; - private GetRequestOperationHandler operationHandler; +public class GetRequestOperationHandlerJUnitTest extends OperationHandlerJUnitTest { + private final String TEST_KEY = "my key"; + private final String TEST_VALUE = "my value"; + private final String TEST_REGION = "test region"; + private final String MISSING_REGION = "missing region"; + private final String MISSING_KEY = "missing key"; + private final String NULLED_KEY = "nulled key"; private StringCodec stringDecoder; @Before public void setUp() throws Exception { - serializationServiceStub = mock(SerializationService.class); + super.setUp(); + when(serializationServiceStub.decode(BasicTypes.EncodingType.STRING, TEST_KEY.getBytes(Charset.forName("UTF-8")))).thenReturn(TEST_KEY); when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_VALUE)) @@ -75,7 +73,6 @@ public class GetRequestOperationHandlerJUnitTest { when(regionStub.containsKey(MISSING_KEY)).thenReturn(false); when(regionStub.containsKey(NULLED_KEY)).thenReturn(true); - cacheStub = mock(Cache.class); when(cacheStub.getRegion(TEST_REGION)).thenReturn(regionStub); when(cacheStub.getRegion(MISSING_REGION)).thenReturn(null); operationHandler = new GetRequestOperationHandler(); @@ -88,7 +85,8 @@ public class GetRequestOperationHandlerJUnitTest { CodecNotRegisteredForTypeException { ClientProtocol.Request getRequest = generateTestRequest(false, false, false); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, getRequest, cacheStub); + (ClientProtocol.Response) operationHandler + .process(serializationServiceStub, getRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETRESPONSE, response.getResponseAPICase()); @@ -104,7 +102,8 @@ public class GetRequestOperationHandlerJUnitTest { CodecNotRegisteredForTypeException { ClientProtocol.Request getRequest = generateTestRequest(true, false, false); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, getRequest, cacheStub); + (ClientProtocol.Response) operationHandler + .process(serializationServiceStub, getRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE, response.getResponseAPICase()); @@ -116,7 +115,7 @@ public class GetRequestOperationHandlerJUnitTest { CodecNotRegisteredForTypeException { ClientProtocol.Request getRequest = generateTestRequest(false, true, false); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, getRequest, cacheStub); + (ClientProtocol.Response) operationHandler.process(serializationServiceStub, getRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETRESPONSE, response.getResponseAPICase()); @@ -130,7 +129,8 @@ public class GetRequestOperationHandlerJUnitTest { CodecNotRegisteredForTypeException { ClientProtocol.Request getRequest = generateTestRequest(false, false, true); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, getRequest, cacheStub); + (ClientProtocol.Response) operationHandler + .process(serializationServiceStub, getRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETRESPONSE, response.getResponseAPICase()); @@ -149,7 +149,8 @@ public class GetRequestOperationHandlerJUnitTest { ClientProtocol.Request getRequest = generateTestRequest(false, false, false); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, getRequest, cacheStub); + (ClientProtocol.Response) operationHandler + .process(serializationServiceStub, getRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE, response.getResponseAPICase()); http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/OperationHandlerJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/OperationHandlerJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/OperationHandlerJUnitTest.java new file mode 100644 index 0000000..2ff2d13 --- /dev/null +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/OperationHandlerJUnitTest.java @@ -0,0 +1,39 @@ +/* + * 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.Mockito.mock; + +import org.apache.geode.cache.Cache; +import org.apache.geode.protocol.operations.OperationHandler; +import org.apache.geode.serialization.SerializationService; +import org.apache.geode.test.junit.categories.UnitTest; +import org.junit.Before; +import org.junit.experimental.categories.Category; + +@Category(UnitTest.class) +public class OperationHandlerJUnitTest { + protected Cache cacheStub; + protected SerializationService serializationServiceStub; + protected OperationHandler operationHandler; + + @Before + public void setUp() throws Exception { + cacheStub = mock(Cache.class); + serializationServiceStub = mock(SerializationService.class); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/PutAllRequestOperationHandlerJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/PutAllRequestOperationHandlerJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/PutAllRequestOperationHandlerJUnitTest.java index 1b9648e..bd56af6 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/PutAllRequestOperationHandlerJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/PutAllRequestOperationHandlerJUnitTest.java @@ -14,6 +14,14 @@ */ package org.apache.geode.protocol.protobuf.operations; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.argThat; +import static org.mockito.Mockito.doThrow; +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 org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.protobuf.BasicTypes; @@ -26,9 +34,11 @@ import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredF 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.hamcrest.CoreMatchers; import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.mockito.ArgumentMatcher; import java.nio.ByteBuffer; import java.nio.charset.Charset; @@ -37,27 +47,21 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import static org.mockito.Mockito.*; -import org.hamcrest.CoreMatchers; -import org.mockito.ArgumentMatcher; - @Category(UnitTest.class) -public class PutAllRequestOperationHandlerJUnitTest { - private static final String TEST_KEY1 = "my key1"; - private static final String TEST_KEY2 = "my key2"; - private static final String TEST_KEY3 = "my key3"; - private static final String TEST_INVALID_KEY = "invalid key"; - private static final String TEST_VALUE1 = "my value1"; - private static final String TEST_VALUE2 = "my value2"; - private static final String TEST_VALUE3 = "my value3"; - private static final Integer TEST_INVALID_VALUE = 732; - private static final String TEST_REGION = "test region"; - private static final String EXCEPTION_TEXT = "Simulating put failure"; - private Cache cacheStub; - private SerializationService serializationServiceStub; +public class PutAllRequestOperationHandlerJUnitTest extends OperationHandlerJUnitTest { + private final String TEST_KEY1 = "my key1"; + private final String TEST_KEY2 = "my key2"; + private final String TEST_KEY3 = "my key3"; + private final String TEST_INVALID_KEY = "invalid key"; + private final String TEST_VALUE1 = "my value1"; + private final String TEST_VALUE2 = "my value2"; + private final String TEST_VALUE3 = "my value3"; + private final Integer TEST_INVALID_VALUE = 732; + private final String TEST_REGION = "test region"; + private final String EXCEPTION_TEXT = "Simulating put failure"; private Region regionMock; - class isAMapContainingInvalidKey implements ArgumentMatcher<Map> { + private class MapContainingInvalidKeyMatcher implements ArgumentMatcher<Map> { @Override public boolean matches(Map argument) { return argument.containsKey(TEST_INVALID_KEY); @@ -66,7 +70,8 @@ public class PutAllRequestOperationHandlerJUnitTest { @Before public void setUp() throws Exception { - serializationServiceStub = mock(SerializationService.class); + super.setUp(); + addStringStubEncoding(serializationServiceStub, TEST_KEY1); addStringStubEncoding(serializationServiceStub, TEST_KEY2); addStringStubEncoding(serializationServiceStub, TEST_KEY3); @@ -78,14 +83,13 @@ public class PutAllRequestOperationHandlerJUnitTest { .thenReturn(ByteBuffer.allocate(Integer.BYTES).putInt(TEST_INVALID_VALUE).array()); when(serializationServiceStub.decode(BasicTypes.EncodingType.INT, ByteBuffer.allocate(Integer.BYTES).putInt(TEST_INVALID_VALUE).array())) - .thenReturn(TEST_INVALID_VALUE); + .thenReturn(TEST_INVALID_VALUE); regionMock = mock(Region.class); doThrow(new ClassCastException(EXCEPTION_TEXT)).when(regionMock) - .putAll(argThat(new isAMapContainingInvalidKey())); + .putAll(argThat(new MapContainingInvalidKeyMatcher())); - cacheStub = mock(Cache.class); when(cacheStub.getRegion(TEST_REGION)).thenReturn(regionMock); } http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandlerJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandlerJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandlerJUnitTest.java index 10108ef..a861854 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandlerJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/PutRequestOperationHandlerJUnitTest.java @@ -35,17 +35,16 @@ import java.nio.charset.Charset; import static org.mockito.Mockito.*; @Category(UnitTest.class) -public class PutRequestOperationHandlerJUnitTest { - public static final String TEST_KEY = "my key"; - public static final String TEST_VALUE = "99"; - public static final String TEST_REGION = "test region"; - public Cache cacheStub; - public SerializationService serializationServiceStub; +public class PutRequestOperationHandlerJUnitTest extends OperationHandlerJUnitTest { + private final String TEST_KEY = "my key"; + private final String TEST_VALUE = "99"; + private final String TEST_REGION = "test region"; private Region regionMock; @Before public void setUp() throws Exception { - serializationServiceStub = mock(SerializationService.class); + super.setUp(); + when(serializationServiceStub.decode(BasicTypes.EncodingType.STRING, TEST_KEY.getBytes(Charset.forName("UTF-8")))).thenReturn(TEST_KEY); when(serializationServiceStub.decode(BasicTypes.EncodingType.STRING, @@ -58,7 +57,6 @@ public class PutRequestOperationHandlerJUnitTest { regionMock = mock(Region.class); when(regionMock.put(TEST_KEY, TEST_VALUE)).thenReturn(1); - cacheStub = mock(Cache.class); when(cacheStub.getRegion(TEST_REGION)).thenReturn(regionMock); } http://git-wip-us.apache.org/repos/asf/geode/blob/0f0fa0c0/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandlerJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandlerJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandlerJUnitTest.java index c66d2f2..5c5c6dd 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandlerJUnitTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/operations/RemoveRequestOperationHandlerJUnitTest.java @@ -14,6 +14,10 @@ */ package org.apache.geode.protocol.protobuf.operations; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; import org.apache.geode.protocol.protobuf.BasicTypes; @@ -34,26 +38,19 @@ import org.junit.experimental.categories.Category; import java.nio.charset.Charset; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - @Category(UnitTest.class) -public class RemoveRequestOperationHandlerJUnitTest { - public static final String TEST_KEY = "my key"; - public static final String TEST_VALUE = "my value"; - public static final String TEST_REGION = "test region"; - public static final String MISSING_REGION = "missing region"; - public static final String MISSING_KEY = "missing key"; - public Cache cacheStub; - public SerializationService serializationServiceStub; - private RemoveRequestOperationHandler operationHandler; +public class RemoveRequestOperationHandlerJUnitTest extends OperationHandlerJUnitTest { + private final String TEST_KEY = "my key"; + private final String TEST_VALUE = "my value"; + private final String TEST_REGION = "test region"; + private final String MISSING_REGION = "missing region"; + private final String MISSING_KEY = "missing key"; private StringCodec stringDecoder; private Region regionStub; @Before public void setUp() throws Exception { - serializationServiceStub = mock(SerializationService.class); + super.setUp(); when(serializationServiceStub.decode(BasicTypes.EncodingType.STRING, TEST_KEY.getBytes(Charset.forName("UTF-8")))).thenReturn(TEST_KEY); when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_VALUE)) @@ -68,7 +65,6 @@ public class RemoveRequestOperationHandlerJUnitTest { when(regionStub.containsKey(TEST_KEY)).thenReturn(true); when(regionStub.containsKey(MISSING_KEY)).thenReturn(false); - cacheStub = mock(Cache.class); when(cacheStub.getRegion(TEST_REGION)).thenReturn(regionStub); when(cacheStub.getRegion(MISSING_REGION)).thenReturn(null); operationHandler = new RemoveRequestOperationHandler(); @@ -81,7 +77,8 @@ public class RemoveRequestOperationHandlerJUnitTest { CodecNotRegisteredForTypeException { ClientProtocol.Request removeRequest = generateTestRequest(false, false); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, removeRequest, cacheStub); + (ClientProtocol.Response) operationHandler + .process(serializationServiceStub, removeRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.REMOVERESPONSE, response.getResponseAPICase()); @@ -95,7 +92,8 @@ public class RemoveRequestOperationHandlerJUnitTest { CodecNotRegisteredForTypeException { ClientProtocol.Request removeRequest = generateTestRequest(true, false); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, removeRequest, cacheStub); + (ClientProtocol.Response) operationHandler + .process(serializationServiceStub, removeRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE, response.getResponseAPICase()); @@ -107,7 +105,8 @@ public class RemoveRequestOperationHandlerJUnitTest { CodecNotRegisteredForTypeException { ClientProtocol.Request removeRequest = generateTestRequest(false, true); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, removeRequest, cacheStub); + (ClientProtocol.Response) operationHandler + .process(serializationServiceStub, removeRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.REMOVERESPONSE, response.getResponseAPICase()); @@ -125,7 +124,7 @@ public class RemoveRequestOperationHandlerJUnitTest { ClientProtocol.Request removeRequest = generateTestRequest(false, false); ClientProtocol.Response response = - operationHandler.process(serializationServiceStub, removeRequest, cacheStub); + (ClientProtocol.Response) operationHandler.process(serializationServiceStub, removeRequest, cacheStub); Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE, response.getResponseAPICase());
