Skip to content

Commit

Permalink
add cucumber test for default version
Browse files Browse the repository at this point in the history
  • Loading branch information
DDH13 committed Apr 26, 2024
1 parent 3182a13 commit efc2d79
Show file tree
Hide file tree
Showing 5 changed files with 558 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ public void GetStudent(String arg0, int arg1) throws StatusRuntimeException {
}
}

@Then("I make grpc request GetStudent default version to {string} with port {int}")
public void GetStudentDefaultVersion(String arg0, int arg1) throws StatusRuntimeException {
try {
SimpleGRPCStudentClient grpcStudentClient = new SimpleGRPCStudentClient(arg0, arg1);
sharedContext.setStudentResponse(grpcStudentClient.GetStudent(sharedContext.getHeaders()));
} catch (StatusRuntimeException e) {
if (e.getStatus().getCode()== Status.Code.PERMISSION_DENIED){
sharedContext.setGrpcErrorCode(403);
}
}
}

// It will send request using a new thread and forget about the response
@Then("I send {string} async request to {string} with body {string}")
public void sendAsyncHttpRequest(String httpMethod, String url, String body) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.wso2.apk.integration.utils.GenericClientInterceptor;
import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentRequest;
import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentResponse;
import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentServiceDefaultVersionGrpc;
import org.wso2.apk.integration.utils.clients.studentGrpcClient.StudentServiceGrpc;

import javax.net.ssl.SSLException;
Expand Down Expand Up @@ -60,7 +61,37 @@ public StudentResponse GetStudent(Map<String, String> headers) throws StatusRunt
}
}
}
public StudentResponse GetStudentDefaultVersion(Map<String, String> headers) throws StatusRuntimeException{
ManagedChannel managedChannel = null;
try {
SslContext sslContext = GrpcSslContexts.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();

GenericClientInterceptor interceptor = new GenericClientInterceptor(headers);
managedChannel = NettyChannelBuilder.forAddress(host, port)
.sslContext(sslContext)
.intercept(interceptor)
.build();
StudentServiceDefaultVersionGrpc.StudentServiceBlockingStub blockingStub = StudentServiceDefaultVersionGrpc.newBlockingStub(managedChannel);
return blockingStub.getStudent(StudentRequest.newBuilder().setId(1).build());
}catch (SSLException e) {
throw new RuntimeException("Failed to create SSL context", e);
} finally {
// Shut down the channel to release resources
if (managedChannel != null) {
managedChannel.shutdown(); // Initiates a graceful shutdown
try {
// Wait at most 5 seconds for the channel to terminate
if (!managedChannel.awaitTermination(5, TimeUnit.SECONDS)) {
managedChannel.shutdownNow(); // Force shutdown if it does not complete within the timeout
}
} catch (InterruptedException ie) {
managedChannel.shutdownNow(); // Force shutdown if the thread is interrupted
}
}
}
}

}

Loading

0 comments on commit efc2d79

Please sign in to comment.