+
+ /**
+ * Constructs the ResponseString on the basis of request.
+ *
+ * @param executionServiceInput service input
+ * @param countOfSuccesfulEvents number of successive successful events
+ * @return builder for ExecutionServiceOutput response
+ * @throws InvalidProtocolBufferException when response string cannot be converted
+ */
+ public Builder getResponse(ExecutionServiceInput executionServiceInput, int countOfSuccesfulEvents)
+ throws InvalidProtocolBufferException {
+ var resourceName = "DefaultResponseEvent";
+ if (!StringUtils.isBlank(executionServiceInput.getActionIdentifiers().getActionName())) {
+ var actionIdentifiers = executionServiceInput.getActionIdentifiers();
+ resourceName = actionIdentifiers.getBlueprintName() + "-" + actionIdentifiers.getActionName();
+ }
+ if (countOfSuccesfulEvents > 0 && countOfEvents.getAndIncrement() % countOfSuccesfulEvents == 0) {
+ // generating the failure response
+ resourceName = resourceName + "-error.json";
+ } else {
+ resourceName = resourceName + ".json";
+ }
+ LOGGER.info("Fetching response from {}", resourceName);
+ var responseString = ResourceUtils.getResourceAsString(resourceLocation + resourceName);
+ var builder = ExecutionServiceOutput.newBuilder();
+ if (null == responseString) {
+ LOGGER.info("Expected response file {} not found in {}", resourceName, resourceLocation);
+ var actionIdentifiers = executionServiceInput.getActionIdentifiers();
+ builder.setCommonHeader(executionServiceInput.getCommonHeader());
+ builder.setActionIdentifiers(actionIdentifiers);
+ builder.setPayload(executionServiceInput.getPayload());
+ builder.setStatus(Status.newBuilder().setCode(500).setMessage("failure")
+ .setErrorMessage("failed to get get cba file name(" + actionIdentifiers.getBlueprintName()
+ + "), version(" + actionIdentifiers.getBlueprintVersion() + ") from db : file check failed.")
+ .setEventType(EventType.EVENT_COMPONENT_FAILURE).setTimestamp(Instant.now().toString()));
+ } else {
+ LOGGER.debug("Returning response from CDS Simulator: {}", responseString);
+ JsonFormat.parser().ignoringUnknownFields().merge(responseString, builder);
+ }
+ return builder;
+ }
+
+ @Override
+ public void run() {
+ try {
+ server.awaitTermination();
+ } catch (InterruptedException e) {
+ LOGGER.info("gRPC server is terminated");
+ Thread.currentThread().interrupt();
+ }
+ }