geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject [1/2] geode git commit: GEODE-3129 - Added error messages to protobuf protocol. This now closes #621
Date Fri, 07 Jul 2017 20:30:17 GMT
Repository: geode
Updated Branches:
  refs/heads/develop 9d5cf8ea6 -> 0ce62710d


http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/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 f92b194..b7d5201 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,13 +14,13 @@
  */
 package org.apache.geode.protocol.protobuf.operations;
 
-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.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.codec.StringCodec;
 import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException;
 import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException;
 import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException;
@@ -32,10 +32,7 @@ import org.junit.experimental.categories.Category;
 
 import java.nio.charset.Charset;
 
-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;
 
 @Category(UnitTest.class)
@@ -48,6 +45,7 @@ public class GetRequestOperationHandlerJUnitTest {
   public Cache cacheStub;
   public SerializationService serializationServiceStub;
   private GetRequestOperationHandler operationHandler;
+  private StringCodec stringDecoder;
 
   @Before
   public void setUp() throws Exception {
@@ -56,6 +54,10 @@ public class GetRequestOperationHandlerJUnitTest {
         TEST_KEY.getBytes(Charset.forName("UTF-8")))).thenReturn(TEST_KEY);
     when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_VALUE))
         .thenReturn(TEST_VALUE.getBytes(Charset.forName("UTF-8")));
+    when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_KEY))
+        .thenReturn(TEST_KEY.getBytes(Charset.forName("UTF-8")));
+    when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, MISSING_KEY))
+        .thenReturn(MISSING_KEY.getBytes(Charset.forName("UTF-8")));
 
     Region regionStub = mock(Region.class);
     when(regionStub.get(TEST_KEY)).thenReturn(TEST_VALUE);
@@ -64,20 +66,22 @@ public class GetRequestOperationHandlerJUnitTest {
     when(cacheStub.getRegion(TEST_REGION)).thenReturn(regionStub);
     when(cacheStub.getRegion(MISSING_REGION)).thenReturn(null);
     operationHandler = new GetRequestOperationHandler();
+    stringDecoder = new StringCodec();
   }
 
   @Test
   public void processReturnsTheEncodedValueFromTheRegion()
       throws CodecAlreadyRegisteredForTypeException, UnsupportedEncodingTypeException,
       CodecNotRegisteredForTypeException {
-    RegionAPI.GetResponse response = operationHandler.process(serializationServiceStub,
-        MessageUtil.makeGetRequest(TEST_KEY, TEST_REGION), cacheStub);
-
-    Assert.assertEquals(true, response.getSuccess());
-    Assert.assertEquals(true, response.getKeyExists());
-    Assert.assertEquals(BasicTypes.EncodingType.STRING, response.getResult().getEncodingType());
-    String actualValue =
-        MessageUtil.getStringCodec().decode(response.getResult().getValue().toByteArray());
+    ClientProtocol.Request getRequest = generateTestRequest(false, false);
+    ClientProtocol.Response response =
+        operationHandler.process(serializationServiceStub, getRequest, cacheStub);
+
+    Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETRESPONSE,
+        response.getResponseAPICase());
+    RegionAPI.GetResponse getResponse = response.getGetResponse();
+    Assert.assertEquals(BasicTypes.EncodingType.STRING, getResponse.getResult().getEncodingType());
+    String actualValue = stringDecoder.decode(getResponse.getResult().getValue().toByteArray());
     Assert.assertEquals(TEST_VALUE, actualValue);
   }
 
@@ -85,39 +89,51 @@ public class GetRequestOperationHandlerJUnitTest {
   public void processReturnsUnsucessfulResponseForInvalidRegion()
       throws CodecAlreadyRegisteredForTypeException, UnsupportedEncodingTypeException,
       CodecNotRegisteredForTypeException {
-    RegionAPI.GetResponse response = operationHandler.process(serializationServiceStub,
-        MessageUtil.makeGetRequest(TEST_KEY, MISSING_REGION), cacheStub);
+    ClientProtocol.Request getRequest = generateTestRequest(true, false);
+    ClientProtocol.Response response =
+        operationHandler.process(serializationServiceStub, getRequest, cacheStub);
 
-    Assert.assertEquals(false, response.getSuccess());
+    Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE,
+        response.getResponseAPICase());
   }
 
   @Test
   public void processReturnsKeyNotFoundWhenKeyIsNotFound()
       throws CodecAlreadyRegisteredForTypeException, UnsupportedEncodingTypeException,
       CodecNotRegisteredForTypeException {
-    RegionAPI.GetResponse response = operationHandler.process(serializationServiceStub,
-        MessageUtil.makeGetRequest(MISSING_KEY, TEST_REGION), cacheStub);
-
-    Assert.assertEquals(true, response.getSuccess());
-    Assert.assertEquals(false, response.getKeyExists());
+    ClientProtocol.Request getRequest = generateTestRequest(false, true);
+    ClientProtocol.Response response =
+        operationHandler.process(serializationServiceStub, getRequest, cacheStub);
+
+    Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.GETRESPONSE,
+        response.getResponseAPICase());
+    RegionAPI.GetResponse getResponse = response.getGetResponse();
+    Assert.assertFalse(getResponse.hasResult());
   }
 
   @Test
   public void processReturnsErrorWhenUnableToDecodeRequest()
       throws CodecAlreadyRegisteredForTypeException, UnsupportedEncodingTypeException,
       CodecNotRegisteredForTypeException {
-    LogWriter loggerMock = mock(LogWriter.class);
-    when(cacheStub.getLogger()).thenReturn(loggerMock);
-
     CodecNotRegisteredForTypeException exception =
         new CodecNotRegisteredForTypeException("error finding codec for type");
     when(serializationServiceStub.decode(BasicTypes.EncodingType.STRING,
         TEST_KEY.getBytes(Charset.forName("UTF-8")))).thenThrow(exception);
 
-    RegionAPI.GetResponse response = operationHandler.process(serializationServiceStub,
-        MessageUtil.makeGetRequest(TEST_KEY, TEST_REGION), cacheStub);
+    ClientProtocol.Request getRequest = generateTestRequest(false, false);
+    ClientProtocol.Response response =
+        operationHandler.process(serializationServiceStub, getRequest, cacheStub);
+
+    Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE,
+        response.getResponseAPICase());
+  }
 
-    Assert.assertEquals(false, response.getSuccess());
-    verify(loggerMock).error(any(String.class), eq(exception));
+  private ClientProtocol.Request generateTestRequest(boolean missingRegion, boolean missingKey)
+      throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException {
+    String region = missingRegion ? MISSING_REGION : TEST_REGION;
+    String key = missingKey ? MISSING_KEY : TEST_KEY;
+    BasicTypes.EncodedValue testKey =
+        ProtobufUtilities.createEncodedValue(serializationServiceStub, key);
+    return ProtobufRequestUtilities.createGetRequest(region, testKey);
   }
 }

http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/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 ddc23fc..10108ef 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
@@ -14,18 +14,14 @@
  */
 package org.apache.geode.protocol.protobuf.operations;
 
-import com.google.protobuf.ByteString;
-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.EncodingTypeTranslator;
-import org.apache.geode.protocol.protobuf.RegionAPI;
+import org.apache.geode.protocol.protobuf.ClientProtocol;
+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.codec.StringCodec;
 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 org.apache.geode.test.dunit.Assert;
@@ -34,7 +30,6 @@ import org.junit.Before;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
-import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 
 import static org.mockito.Mockito.*;
@@ -47,7 +42,6 @@ public class PutRequestOperationHandlerJUnitTest {
   public Cache cacheStub;
   public SerializationService serializationServiceStub;
   private Region regionMock;
-  private LogWriter loggerMock;
 
   @Before
   public void setUp() throws Exception {
@@ -56,27 +50,28 @@ public class PutRequestOperationHandlerJUnitTest {
         TEST_KEY.getBytes(Charset.forName("UTF-8")))).thenReturn(TEST_KEY);
     when(serializationServiceStub.decode(BasicTypes.EncodingType.STRING,
         TEST_VALUE.getBytes(Charset.forName("UTF-8")))).thenReturn(TEST_VALUE);
+    when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_KEY))
+        .thenReturn(TEST_KEY.getBytes(Charset.forName("UTF-8")));
+    when(serializationServiceStub.encode(BasicTypes.EncodingType.STRING, TEST_VALUE))
+        .thenReturn(TEST_VALUE.getBytes(Charset.forName("UTF-8")));
 
     regionMock = mock(Region.class);
     when(regionMock.put(TEST_KEY, TEST_VALUE)).thenReturn(1);
 
     cacheStub = mock(Cache.class);
     when(cacheStub.getRegion(TEST_REGION)).thenReturn(regionMock);
-
-    loggerMock = mock(LogWriter.class);
-    when(cacheStub.getLogger()).thenReturn(loggerMock);
   }
 
   @Test
   public void test_puttingTheEncodedEntryIntoRegion() throws UnsupportedEncodingTypeException,
       CodecNotRegisteredForTypeException, CodecAlreadyRegisteredForTypeException {
     PutRequestOperationHandler operationHandler = new PutRequestOperationHandler();
+    ClientProtocol.Response response =
+        operationHandler.process(serializationServiceStub, generateTestRequest(), cacheStub);
 
+    Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.PUTRESPONSE,
+        response.getResponseAPICase());
 
-    RegionAPI.PutResponse response = operationHandler.process(serializationServiceStub,
-        MessageUtil.makePutRequest(TEST_KEY, TEST_VALUE, TEST_REGION), cacheStub);
-
-    Assert.assertTrue(response.getSuccess());
     verify(regionMock).put(TEST_KEY, TEST_VALUE);
     verify(regionMock, times(1)).put(anyString(), anyString());
   }
@@ -90,11 +85,11 @@ public class PutRequestOperationHandlerJUnitTest {
         TEST_KEY.getBytes(Charset.forName("UTF-8")))).thenThrow(exception);
     PutRequestOperationHandler operationHandler = new PutRequestOperationHandler();
 
-    RegionAPI.PutResponse response = operationHandler.process(serializationServiceStub,
-        MessageUtil.makePutRequest(TEST_KEY, TEST_VALUE, TEST_REGION), cacheStub);
+    ClientProtocol.Response response =
+        operationHandler.process(serializationServiceStub, generateTestRequest(), cacheStub);
 
-    verify(loggerMock).error(any(String.class), eq(exception));
-    Assert.assertFalse(response.getSuccess());
+    Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE,
+        response.getResponseAPICase());
   }
 
   @Test
@@ -106,11 +101,11 @@ public class PutRequestOperationHandlerJUnitTest {
         TEST_KEY.getBytes(Charset.forName("UTF-8")))).thenThrow(exception);
     PutRequestOperationHandler operationHandler = new PutRequestOperationHandler();
 
-    RegionAPI.PutResponse response = operationHandler.process(serializationServiceStub,
-        MessageUtil.makePutRequest(TEST_KEY, TEST_VALUE, TEST_REGION), cacheStub);
+    ClientProtocol.Response response =
+        operationHandler.process(serializationServiceStub, generateTestRequest(), cacheStub);
 
-    verify(loggerMock).error(any(String.class), eq(exception));
-    Assert.assertFalse(response.getSuccess());
+    Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE,
+        response.getResponseAPICase());
   }
 
   @Test
@@ -118,10 +113,11 @@ public class PutRequestOperationHandlerJUnitTest {
       UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException {
     when(cacheStub.getRegion(TEST_REGION)).thenReturn(null);
     PutRequestOperationHandler operationHandler = new PutRequestOperationHandler();
-    RegionAPI.PutResponse response = operationHandler.process(serializationServiceStub,
-        MessageUtil.makePutRequest(TEST_KEY, TEST_VALUE, TEST_REGION), cacheStub);
+    ClientProtocol.Response response =
+        operationHandler.process(serializationServiceStub, generateTestRequest(), cacheStub);
 
-    Assert.assertFalse(response.getSuccess());
+    Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE,
+        response.getResponseAPICase());
   }
 
   @Test
@@ -130,10 +126,20 @@ public class PutRequestOperationHandlerJUnitTest {
     when(regionMock.put(any(), any())).thenThrow(ClassCastException.class);
 
     PutRequestOperationHandler operationHandler = new PutRequestOperationHandler();
-    RegionAPI.PutResponse response = operationHandler.process(serializationServiceStub,
-        MessageUtil.makePutRequest(TEST_KEY, TEST_VALUE, TEST_REGION), cacheStub);
+    ClientProtocol.Response response =
+        operationHandler.process(serializationServiceStub, generateTestRequest(), cacheStub);
+
+    Assert.assertEquals(ClientProtocol.Response.ResponseAPICase.ERRORRESPONSE,
+        response.getResponseAPICase());
+  }
 
-    verify(loggerMock).error(any(String.class), any(ClassCastException.class));
-    Assert.assertFalse(response.getSuccess());
+  private ClientProtocol.Request generateTestRequest()
+      throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException {
+    BasicTypes.EncodedValue testKey =
+        ProtobufUtilities.createEncodedValue(serializationServiceStub, TEST_KEY);
+    BasicTypes.EncodedValue testValue =
+        ProtobufUtilities.createEncodedValue(serializationServiceStub, TEST_VALUE);
+    BasicTypes.Entry testEntry = ProtobufUtilities.createEntry(testKey, testValue);
+    return ProtobufRequestUtilities.createPutRequest(TEST_REGION, testEntry);
   }
 }

http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/test/java/org/apache/geode/protocol/serializer/ProtobufProtocolSerializerJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/serializer/ProtobufProtocolSerializerJUnitTest.java
b/geode-protobuf/src/test/java/org/apache/geode/protocol/serializer/ProtobufProtocolSerializerJUnitTest.java
index 5a94dae..c389cc2 100644
--- a/geode-protobuf/src/test/java/org/apache/geode/protocol/serializer/ProtobufProtocolSerializerJUnitTest.java
+++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/serializer/ProtobufProtocolSerializerJUnitTest.java
@@ -16,8 +16,8 @@ package org.apache.geode.protocol.serializer;
 
 import org.apache.geode.protocol.MessageUtil;
 import org.apache.geode.protocol.exception.InvalidProtocolMessageException;
-import org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer;
 import org.apache.geode.protocol.protobuf.ClientProtocol;
+import org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer;
 import org.apache.geode.test.junit.categories.UnitTest;
 import org.junit.Assert;
 import org.junit.Before;

http://git-wip-us.apache.org/repos/asf/geode/blob/0ce62710/geode-protobuf/src/test/java/org/apache/geode/serialization/codec/BinaryFormatJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-protobuf/src/test/java/org/apache/geode/serialization/codec/BinaryFormatJUnitTest.java
b/geode-protobuf/src/test/java/org/apache/geode/serialization/codec/BinaryFormatJUnitTest.java
index dd72a19..9821803 100644
--- a/geode-protobuf/src/test/java/org/apache/geode/serialization/codec/BinaryFormatJUnitTest.java
+++ b/geode-protobuf/src/test/java/org/apache/geode/serialization/codec/BinaryFormatJUnitTest.java
@@ -14,10 +14,6 @@
  */
 package org.apache.geode.serialization.codec;
 
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
-
 import org.apache.geode.test.junit.categories.UnitTest;
 import org.junit.Before;
 import org.junit.Test;
@@ -25,6 +21,8 @@ import org.junit.experimental.categories.Category;
 
 import java.nio.charset.Charset;
 
+import static org.junit.Assert.*;
+
 @Category(UnitTest.class)
 public class BinaryFormatJUnitTest {
   private static final Charset UTF8 = Charset.forName("UTF-8");


Mime
View raw message