/** * Gets the Message associated with this request. * @return this request's message. */ public Message getMessage() { if (message == null) { message = getLocal().getMessages().get(messageName); if (message == null) { throw new AvroRuntimeException("Not a local message: "+messageName); } } return message; }
private static Message parseMessage(String message) throws Exception { return Protocol.parse("{\"protocol\": \"org.foo.Bar\"," +"\"types\": []," +"\"messages\": {" + message + "}}").getMessages().values().iterator().next(); }
/** * Provides a gRPC {@link MethodDescriptor} for a RPC method/message of Avro {@link Protocol}. * * @param methodType gRPC type for the method. * @return a {@link MethodDescriptor} */ public MethodDescriptor<Object[], Object> getMethod(String methodName, MethodDescriptor .MethodType methodType) { return methods.computeIfAbsent(methodName, key -> MethodDescriptor.<Object[], Object>newBuilder() .setFullMethodName(generateFullMethodName(serviceName, methodName)) .setType(methodType) .setRequestMarshaller(new AvroRequestMarshaller(protocol.getMessages().get(methodName))) .setResponseMarshaller( new AvroResponseMarshaller(protocol.getMessages().get(methodName))) .build()); } }
@Test public void testEchoBytes() throws IOException { Random random = new Random(); int length = random.nextInt(1024*16); GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("echoBytes").getRequest()); ByteBuffer data = ByteBuffer.allocate(length); random.nextBytes(data.array()); data.flip(); params.put("data", data); Object echoed = requestor.request("echoBytes", params); assertEquals(data, echoed); }
@Test public void testParsing() throws IOException { Protocol protocol = getSimpleProtocol(); assertEquals(protocol.getDoc(), "Protocol used for testing."); assertEquals(6, protocol.getMessages().size()); assertEquals("Pretend you're in a cave!", protocol.getMessages().get("echo").getDoc()); }
@Test public void testUndeclaredError() throws IOException { this.throwUndeclaredError = true; RuntimeException error = null; GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("error").getRequest()); try { requestor.request("error", params); } catch (RuntimeException e) { error = e; } finally { this.throwUndeclaredError = false; } assertNotNull(error); assertTrue(error.toString().contains("foo")); }
@Test public void testError() throws IOException { GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("error").getRequest()); AvroRemoteException error = null; try { requestor.request("error", params); } catch (AvroRemoteException e) { error = e; } assertNotNull(error); assertEquals("an error", ((GenericRecord)error.getValue()).get("message").toString()); }
private void makeRequest(Transceiver t) throws IOException { GenericRecord params = new GenericData.Record(protocol.getMessages().get( "m").getRequest()); params.put("x", 0); GenericRequestor r = new GenericRequestor(protocol, t); assertEquals(1, r.request("m", params)); }
@Test public void testP4() throws Exception { Protocol p = ReflectData.get().getProtocol(P4.class); Protocol.Message message = p.getMessages().get("foo"); assertEquals(Schema.Type.INT, message.getResponse().getType()); Field field = message.getRequest().getField("x"); assertEquals(Schema.Type.INT, field.schema().getType()); }
@Test public void testP2() throws Exception { Schema e1 = ReflectData.get().getSchema(E1.class); assertEquals(Schema.Type.RECORD, e1.getType()); assertTrue(e1.isError()); Field message = e1.getField("detailMessage"); assertNotNull("field 'detailMessage' should not be null", message); Schema messageSchema = message.schema(); assertEquals(Schema.Type.UNION, messageSchema.getType()); assertEquals(Schema.Type.NULL, messageSchema.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, messageSchema.getTypes().get(1).getType()); Protocol p2 = ReflectData.get().getProtocol(P2.class); Protocol.Message m = p2.getMessages().get("error"); // check error schema is union Schema response = m.getErrors(); assertEquals(Schema.Type.UNION, response.getType()); assertEquals(Schema.Type.STRING, response.getTypes().get(0).getType()); assertEquals(e1, response.getTypes().get(1)); }
@Test public void testP0() throws Exception { Protocol p0 = ReflectData.get().getProtocol(P0.class); Protocol.Message message = p0.getMessages().get("foo"); // check response schema is union Schema response = message.getResponse(); assertEquals(Schema.Type.UNION, response.getType()); assertEquals(Schema.Type.NULL, response.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, response.getTypes().get(1).getType()); // check request schema is union Schema request = message.getRequest(); Field field = request.getField("s"); assertNotNull("field 's' should not be null", field); Schema param = field.schema(); assertEquals(Schema.Type.UNION, param.getType()); assertEquals(Schema.Type.NULL, param.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, param.getTypes().get(1).getType()); // check union erasure assertEquals(String.class, ReflectData.get().getClass(response)); assertEquals(String.class, ReflectData.get().getClass(param)); }
@Test public void testHello() throws IOException { GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("hello").getRequest()); params.put("greeting", new Utf8("bob")); Utf8 response = (Utf8)requestor.request("hello", params); assertEquals(new Utf8("goodbye"), response); }
@Test public void testP1() throws Exception { Protocol p1 = ReflectData.get().getProtocol(P1.class); Protocol.Message message = p1.getMessages().get("foo"); // check response schema is union Schema response = message.getResponse(); assertEquals(Schema.Type.UNION, response.getType()); assertEquals(Schema.Type.NULL, response.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, response.getTypes().get(1).getType()); // check request schema is union Schema request = message.getRequest(); Field field = request.getField("s"); assertNotNull("field 's' should not be null", field); Schema param = field.schema(); assertEquals(Schema.Type.UNION, param.getType()); assertEquals(Schema.Type.NULL, param.getTypes().get(0).getType()); assertEquals(Schema.Type.STRING, param.getTypes().get(1).getType()); // check union erasure assertEquals(String.class, ReflectData.get().getClass(response)); assertEquals(String.class, ReflectData.get().getClass(param)); }
@Test public void testMessageFieldAliases() throws IOException{ Protocol protocol = getSimpleProtocol(); final Message msg = protocol.getMessages().get("hello"); assertNotNull(msg); final Schema.Field field = msg.getRequest().getField("greeting"); assertNotNull(field); assertTrue(field.aliases().contains("salute")); }
/** Test that Responder ignores one-way with stateless transport. */ @Test public void testStatelessOneway() throws Exception { // a version of the Simple protocol that doesn't declare "ack" one-way Protocol protocol = new Protocol("Simple", "org.apache.avro.test"); Protocol.Message message = protocol.createMessage("ack", null, Schema.createRecord(new ArrayList<>()), Schema.create(Schema.Type.NULL), Schema.createUnion(new ArrayList<>())); protocol.getMessages().put("ack", message); // call a server over a stateless protocol that has a one-way "ack" GenericRequestor requestor = new GenericRequestor(protocol, createTransceiver()); requestor.request("ack", new GenericData.Record(message.getRequest())); // make the request again, to better test handshakes w/ differing protocols requestor.request("ack", new GenericData.Record(message.getRequest())); }
@Test public void testMessageCustomProperties() throws IOException{ Protocol protocol = getSimpleProtocol(); final Message msg = protocol.getMessages().get("hello"); assertNotNull(msg); final Schema.Field field = msg.getRequest().getField("greeting"); assertNotNull(field); assertEquals("customValue", field.getProp("customProp")); } }
@Test public void testSingleRpc() throws IOException { Transceiver t = new LocalTransceiver(new TestResponder(protocol)); GenericRecord params = new GenericData.Record(protocol.getMessages().get( "m").getRequest()); params.put("x", new Utf8("hello")); GenericRequestor r = new GenericRequestor(protocol, t); for(int x = 0; x < 5; x++) assertEquals(new Utf8("there"), r.request("m", params)); }
@Test(expected=SaslException.class) public void testAnonymousClient() throws Exception { Server s = new SaslSocketServer (new TestResponder(), new InetSocketAddress(0), DIGEST_MD5_MECHANISM, SERVICE, HOST, DIGEST_MD5_PROPS, new TestSaslCallbackHandler()); s.start(); Transceiver c = new SaslSocketTransceiver(new InetSocketAddress(s.getPort())); GenericRequestor requestor = new GenericRequestor(PROTOCOL, c); GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("hello").getRequest()); params.put("greeting", "bob"); Utf8 response = (Utf8)requestor.request("hello", params); assertEquals(new Utf8("goodbye"), response); s.close(); c.close(); }
@Test public void testEcho() throws IOException { GenericRecord record = new GenericData.Record(PROTOCOL.getType("TestRecord")); record.put("name", new Utf8("foo")); record.put("kind", new GenericData.EnumSymbol (PROTOCOL.getType("Kind"), "BAR")); record.put("hash", new GenericData.Fixed (PROTOCOL.getType("MD5"), new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5})); GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("echo").getRequest()); params.put("record", record); Object echoed = requestor.request("echo", params); assertEquals(record, echoed); }
@Test(expected=SaslException.class) public void testWrongPassword() throws Exception { Server s = new SaslSocketServer (new TestResponder(), new InetSocketAddress(0), DIGEST_MD5_MECHANISM, SERVICE, HOST, DIGEST_MD5_PROPS, new TestSaslCallbackHandler()); s.start(); SaslClient saslClient = Sasl.createSaslClient (new String[]{DIGEST_MD5_MECHANISM}, PRINCIPAL, SERVICE, HOST, DIGEST_MD5_PROPS, new WrongPasswordCallbackHandler()); Transceiver c = new SaslSocketTransceiver (new InetSocketAddress(server.getPort()), saslClient); GenericRequestor requestor = new GenericRequestor(PROTOCOL, c); GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("hello").getRequest()); params.put("greeting", "bob"); Utf8 response = (Utf8)requestor.request("hello", params); assertEquals(new Utf8("goodbye"), response); s.close(); c.close(); }