|
| 1 | +package io.contract_testing.contractcase.client; |
| 2 | + |
| 3 | +import static io.contract_testing.contractcase.client.ConnectorIncomingMapper.mapMatchErrorRequest; |
| 4 | +import static io.contract_testing.contractcase.client.ConnectorIncomingMapper.mapMessageErrorRequest; |
| 5 | +import static io.contract_testing.contractcase.client.ConnectorIncomingMapper.mapPrintableTestTitle; |
| 6 | +import static io.contract_testing.contractcase.client.ConnectorOutgoingMapper.mapResult; |
| 7 | +import static io.contract_testing.contractcase.client.ConnectorOutgoingMapper.mapResultResponse; |
| 8 | + |
| 9 | +import io.contract_testing.contractcase.ContractCaseCoreError; |
| 10 | +import io.contract_testing.contractcase.case_boundary.ILogPrinter; |
| 11 | +import io.contract_testing.contractcase.case_boundary.IResultPrinter; |
| 12 | +import io.contract_testing.contractcase.grpc.ContractCaseStream; |
| 13 | +import io.contract_testing.contractcase.grpc.ContractCaseStream.ContractResponse; |
| 14 | +import io.contract_testing.contractcase.grpc.ContractCaseStream.DefinitionRequest; |
| 15 | +import io.contract_testing.contractcase.grpc.ContractCaseStream.ResultResponse; |
| 16 | +import io.contract_testing.contractcase.grpc.ContractCaseStream.ResultSuccess; |
| 17 | +import io.grpc.Status; |
| 18 | +import io.grpc.stub.StreamObserver; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | + |
| 21 | +class ContractResponseStreamObserver implements StreamObserver<ContractResponse> { |
| 22 | + |
| 23 | + private final RpcConnector rpcConnector; |
| 24 | + private final ILogPrinter logPrinter; |
| 25 | + private final IResultPrinter resultPrinter; |
| 26 | + private final ConfigHandle configHandle; |
| 27 | + |
| 28 | + |
| 29 | + public ContractResponseStreamObserver(RpcConnector rpcConnector, |
| 30 | + @NotNull ILogPrinter logPrinter, |
| 31 | + @NotNull IResultPrinter resultPrinter, |
| 32 | + ConfigHandle configHandle) { |
| 33 | + this.rpcConnector = rpcConnector; |
| 34 | + this.logPrinter = logPrinter; |
| 35 | + this.resultPrinter = resultPrinter; |
| 36 | + this.configHandle = configHandle; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void onNext(ContractResponse note) { |
| 41 | + /* For when we receive messages from the server */ |
| 42 | + final var requestId = ConnectorIncomingMapper.map(note.getId()); |
| 43 | + switch (note.getKindCase()) { |
| 44 | + case RUN_STATE_HANDLER -> { |
| 45 | + final var stateHandlerRunRequest = note.getRunStateHandler(); |
| 46 | + // TODO Implement this properly |
| 47 | + rpcConnector.sendResponse( |
| 48 | + DefinitionRequest.newBuilder().setResultResponse(ResultResponse.newBuilder() |
| 49 | + .setResult(ContractCaseStream.BoundaryResult.newBuilder() |
| 50 | + .setSuccess(ResultSuccess.newBuilder().build()) |
| 51 | + .build() |
| 52 | + ) |
| 53 | + ), |
| 54 | + requestId |
| 55 | + ); |
| 56 | + } |
| 57 | + case LOG_REQUEST -> { |
| 58 | + final var logRequest = note.getLogRequest(); |
| 59 | + rpcConnector.sendResponse( |
| 60 | + DefinitionRequest.newBuilder().setResultResponse( |
| 61 | + ResultResponse.newBuilder().setResult( |
| 62 | + mapResult( |
| 63 | + logPrinter.log( |
| 64 | + ConnectorIncomingMapper.map(logRequest.getLevel()), |
| 65 | + ConnectorIncomingMapper.map(logRequest.getTimestamp()), |
| 66 | + ConnectorIncomingMapper.map(logRequest.getVersion()), |
| 67 | + ConnectorIncomingMapper.map(logRequest.getTypeString()), |
| 68 | + ConnectorIncomingMapper.map(logRequest.getLocation()), |
| 69 | + ConnectorIncomingMapper.map(logRequest.getMessage()), |
| 70 | + ConnectorIncomingMapper.map(logRequest.getAdditional()) |
| 71 | + ) |
| 72 | + ) |
| 73 | + ) |
| 74 | + ), |
| 75 | + requestId |
| 76 | + ); |
| 77 | + } |
| 78 | + case PRINT_MATCH_ERROR_REQUEST -> { |
| 79 | + final var printMatchErrorRequest = note.getPrintMatchErrorRequest(); |
| 80 | + rpcConnector.sendResponse( |
| 81 | + mapResultResponse( |
| 82 | + resultPrinter.printMatchError( |
| 83 | + mapMatchErrorRequest(printMatchErrorRequest) |
| 84 | + ) |
| 85 | + ), |
| 86 | + requestId |
| 87 | + ); |
| 88 | + } |
| 89 | + case PRINT_MESSAGE_ERROR_REQUEST -> { |
| 90 | + rpcConnector.sendResponse( |
| 91 | + mapResultResponse( |
| 92 | + resultPrinter.printMessageError( |
| 93 | + mapMessageErrorRequest( |
| 94 | + note.getPrintMessageErrorRequest() |
| 95 | + ) |
| 96 | + ) |
| 97 | + ), |
| 98 | + requestId |
| 99 | + ); |
| 100 | + } |
| 101 | + case PRINT_TEST_TITLE_REQUEST -> { |
| 102 | + final var printTestTitleRequest = note.getPrintTestTitleRequest(); |
| 103 | + rpcConnector.sendResponse(mapResultResponse(resultPrinter.printTestTitle( |
| 104 | + mapPrintableTestTitle(printTestTitleRequest))), requestId); |
| 105 | + } |
| 106 | + case TRIGGER_FUNCTION_REQUEST -> { |
| 107 | + var triggerFunctionRequest = note.getTriggerFunctionRequest(); |
| 108 | + var handle = ConnectorIncomingMapper.map(triggerFunctionRequest.getTriggerFunction() |
| 109 | + .getHandle()); |
| 110 | + if (handle == null) { |
| 111 | + throw new ContractCaseCoreError( |
| 112 | + "Received a trigger request message with a null trigger handle", |
| 113 | + "Java Internal Connector" |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + rpcConnector.sendResponse( |
| 118 | + DefinitionRequest.newBuilder().setResultResponse( |
| 119 | + ResultResponse.newBuilder().setResult( |
| 120 | + mapResult( |
| 121 | + configHandle.getTriggerFunction(handle).trigger( |
| 122 | + ConnectorIncomingMapper.map(triggerFunctionRequest.getConfig() |
| 123 | + ) |
| 124 | + ) |
| 125 | + ) |
| 126 | + ) |
| 127 | + ), |
| 128 | + requestId |
| 129 | + ); |
| 130 | + } |
| 131 | + case RESULT_RESPONSE -> { |
| 132 | + rpcConnector.completeWait(requestId, note.getResultResponse().getResult()); |
| 133 | + } |
| 134 | + case KIND_NOT_SET -> { |
| 135 | + throw new ContractCaseCoreError( |
| 136 | + "Received a message with no kind set", |
| 137 | + "Java Internal Connector" |
| 138 | + ); |
| 139 | + } |
| 140 | + case START_TEST_EVENT -> { |
| 141 | + throw new ContractCaseCoreError( |
| 142 | + "Received start test event incorrectly during a define contract", |
| 143 | + "Java Internal Connector" |
| 144 | + ); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void onError(Throwable t) { |
| 151 | + Status status = Status.fromThrowable(t); |
| 152 | + if (Status.Code.UNAVAILABLE.equals(status.getCode())) { |
| 153 | + System.err.println(""" |
| 154 | + ContractCase was unable to contact its internal server. |
| 155 | + This is either a conflict while starting the server, |
| 156 | + a crash while the server was running, or a bug in |
| 157 | + ContractCase. Please see the rest of the log output |
| 158 | + for details. |
| 159 | +
|
| 160 | + If you are unable to resolve this locally, |
| 161 | + please open an issue here: |
| 162 | +
|
| 163 | + https://github.com/case-contract-testing/contract-case |
| 164 | + """); |
| 165 | + } else { |
| 166 | + System.err.println("ContractCase failed: " + status); |
| 167 | + } |
| 168 | + rpcConnector.setErrorStatus(status); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void onCompleted() { |
| 173 | + } |
| 174 | +} |
0 commit comments