@Before
  public void initJNDI() throws Exception {
    System.out.println("init [TestFlow]");
    this.setUp();
    // Create initial context
    System.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    final InitialContext ic = new InitialContext();

    try {
      ic.destroySubcontext("java:");
    } catch (final Exception e) {
    }

    ic.createSubcontext("java:");
    ic.createSubcontext("java:/comp");
    ic.createSubcontext("java:/comp/env");
    ic.createSubcontext("java:/comp/env/jdbc");
    ic.createSubcontext("java:/comp/env/cfg");
    ic.createSubcontext("java:/comp/env/cfg/Source");
    ic.createSubcontext("java:/comp/env/cfg/ConnectionParameters");

    final ConnectionParameters cp = new ConnectionParameters();
    ic.bind("java:/comp/env/cfg/ConnectionParameters/attributes", cp);
    ic.bind("java:/comp/env/cfg/ConnectionParameters/authentication", cp);

    ic.bind("java:/comp/env/cfg/Source/attributes", "FAKE");
    ic.bind("java:/comp/env/cfg/Source/authentication", "FAKE");
    ic.bind("java:/comp/env/cfg/bindQuery", "FAKE");
    ic.bind("java:/comp/env/cfg/restAuthPassword", "");
    ic.bind("java:/comp/env/cfg/restAuthUser", "flow");
    ic.bind("java:/comp/env/cfg/issuanceServiceURL", getBaseURI() + "issuance/");
    ic.bind("java:/comp/env/cfg/userServiceURL", getBaseURI() + "user/");
    ic.bind("java:/comp/env/cfg/verificationServiceURL", getBaseURI() + "verification/");
    ic.bind("java:/comp/env/cfg/verifierIdentity", "unknown");

    final SQLiteDataSource ds = new SQLiteDataSource();

    storageFile = File.createTempFile("test", "sql");

    ds.setUrl("jdbc:sqlite:" + storageFile.getPath());
    System.out.println(ds.getUrl());
    ic.rebind("java:/comp/env/jdbc/" + dbName, ds);
    ic.bind("java:/comp/env/cfg/useDbLocking", new Boolean(true));

    ic.close();

    RESTHelper.postRequest(issuanceServiceURL + "reset");
    RESTHelper.postRequest(verificationServiceURL + "reset");
    RESTHelper.postRequest(userServiceURL + "reset");

    // System.exit(1);
  }
  /**
   * Performs the whole more or less adopted from the ancient tutorial.
   *
   * <p>Please don't change the order of the calls as it is relevant! To run this test you have to:
   * - have the services running (on tomcat) - have write access to the location where the STORAGE
   * resides - the STORAGE must be EMPTIED/CLEARED before running this. - the configuration needs to
   * be set-up to use FAKE identity and FAKE attribute source (because we check against hardcoded
   * values used in the Fake*Providers. (Please don't change the values in Fake*Providers without
   * reflecting the changes here and vice versa)).
   *
   * <p>Notes: This integration test tests the whole flow from setup to generation of
   * CredentialSpecification to IssuanceRequest to Verification. However, this test does not check
   * any intermediate results (other than ensuring that the webservices responded with the correct
   * status code) because this test assumes that if the final Verification process succeeds, the
   * test was successful. In other words: This test will obtain a Credential from the Issuance
   * service and verifies the obtained Credential against a PresentationPolicy at the Verification
   * service.
   *
   * @throws UnsupportedEncodingException
   * @throws InterruptedException
   * @throws JAXBException
   * @throws NamingException
   * @throws UniformInterfaceException
   * @throws ClientHandlerException
   */
  @Test
  public void flowTest()
      throws UnsupportedEncodingException, InterruptedException, JAXBException,
          ClientHandlerException, UniformInterfaceException, NamingException {
    System.out.println("hi there");

    /*
     * Test if all three services are running by calling /status/ on each
     * service and expecting a 200 response.
     */
    testUserStatus();
    testIssuanceStatus();
    testVerificationStatus();

    /*
     * Ok, if we are here all services are at least running
     */

    /* Test authentication */
    testAuthentication(readTextFile("/simpleAuth.xml"));

    /*
     * Get an attributeInfoCollection and convert it to a
     * credentialSpecification
     */
    final String attributeInfoCollection = testAttributeInfoCollection();
    final String credSpec = testGenCredSpec(attributeInfoCollection);

    /* Store/Get credentialSpecification at issuer */
    testStoreCredSpecAtIssuer(credSpec);
    testGetCredSpecFromIssuer();

    /* Store/Get queryRule at issuer */
    testStoreQueryRuleAtIssuer(readTextFile("/queryRule.xml"));
    testGetQueryRuleFromIssuer();

    /* Store/Get IssuancePolicy at issuer */
    testStoreIssuancePolicyAtIssuer(readTextFile("/issuancePolicy.xml"));
    testGetIssuancePolicyFromIssuer();

    /*
     * Ok, if we are here the first phase of setup is done.
     */

    /* Generate the SystemParameters */
    final String systemParameters = testSetupSystemParametersIssuer();

    /* Store CredentialSpecification at User and Verifier */
    testStoreCredSpecAtUser(credSpec);
    testStoreCredSpecAtVerifier(credSpec);

    /* Store SystemParameters at User and Verifier */
    testStoreSysParamsAtUser(systemParameters);
    testStoreSysParamsAtVerifier(systemParameters);

    /* Setup IssuerParameters */
    final String issuerParameters =
        testSetupIssuerParametersIssuer(readTextFile("/issuerParametersInput.xml"));
    System.out.println("--- issuerParameters");
    System.out.println(issuerParameters);

    /* Store IssuerParameters at User and Verifier */
    testStoreIssParamsAtUser(issuerParameters);
    testStoreIssParamsAtVerifier(issuerParameters);

    /*
     * Ok, phase two of setup is done (which means setup is done). Now the
     * actual issuance protocol can take place.
     */

    for (int i = 0; i < 3; i++) {
      final String issuanceMessageAndBoolean =
          testIssuanceRequest(readTextFile("/issuanceRequest.xml"));

      /* Extract issuance message */
      final String firstIssuanceMessage = testExtractIssuanceMessage(issuanceMessageAndBoolean);
      System.out.println("--- firstIssuanceMessage");
      System.out.println(firstIssuanceMessage);

      /* Issuance steps in the protocol */
      final String issuanceReturn = testIssuanceStepUser1(firstIssuanceMessage);
      String contextString = getContextString(issuanceReturn);
      System.out.println("--- issuanceReturn");
      System.out.println(issuanceReturn);
      System.out.println(contextString);

      String uiIssuanceReturn = readTextFile("/uiIssuanceReturn.xml");
      uiIssuanceReturn = replaceContextString(uiIssuanceReturn, contextString);
      System.out.println("--- uiIssuanceReturn");
      System.out.println(uiIssuanceReturn);

      final String secondIssuanceMessage = testIssuanceStepUserUi1(uiIssuanceReturn);
      System.out.println("--- secondIssuanceMessage");
      System.out.println(secondIssuanceMessage);

      final String thirdIssuanceMessageAndBoolean = testIssuanceStepIssuer1(secondIssuanceMessage);
      final String thirdIssuanceMessage =
          testExtractIssuanceMessage(thirdIssuanceMessageAndBoolean);

      @SuppressWarnings("unused")
      final String fourthIssuanceMessageAndBoolean = testIssuanceStepUser2(thirdIssuanceMessage);

      /* Verification stuff */
      final String presentationPolicyAlternatives =
          testCreatePresentationPolicy(readTextFile("/presentationPolicyAlternatives.xml"));
      testCreatePresentationPolicy(readTextFile("/presentationPolicyAlternatives.xml"));

      final String presentationReturn = testCreatePresentationToken(presentationPolicyAlternatives);
      contextString = getContextString(presentationReturn);
      System.out.println(contextString);

      String uiPresentationReturn = readTextFile("/uiPresentationReturn.xml");
      uiPresentationReturn = replaceContextString(uiPresentationReturn, contextString);

      final String presentationToken = testCreatePresentationTokenUi(uiPresentationReturn);

      final String rPresentationToken = presentationToken.replaceAll("<\\?xml(.*)\\?>", "");
      final String rPresentationPolicyAlternatives =
          presentationPolicyAlternatives.replaceAll("<\\?xml(.*)\\?>", "");

      String ppapt = "";
      ppapt += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
      ppapt +=
          "<PresentationPolicyAlternativesAndPresentationToken xmlns=\"http://abc4trust.eu/wp2/abcschemav1.0\" Version=\"1.0\">";
      ppapt += rPresentationPolicyAlternatives;
      ppapt += rPresentationToken;
      ppapt += "</PresentationPolicyAlternativesAndPresentationToken>";

      final String presentationTokenDescription = testVerifyTokenAgainstPolicy(ppapt);
      System.out.println(presentationTokenDescription);

      /* Verification stuff 2 */
      System.out.println("***********");
      System.out.println("***********");
      System.out.println("***********");
      System.out.println("***********");
      System.out.println("***********");

      testStorePresentationPolicyAlternatives(presentationPolicyAlternatives);

      System.out.println("!!!!!!!");

      for (int j = 0; j < 3; j++) {
        testStoreRedirectURI(
            "http://srv-lab-t-425.zhaw.ch:8080/zhaw-p2abc-webservices/demo-resource/page");
        final String presentationPolicyAlternatives_ = testRequestResource();

        final PresentationPolicyAlternatives ppa =
            (PresentationPolicyAlternatives)
                RESTHelper.fromXML(
                    PresentationPolicyAlternatives.class, presentationPolicyAlternatives_);

        final ApplicationData apd =
            ppa.getPresentationPolicy().get(0).getMessage().getApplicationData();
        System.out.println("APD: " + apd.getContent().get(0));

        final String presentationReturn_ =
            testCreatePresentationToken(presentationPolicyAlternatives_);
        final String contextString_ = getContextString(presentationReturn_);
        System.out.println(contextString_);

        String uiPresentationReturn_ = readTextFile("/uiPresentationReturn.xml");
        uiPresentationReturn_ = replaceContextString(uiPresentationReturn_, contextString_);

        final String presentationToken_ = testCreatePresentationTokenUi(uiPresentationReturn_);
        /*
         * PresentationToken presentationToken2 = (PresentationToken)
         * RESTHelper.fromXML(PresentationToken.class,
         * presentationToken_); System.out.println(";VI 0 is " +
         * presentationToken2
         * .getPresentationTokenDescription().getMessage
         * ().getVerifierIdentity().getContent().get(0));
         * presentationToken2
         * .getPresentationTokenDescription().getMessage
         * ().getVerifierIdentity().getContent().clear();
         * presentationToken2
         * .getPresentationTokenDescription().getMessage
         * ().getVerifierIdentity().getContent().add("urn:verifier:1");
         * presentationToken_ =
         * RESTHelper.toXML(PresentationToken.class,
         * of.createPresentationToken(presentationToken2));
         */

        final String presentationTokenDescription_ = testRequestResource2(presentationToken_);
        System.out.println("**#*#*#*#*#**#*#");
        System.out.println(presentationTokenDescription_);
      }

      testLoadSettingsVerification();
      testLoadSettingsUser();

      System.gc();
    }

    /* Test user credentials */
    CredentialCollection credCol =
        (CredentialCollection)
            RESTHelper.getRequest(userServiceURL + "credential/list", CredentialCollection.class);
    System.out.println("Found " + credCol.credentials.size() + " credentials!");
    List<Credential> creds = credCol.credentials;
    assertEquals(creds.size(), 3);
    for (final Credential c : creds) {
      String credUID = c.getCredentialDescription().getCredentialUID().toString();
      System.out.println(credUID);
      credUID = credUID.split("/")[1];
      final Credential c1 =
          (Credential)
              RESTHelper.getRequest(userServiceURL + "credential/get/" + credUID, Credential.class);
      assertEquals(
          c1.getCredentialDescription().getCredentialUID().toString().endsWith(credUID), true);
      RESTHelper.deleteRequest(userServiceURL + "credential/delete/" + credUID);
    }

    credCol =
        (CredentialCollection)
            RESTHelper.getRequest(userServiceURL + "credential/list", CredentialCollection.class);
    System.out.println("Found " + credCol.credentials.size() + " credentials!");
    creds = credCol.credentials;
    assertEquals(creds.size(), 0);

    // while(true) {
    // Thread.sleep(100);
    // }
  }