public static IConversationMemory convertConversationMemorySnapshot(ConversationMemorySnapshot snapshot) { ConversationMemory conversationMemory = new ConversationMemory(snapshot.getConversationId(), snapshot.getBotId(), snapshot.getBotVersion(), snapshot.getUserId()); conversationMemory.setConversationState(snapshot.getConversationState()); conversationMemory.getConversationProperties().putAll(snapshot.getConversationProperties()); List<IConversationMemory.IConversationStep> redoSteps = iterateRedoCache(snapshot.getRedoCache()); for (IConversationMemory.IConversationStep redoStep : redoSteps) { conversationMemory.getRedoCache().add(redoStep); } List<ConversationMemorySnapshot.ConversationStepSnapshot> conversationSteps = snapshot.getConversationSteps(); for (int i = 0; i < conversationSteps.size(); i++) { ConversationOutput conversationOutput = snapshot.getConversationOutputs().get(i); ConversationMemorySnapshot.ConversationStepSnapshot conversationStepSnapshot = conversationSteps.get(i); if (i > 0) { conversationMemory.startNextStep(conversationOutput); } for (ConversationMemorySnapshot.PackageRunSnapshot packageRunSnapshot : conversationStepSnapshot.getPackages()) { for (ConversationMemorySnapshot.ResultSnapshot resultSnapshot : packageRunSnapshot.getLifecycleTasks()) { Data data = new Data(resultSnapshot.getKey(), resultSnapshot.getResult(), resultSnapshot.getPossibleResults(), resultSnapshot.getTimestamp(), resultSnapshot.isPublic()); conversationMemory.getCurrentStep().storeData(data); } } } return conversationMemory; }
public static ConversationMemorySnapshot convertConversationMemory(IConversationMemory conversationMemory) { ConversationMemorySnapshot snapshot = new ConversationMemorySnapshot(); if (conversationMemory.getUserId() != null) { snapshot.setUserId(conversationMemory.getUserId()); } if (conversationMemory.getConversationId() != null) { snapshot.setConversationId(conversationMemory.getConversationId()); } snapshot.setBotId(conversationMemory.getBotId()); snapshot.setBotVersion(conversationMemory.getBotVersion()); snapshot.setConversationState(conversationMemory.getConversationState()); for (IConversationMemory.IConversationStep redoStep : conversationMemory.getRedoCache()) { ConversationMemorySnapshot.ConversationStepSnapshot redoStepSnapshot = iterateConversationStep(redoStep); snapshot.getRedoCache().push(redoStepSnapshot); } for (int i = conversationMemory.getAllSteps().size() - 1; i >= 0; i--) { IConversationMemory.IConversationStep conversationStep = conversationMemory.getAllSteps().get(i); snapshot.getConversationSteps().add(iterateConversationStep(conversationStep)); } snapshot.getConversationOutputs().addAll(conversationMemory.getConversationOutputs()); snapshot.getConversationProperties().putAll(conversationMemory.getConversationProperties()); return snapshot; }
if (!botId.equals(memorySnapshot.getBotId())) { continue; if (!botVersion.equals(memorySnapshot.getBotVersion())) { continue; if (!conversationState.equals(memorySnapshot.getConversationState())) { continue; conversationDescriptor.setEnvironment(memorySnapshot.getEnvironment()); conversationDescriptor.setConversationStepSize(memorySnapshot.getConversationSteps().size()); URI createdBy = conversationDescriptor.getCreatedBy(); if (createdBy != null) { conversationDescriptor.setCreatedByUserName(user.getDisplayName()); conversationDescriptor.setBotName(documentDescriptorStore.readDescriptor(memorySnapshot.getBotId(), memorySnapshot.getBotVersion()).getName()); conversationDescriptor.setConversationState(memorySnapshot.getConversationState());
@Override public Response createTestCase(String id) { try { ConversationMemorySnapshot conversationMemorySnapshot = conversationMemoryStore.loadConversationMemorySnapshot(id); TestCase testCase = new TestCase(); testCase.setBotId(conversationMemorySnapshot.getBotId()); testCase.setBotVersion(conversationMemorySnapshot.getBotVersion()); testCase.setTestCaseState(TestCaseState.SUCCESS); testCase.setActual(conversationMemorySnapshot); testCase.setExpected(conversationMemorySnapshot); IResourceStore.IResourceId resourceId = testCaseStore.create(testCase); URI createdUri = RestUtilities.createURI(resourceURI, resourceId.getId(), versionQueryParam, 0); return Response.created(createdUri).build(); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e); } catch (IResourceStore.ResourceNotFoundException e) { log.debug(e.getLocalizedMessage(), e); throw new NoLogWebApplicationException(Response.Status.NOT_FOUND); } }
private ConversationMemorySnapshot runTestCase(String botId, TestCase testCase) throws Exception { IRestBotEngine botEngine = restInterfaceFactory.get(IRestBotEngine.class, apiServerURI); Response ConversationResponse = botEngine.startConversation(Deployment.Environment.test, botId, "testCaseRunner"); URI conversationURI = ConversationResponse.getLocation(); String conversationURIPath = conversationURI.getPath(); String conversationId = conversationURIPath.substring(conversationURIPath.lastIndexOf("/") + 1); ConversationMemorySnapshot expected = testCase.getExpected(); List<ConversationMemorySnapshot.ConversationStepSnapshot> expectedConversationSteps = expected.getConversationSteps(); //we skip the first one, since the initial run has already been done at this point (at startConversation) for (int i = 1; i < expectedConversationSteps.size(); i++) { ConversationMemorySnapshot.ConversationStepSnapshot expectedConversationStep = expectedConversationSteps.get(i); String input = getFirstInput(expectedConversationStep); if (RuntimeUtilities.isNullOrEmpty(input)) { input = " "; } botEngine.say(Deployment.Environment.test, botId, conversationId, true, false, Collections.emptyList(), input, new MockAsyncResponse()); while (botEngine.getConversationState(Deployment.Environment.test, conversationId) == ConversationState.IN_PROGRESS) { Thread.sleep(1000); } } return conversationMemoryStore.loadConversationMemorySnapshot(conversationId); }
@Override public SimpleConversationMemorySnapshot readConversation(Deployment.Environment environment, String botId, String conversationId, Boolean returnDetailed, Boolean returnCurrentStepOnly, List<String> returningFields) { RuntimeUtilities.checkNotNull(environment, "environment"); RuntimeUtilities.checkNotNull(botId, "botId"); RuntimeUtilities.checkNotNull(conversationId, "conversationId"); try { var conversationMemorySnapshot = conversationMemoryStore.loadConversationMemorySnapshot(conversationId); if (!botId.equals(conversationMemorySnapshot.getBotId())) { String message = "conversationId: '%s' does not belong to bot with conversationId: '%s'. " + "(provided botId='%s', botId in ConversationMemory='%s')"; message = String.format(message, conversationId, botId, botId, conversationMemorySnapshot.getBotId()); throw new IllegalAccessException(message); } return getSimpleConversationMemorySnapshot(conversationMemorySnapshot, returnDetailed, returnCurrentStepOnly, returningFields); } catch (ResourceStoreException | IllegalAccessException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e.getLocalizedMessage(), e); } catch (ResourceNotFoundException e) { throw new NoLogWebApplicationException(Response.Status.NOT_FOUND); } }
public void executeTestCase(final String id, final TestCase testCase) { SystemRuntime.getRuntime().submitCallable((Callable<Void>) () -> { try { testCaseStore.setTestCaseState(id, TestCaseState.IN_PROGRESS); if (!isBotDeployed(testCase.getBotId(), testCase.getBotVersion())) { deployBot(testCase.getBotId(), testCase.getBotVersion()); } ConversationMemorySnapshot actual = runTestCase(testCase.getBotId(), testCase); testCase.setActual(actual); testCase.setLastRun(new Date(System.currentTimeMillis())); testCase.setTestCaseState(testCase.getExpected().equals(testCase.getActual()) ? TestCaseState.SUCCESS : TestCaseState.FAILED); testCaseStore.storeTestCase(id, testCase); } catch (Exception e) { testCaseStore.setTestCaseState(id, TestCaseState.ERROR); throw e; } return null; }, ThreadContext.getResources()); }
private void mergeConversationMemory(IConversationMemory currentConversationMemory, ConversationMemorySnapshot callbackMemorySnapshot) { if (callbackMemorySnapshot != null && !callbackMemorySnapshot.getConversationSteps().isEmpty()) { var currentStep = currentConversationMemory.getCurrentStep(); var callbackConversationMemory = convertConversationMemorySnapshot(callbackMemorySnapshot); var currentCallbackStep = callbackConversationMemory.getCurrentStep(); Set<String> callbackKeys = currentCallbackStep.getAllKeys(); for (String callbackKey : callbackKeys) { currentStep.storeData(currentCallbackStep.getData(callbackKey)); } } }
public static SimpleConversationMemorySnapshot convertSimpleConversationMemory(ConversationMemorySnapshot conversationMemorySnapshot, boolean returnDetailed) { SimpleConversationMemorySnapshot simpleSnapshot = new SimpleConversationMemorySnapshot(); if (conversationMemorySnapshot.getUserId() != null) { simpleSnapshot.setUserId(conversationMemorySnapshot.getUserId()); } simpleSnapshot.setBotId(conversationMemorySnapshot.getBotId()); simpleSnapshot.setBotVersion(conversationMemorySnapshot.getBotVersion()); simpleSnapshot.setConversationState(conversationMemorySnapshot.getConversationState()); simpleSnapshot.setEnvironment(conversationMemorySnapshot.getEnvironment()); simpleSnapshot.setRedoCacheSize(conversationMemorySnapshot.getRedoCache().size()); simpleSnapshot.getConversationOutputs().addAll(conversationMemorySnapshot.getConversationOutputs()); simpleSnapshot.getConversationProperties().putAll(conversationMemorySnapshot.getConversationProperties()); for (ConversationMemorySnapshot.ConversationStepSnapshot conversationStepSnapshot : conversationMemorySnapshot.getConversationSteps()) { SimpleConversationMemorySnapshot.SimpleConversationStep simpleConversationStep = new SimpleConversationMemorySnapshot.SimpleConversationStep(); simpleSnapshot.getConversationSteps().add(simpleConversationStep); for (ConversationMemorySnapshot.PackageRunSnapshot packageRunSnapshot : conversationStepSnapshot.getPackages()) { for (ConversationMemorySnapshot.ResultSnapshot resultSnapshot : packageRunSnapshot.getLifecycleTasks()) { if (returnDetailed || resultSnapshot.isPublic()) { Object result = resultSnapshot.getResult(); simpleConversationStep.getConversationStep().add( new SimpleConversationMemorySnapshot.ConversationStepData(resultSnapshot.getKey(), result)); } else { continue; } simpleConversationStep.setTimestamp(resultSnapshot.getTimestamp()); } } } return simpleSnapshot; }