Skip to content

Commit

Permalink
Add handling for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DDH13 committed Apr 29, 2024
1 parent 3c10d00 commit 064a678
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.wso2.apk.integration.utils.Utils;
import org.wso2.apk.integration.utils.clients.SimpleGRPCStudentClient;
import org.wso2.apk.integration.utils.clients.SimpleHTTPClient;
import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentResponse;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -97,8 +98,12 @@ public void systemIsReady() {

@Then("the student response body should contain name: {string} age: {int}")
public void theStudentResponseBodyShouldContainNameAndAge(String arg0, int arg1) {
int age = sharedContext.getStudentResponse().getAge();
String name = sharedContext.getStudentResponse().getName();
StudentResponse studentResponse = sharedContext.getStudentResponse();
if (studentResponse == null) {
Assert.fail("Student response is null.");
}
int age = studentResponse.getAge();
String name = studentResponse.getName();
Assert.assertEquals(name, arg0);
Assert.assertEquals(age, arg1);
}
Expand Down Expand Up @@ -165,6 +170,8 @@ public void GetStudent(String arg0, int arg1) throws StatusRuntimeException {
} catch (StatusRuntimeException e) {
if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){
sharedContext.setGrpcErrorCode(403);
} else {
logger.error(e.getMessage());
}
}
}
Expand All @@ -177,6 +184,8 @@ public void GetStudentDefaultVersion(String arg0, int arg1) throws StatusRuntime
} catch (StatusRuntimeException e) {
if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){
sharedContext.setGrpcErrorCode(403);
} else {
logger.error(e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ public StudentResponse GetStudent(Map<String, String> headers) throws StatusRunt
.intercept(interceptor)
.build();
StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel);
return blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build());
if (blockingStub == null) {
throw new RuntimeException("Failed to create blocking stub");
}
StudentResponse response = blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build());
if (response == null) {
throw new RuntimeException("Failed to get student");
}
return response;
}catch (SSLException e) {
throw new RuntimeException("Failed to create SSL context", e);
} finally {
Expand Down Expand Up @@ -74,7 +81,14 @@ public StudentResponse GetStudentDefaultVersion(Map<String, String> headers) thr
.intercept(interceptor)
.build();
StudentServiceDefaultVersionGrpc.StudentServiceBlockingStub blockingStub = StudentServiceDefaultVersionGrpc.newBlockingStub(managedChannel);
return blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build());
if (blockingStub == null) {
throw new RuntimeException("Failed to create blocking stub");
}
StudentResponse response = blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build());
if (response == null) {
throw new RuntimeException("Failed to get student");
}
return response;
}catch (SSLException e) {
throw new RuntimeException("Failed to create SSL context", e);
} finally {
Expand Down

0 comments on commit 064a678

Please sign in to comment.