|
| 1 | +package i5.las2peer.services.apiTestingBot; |
| 2 | + |
| 3 | +import i5.las2peer.api.Context; |
| 4 | +import i5.las2peer.services.apiTestingBot.chat.Intent; |
| 5 | +import i5.las2peer.services.apiTestingBot.chat.MessageHandler; |
| 6 | +import i5.las2peer.services.apiTestingBot.context.TestModelingContext; |
| 7 | +import i5.las2peer.services.apiTestingBot.context.TestModelingState; |
| 8 | +import io.swagger.annotations.Api; |
| 9 | +import org.json.simple.JSONObject; |
| 10 | +import org.json.simple.JSONValue; |
| 11 | + |
| 12 | +import javax.ws.rs.GET; |
| 13 | +import javax.ws.rs.POST; |
| 14 | +import javax.ws.rs.Path; |
| 15 | +import javax.ws.rs.core.Response; |
| 16 | + |
| 17 | +import static i5.las2peer.services.apiTestingBot.context.TestModelingState.*; |
| 18 | + |
| 19 | +@Api |
| 20 | +@Path("/") |
| 21 | +public class RESTResources { |
| 22 | + |
| 23 | + @GET |
| 24 | + @Path("/") |
| 25 | + public Response get() { |
| 26 | + return Response.status(200).entity("API-Testing-Bot service is running!").build(); |
| 27 | + } |
| 28 | + |
| 29 | + @POST |
| 30 | + @Path("/test/model") |
| 31 | + public Response modelTest(String body) { |
| 32 | + JSONObject bodyJSON = (JSONObject) JSONValue.parse(body); |
| 33 | + String intent = (String) bodyJSON.get("intent"); |
| 34 | + String message = (String) bodyJSON.get("msg"); |
| 35 | + String channel = (String) bodyJSON.get("channel"); |
| 36 | + |
| 37 | + // get current modeling context for this channel |
| 38 | + TestModelingContext context; |
| 39 | + if(!APITestingBot.channelModelingContexts.containsKey(channel)) APITestingBot.channelModelingContexts.put(channel, new TestModelingContext()); |
| 40 | + context = APITestingBot.channelModelingContexts.get(channel); |
| 41 | + |
| 42 | + // get the initial state of the context (at the beginning of this execution) |
| 43 | + TestModelingState initialState = context.getState(); |
| 44 | + |
| 45 | + boolean handleNextState = false; |
| 46 | + |
| 47 | + StringBuilder responseMessageSB = new StringBuilder(); |
| 48 | + APITestingBot service = (APITestingBot) Context.get().getService(); |
| 49 | + MessageHandler messageHandler = new MessageHandler(service.getCaeBackendURL()); |
| 50 | + |
| 51 | + do { |
| 52 | + if (initialState == INIT && intent.equals(Intent.MODEL_TEST)) { |
| 53 | + handleNextState = messageHandler.handleInit(responseMessageSB, context, channel); |
| 54 | + } |
| 55 | + |
| 56 | + if ((handleNextState && context.getState() == SELECT_PROJECT) || initialState == SELECT_PROJECT) { |
| 57 | + handleNextState = messageHandler.handleProjectSelection(responseMessageSB, context, message); |
| 58 | + } |
| 59 | + |
| 60 | + if ((handleNextState && context.getState() == SELECT_MICROSERVICE) || initialState == SELECT_MICROSERVICE) { |
| 61 | + handleNextState = messageHandler.handleMicroserviceSelection(responseMessageSB, context, message); |
| 62 | + } |
| 63 | + |
| 64 | + if (initialState == NAME_TEST_CASE) { |
| 65 | + handleNextState = messageHandler.handleTestCaseName(responseMessageSB, context, message); |
| 66 | + } |
| 67 | + |
| 68 | + if (initialState == SELECT_METHOD) { |
| 69 | + handleNextState = messageHandler.handleMethodSelection(responseMessageSB, context, message); |
| 70 | + } |
| 71 | + |
| 72 | + if ((handleNextState && context.getState() == ENTER_PATH_PARAMS) || initialState == ENTER_PATH_PARAMS) { |
| 73 | + handleNextState = messageHandler.handlePathParams(responseMessageSB, context, message); |
| 74 | + } |
| 75 | + |
| 76 | + if ((handleNextState && context.getState() == BODY_QUESTION)) { |
| 77 | + handleNextState = messageHandler.handleBodyQuestion(responseMessageSB); |
| 78 | + } |
| 79 | + |
| 80 | + if (!handleNextState && initialState == BODY_QUESTION) { |
| 81 | + handleNextState = messageHandler.handleBodyQuestionAnswer(responseMessageSB, context, intent); |
| 82 | + } |
| 83 | + |
| 84 | + if (initialState == ENTER_BODY) { |
| 85 | + handleNextState = messageHandler.handleBody(responseMessageSB, context, message); |
| 86 | + } |
| 87 | + |
| 88 | + if (handleNextState && context.getState() == ASSERTIONS_QUESTION) { |
| 89 | + handleNextState = messageHandler.handleAssertionsQuestion(responseMessageSB); |
| 90 | + } |
| 91 | + |
| 92 | + if (initialState == ASSERTIONS_QUESTION) { |
| 93 | + handleNextState = messageHandler.handleAssertionsQuestionAnswer(responseMessageSB, context, intent); |
| 94 | + } |
| 95 | + |
| 96 | + if (handleNextState && context.getState() == ASSERTION_TYPE_QUESTION) { |
| 97 | + handleNextState = messageHandler.handleAssertionTypeQuestion(responseMessageSB); |
| 98 | + } |
| 99 | + |
| 100 | + if (initialState == ASSERTION_TYPE_QUESTION) { |
| 101 | + handleNextState = messageHandler.handleAssertionTypeQuestionAnswer(responseMessageSB, context, message); |
| 102 | + } |
| 103 | + |
| 104 | + if (handleNextState && context.getState() == ENTER_STATUS_CODE) { |
| 105 | + handleNextState = messageHandler.handleStatusCodeQuestion(responseMessageSB); |
| 106 | + } |
| 107 | + |
| 108 | + if (initialState == ENTER_STATUS_CODE) { |
| 109 | + handleNextState = messageHandler.handleStatusCodeInput(responseMessageSB, context, message); |
| 110 | + } |
| 111 | + |
| 112 | + if (handleNextState && context.getState() == ASSERTIONS_OVERVIEW) { |
| 113 | + handleNextState = messageHandler.handleAssertionsOverview(responseMessageSB, context); |
| 114 | + } |
| 115 | + |
| 116 | + if (handleNextState && context.getState() == ADD_ANOTHER_ASSERTION_QUESTION) { |
| 117 | + handleNextState = messageHandler.handleAddAssertionQuestion(responseMessageSB); |
| 118 | + } |
| 119 | + |
| 120 | + if (initialState == ADD_ANOTHER_ASSERTION_QUESTION) { |
| 121 | + handleNextState = messageHandler.handleAddAssertionQuestionAnswer(responseMessageSB, context, intent); |
| 122 | + } |
| 123 | + |
| 124 | + if(handleNextState && context.getState() == BODY_ASSERTION_TYPE_QUESTION) { |
| 125 | + handleNextState = messageHandler.handleBodyAssertionTypeQuestion(responseMessageSB); |
| 126 | + } |
| 127 | + |
| 128 | + if(initialState == BODY_ASSERTION_TYPE_QUESTION) { |
| 129 | + handleNextState = messageHandler.handleBodyAssertionTypeInput(responseMessageSB, context, message); |
| 130 | + } |
| 131 | + |
| 132 | + if(handleNextState && context.getState() == ENTER_BODY_ASSERTION_PART) { |
| 133 | + handleNextState = messageHandler.handleBodyAssertionPartQuestion(responseMessageSB, context); |
| 134 | + } |
| 135 | + |
| 136 | + if(initialState == ENTER_BODY_ASSERTION_PART) { |
| 137 | + handleNextState = messageHandler.handleBodyAssertionPartInput(responseMessageSB, context, message, intent); |
| 138 | + } |
| 139 | + |
| 140 | + if(handleNextState && context.getState() == END_OF_BODY_ASSERTION_QUESTION) { |
| 141 | + handleNextState = messageHandler.handleEndOfBodyAssertionQuestion(responseMessageSB, context); |
| 142 | + } |
| 143 | + |
| 144 | + if(initialState == END_OF_BODY_ASSERTION_QUESTION) { |
| 145 | + handleNextState = messageHandler.handleEndOfBodyAssertionQuestionAnswer(responseMessageSB, context, intent); |
| 146 | + } |
| 147 | + |
| 148 | + // set initial state to final (otherwise problems occur in the next loop iterations) |
| 149 | + initialState = FINAL; |
| 150 | + |
| 151 | + } while(handleNextState); |
| 152 | + String responseMessage = responseMessageSB.toString(); |
| 153 | + |
| 154 | + if(responseMessage.isEmpty()) responseMessage = "Error!"; |
| 155 | + |
| 156 | + System.out.println("New state is: " + APITestingBot.channelModelingContexts.get(channel).getState().name()); |
| 157 | + |
| 158 | + JSONObject res = new JSONObject(); |
| 159 | + res.put("text", responseMessage); |
| 160 | + res.put("closeContext", context.getState() == FINAL); |
| 161 | + |
| 162 | + if(context.getState() == FINAL) { |
| 163 | + APITestingBot.channelModelingContexts.remove(channel); |
| 164 | + } |
| 165 | + |
| 166 | + return Response.status(200).entity(res.toJSONString()).build(); |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | +} |
0 commit comments