/**
   * Attempts to create a fresh {@link TestSessionState} wrapped into a {@link
   * TestSessionController} for the given {@link Delivery}.
   *
   * <p>This will return null if the test can't be started because its {@link TestProcessingMap}
   * can't be created, e.g. if its XML can't be parsed.
   */
  public TestSessionController createNewTestSessionStateAndController(
      final User candidate,
      final Delivery delivery,
      final NotificationRecorder notificationRecorder) {
    ensureTestDelivery(delivery);

    /* Resolve the underlying JQTI+ object */
    final AssessmentPackage assessmentPackage =
        assessmentDataService.ensureSelectedAssessmentPackage(delivery);
    final TestProcessingMap testProcessingMap =
        assessmentObjectManagementService.getTestProcessingMap(assessmentPackage);
    if (testProcessingMap == null) {
      return null;
    }

    /* Generate a test plan for this session */
    final TestPlanner testPlanner = new TestPlanner(testProcessingMap);
    if (notificationRecorder != null) {
      testPlanner.addNotificationListener(notificationRecorder);
    }
    final TestPlan testPlan = testPlanner.generateTestPlan();

    /* Create fresh state for session */
    final TestSessionState testSessionState = new TestSessionState(testPlan);

    /* Create config for TestSessionController */
    final DeliverySettings testDeliverySettings =
        assessmentDataService.getEffectiveDeliverySettings(candidate, delivery);
    final TestSessionControllerSettings testSessionControllerSettings =
        new TestSessionControllerSettings();
    testSessionControllerSettings.setTemplateProcessingLimit(
        computeTemplateProcessingLimit(testDeliverySettings));

    /* Create controller and wire up notification recorder */
    final TestSessionController result =
        new TestSessionController(
            jqtiExtensionManager,
            testSessionControllerSettings,
            testProcessingMap,
            testSessionState);
    if (notificationRecorder != null) {
      result.addNotificationListener(notificationRecorder);
    }
    return result;
  }
 public AssessmentResult computeTestAssessmentResult(
     final CandidateSession candidateSession, final TestSessionController testSessionController) {
   final URI sessionIdentifierSourceId = URI.create(qtiWorksDeploymentSettings.getBaseUrl());
   final String sessionIdentifier = "testsession/" + candidateSession.getId();
   return testSessionController.computeAssessmentResult(
       requestTimestampContext.getCurrentRequestTimestamp(),
       sessionIdentifier,
       sessionIdentifierSourceId);
 }
  /**
   * Wraps the given {@link TestSessionState} in a {@link TestSessionController}.
   *
   * <p>It is assumed that the test was runnable, so this will never return null.
   */
  public TestSessionController createTestSessionController(
      final CandidateSession candidateSession,
      final TestSessionState testSessionState,
      final NotificationRecorder notificationRecorder) {
    final User candidate = candidateSession.getCandidate();
    final Delivery delivery = candidateSession.getDelivery();
    ensureTestDelivery(delivery);
    Assert.notNull(testSessionState, "testSessionState");

    /* Try to resolve the underlying JQTI+ object */
    final AssessmentPackage assessmentPackage =
        assessmentDataService.ensureSelectedAssessmentPackage(delivery);
    final TestProcessingMap testProcessingMap =
        assessmentObjectManagementService.getTestProcessingMap(assessmentPackage);
    if (testProcessingMap == null) {
      return null;
    }

    /* Create config for TestSessionController */
    final TestDeliverySettings testDeliverySettings =
        (TestDeliverySettings)
            assessmentDataService.getEffectiveDeliverySettings(candidate, delivery);
    final TestSessionControllerSettings testSessionControllerSettings =
        new TestSessionControllerSettings();
    testSessionControllerSettings.setTemplateProcessingLimit(
        computeTemplateProcessingLimit(testDeliverySettings));

    /* Create controller and wire up notification recorder (if passed) */
    final TestSessionController result =
        new TestSessionController(
            jqtiExtensionManager,
            testSessionControllerSettings,
            testProcessingMap,
            testSessionState);
    if (notificationRecorder != null) {
      result.addNotificationListener(notificationRecorder);
    }

    return result;
  }
  public static void main(final String[] args) {
    /* We'll be loading the following sample test from the classpath */
    final ClassPathResourceLocator assessmentResourceLocator = new ClassPathResourceLocator();
    final URI testUri =
        URI.create(
            "classpath:/uk/ac/ed/ph/qtiworks/samples/testimplementation/dave/test-testFeedback.xml");

    /* Read and set up state & controller */
    final TestSessionController testSessionController =
        RenderingExampleHelpers.createTestSessionController(assessmentResourceLocator, testUri);
    final TestSessionState testSessionState = testSessionController.getTestSessionState();

    /* Enter test */
    System.out.println("\nInitialising");
    final Date timestamp1 = new Date();
    testSessionController.initialize(timestamp1);
    testSessionController.enterTest(timestamp1);

    /* Enter testPart */
    final Date timestamp2 = ObjectUtilities.addToTime(timestamp1, 1000L);
    testSessionController.enterNextAvailableTestPart(timestamp2);

    /* Select first item */
    final Date timestamp3 = ObjectUtilities.addToTime(timestamp1, 5000L);
    final TestPlanNode firstItemRef =
        testSessionState.getTestPlan().searchNodes(TestNodeType.ASSESSMENT_ITEM_REF).get(0);
    testSessionController.selectItemNonlinear(timestamp3, firstItemRef.getKey());

    /* Create rendering request */
    final TestRenderingOptions renderingOptions =
        RenderingExampleHelpers.createTestRenderingOptions();
    final TestRenderingRequest renderingRequest = new TestRenderingRequest();
    renderingRequest.setTestSessionController(testSessionController);
    renderingRequest.setAssessmentResourceLocator(assessmentResourceLocator);
    renderingRequest.setAssessmentResourceUri(testUri);
    renderingRequest.setTestSessionController(testSessionController);
    renderingRequest.setRenderingOptions(renderingOptions);
    renderingRequest.setAuthorMode(true);
    renderingRequest.setTestRenderingMode(null);

    /* Set up result */
    final StringBuilderWriter stringBuilderWriter = new StringBuilderWriter();
    final StreamResult result = new StreamResult(stringBuilderWriter);

    System.out.println("\nRendering");
    final AssessmentRenderer renderer = RenderingExampleHelpers.createAssessmentRenderer();
    renderer.renderTest(renderingRequest, null /* (=Ignore notifications) */, result);
    final String rendered = stringBuilderWriter.toString();
    System.out.println("Rendered HTML: " + rendered);
  }