From 4ea769977fa618b8328cbaa97194dbe29a6cdd58 Mon Sep 17 00:00:00 2001 From: gloriacai01 Date: Fri, 30 Aug 2024 14:52:34 -0400 Subject: [PATCH] add doCommand in missed files --- .../sdk/core/component/servo/ServoRPCService.java | 9 +++++++++ .../sdk/core/component/servo/ServoRPCClientTest.kt | 10 ++++++++++ .../sdk/core/component/servo/ServoRPCServiceTest.kt | 11 +++++++++++ 3 files changed, 30 insertions(+) diff --git a/core/sdk/src/main/java/com/viam/sdk/core/component/servo/ServoRPCService.java b/core/sdk/src/main/java/com/viam/sdk/core/component/servo/ServoRPCService.java index ebde4799f..5edbefb6c 100644 --- a/core/sdk/src/main/java/com/viam/sdk/core/component/servo/ServoRPCService.java +++ b/core/sdk/src/main/java/com/viam/sdk/core/component/servo/ServoRPCService.java @@ -1,4 +1,5 @@ package com.viam.sdk.core.component.servo; + import com.google.protobuf.Struct; import com.viam.common.v1.Common; import com.viam.component.servo.v1.ServoServiceGrpc; import com.viam.sdk.core.resource.ResourceRPCService; @@ -62,6 +63,14 @@ public void getGeometries(com.viam.common.v1.Common.GetGeometriesRequest request List geometries = servo.getGeometries(Optional.ofNullable(request.getExtra())); responseObserver.onNext(Common.GetGeometriesResponse.newBuilder().addAllGeometries(geometries).build()); responseObserver.onCompleted(); + } + @Override + public void doCommand(Common.DoCommandRequest request, + StreamObserver responseObserver) { + Servo servo = getResource(Servo.named(request.getName())); + Struct result = servo.doCommand(request.getCommand().getFieldsMap()); + responseObserver.onNext(Common.DoCommandResponse.newBuilder().setResult(result).build()); + responseObserver.onCompleted(); } } diff --git a/core/sdk/src/test/kotlin/com/viam/sdk/core/component/servo/ServoRPCClientTest.kt b/core/sdk/src/test/kotlin/com/viam/sdk/core/component/servo/ServoRPCClientTest.kt index 119219b6c..917142f43 100644 --- a/core/sdk/src/test/kotlin/com/viam/sdk/core/component/servo/ServoRPCClientTest.kt +++ b/core/sdk/src/test/kotlin/com/viam/sdk/core/component/servo/ServoRPCClientTest.kt @@ -79,4 +79,14 @@ class ServoRPCClientTest { assertEquals(80, pos) } + @Test + fun doCommand() { + val command = mapOf("foo" to Value.newBuilder().setStringValue("bar").build()) + doReturn(Struct.newBuilder().putAllFields(command).build()).`when`(servo).doCommand(anyMap()) + val response = client.doCommand(command) + verify(servo).doCommand(command) + assertEquals(command, response.fieldsMap) + } + + } diff --git a/core/sdk/src/test/kotlin/com/viam/sdk/core/component/servo/ServoRPCServiceTest.kt b/core/sdk/src/test/kotlin/com/viam/sdk/core/component/servo/ServoRPCServiceTest.kt index 9e0ab57ea..cc7ce585b 100644 --- a/core/sdk/src/test/kotlin/com/viam/sdk/core/component/servo/ServoRPCServiceTest.kt +++ b/core/sdk/src/test/kotlin/com/viam/sdk/core/component/servo/ServoRPCServiceTest.kt @@ -92,4 +92,15 @@ class ServoRPCServiceTest { verify(servo).getGeometries(Optional.of(Struct.getDefaultInstance())) } + @Test + fun doCommand(){ + val command = + Struct.newBuilder().putAllFields(mapOf("foo" to Value.newBuilder().setStringValue("bar").build())).build() + doReturn(command).`when`(servo).doCommand(anyMap()) + val request = Common.DoCommandRequest.newBuilder().setName(servo.name.name).setCommand(command).build() + val response = client.doCommand(request) + verify(servo).doCommand(command.fieldsMap) + assertEquals(command, response.result) + } + } \ No newline at end of file