@Test
  public void all_organisations_returned() {
    databaseHelper.addObject(
        "mntner: TEST-MNT\nmnt-by:TEST-MNT\nauth: SSO [email protected]\nsource:test");
    final RpslObject org1 =
        RpslObject.parse(
            "" + "organisation: ORG-TST1-TEST\n" + "mnt-by: TEST-MNT\n" + "source: TEST");
    databaseHelper.addObject(org1);
    final RpslObject org2 =
        RpslObject.parse(
            "" + "organisation: ORG-MD5-TEST\n" + "mnt-by: TEST-MNT\n" + "source: TEST");
    databaseHelper.addObject(org2);

    final WhoisResources result =
        RestTest.target(
                getPort(),
                "api/user/ed7cd420-6402-11e3-949a-0800200c9a66/organisations",
                null,
                apiKey)
            .request(MediaType.APPLICATION_JSON_TYPE)
            .get(WhoisResources.class);

    final List<WhoisObject> whoisObjects = result.getWhoisObjects();
    assertThat(whoisObjects, hasSize(2));

    assertThat(
        whoisObjectMapper.map(whoisObjects.get(0), FormattedClientAttributeMapper.class), is(org2));
    assertThat(
        whoisObjectMapper.map(whoisObjects.get(1), FormattedClientAttributeMapper.class), is(org1));
  }
  @Test
  public void only_organisations_returned() {
    databaseHelper.addObject("mntner: TEST-MNT\nmnt-by:TEST-MNT\nauth: SSO [email protected]");
    final RpslObject organisation =
        RpslObject.parse(
            ""
                + "organisation: ORG-TST-TEST\n"
                + "mnt-ref: TEST-MNT\n"
                + "mnt-by: TEST-MNT\n"
                + "source: TEST");
    databaseHelper.addObject(organisation);
    databaseHelper.addObject("person: Test Person\nnic-hdl: TST-TEST\nmnt-by: TEST-MNT");

    final WhoisResources result =
        RestTest.target(
                getPort(),
                "api/user/8ffe29be-89ef-41c8-ba7f-0e1553a623e5/organisations",
                null,
                apiKey)
            .request(MediaType.APPLICATION_JSON_TYPE)
            .get(WhoisResources.class);

    final List<WhoisObject> whoisObjects = result.getWhoisObjects();
    assertThat(whoisObjects, hasSize(1));

    final RpslObject resultOrg =
        whoisObjectMapper.map(whoisObjects.get(0), FormattedClientAttributeMapper.class);

    assertThat(resultOrg, is(organisation));
  }
  @Test
  public void organisation_only_added_once() {
    databaseHelper.addObject("mntner: TEST-MNT\nmnt-by:TEST-MNT\nauth: SSO [email protected]");
    final RpslObject organisation =
        RpslObject.parse(
            ""
                + "organisation: ORG-TST-TEST\n"
                + "mnt-by: TEST-MNT\n"
                + "mnt-by: TEST-MNT\n"
                + "source: TEST");
    databaseHelper.addObject(organisation);

    final WhoisResources result =
        RestTest.target(
                getPort(),
                "api/user/906635c2-0405-429a-800b-0602bd716124/organisations",
                null,
                apiKey)
            .request(MediaType.APPLICATION_JSON_TYPE)
            .get(WhoisResources.class);

    final List<WhoisObject> whoisObjects = result.getWhoisObjects();
    assertThat(whoisObjects, hasSize(1));

    final RpslObject resultOrg =
        whoisObjectMapper.map(whoisObjects.get(0), FormattedClientAttributeMapper.class);

    assertThat(resultOrg, is(organisation));
  }
  @Test
  public void organisations_w_mnt_by_and_ref_RS_found_via_mnt_ref() {
    databaseHelper.addObject("mntner: TEST-MNT\nmnt-by:TEST-MNT\nauth: SSO [email protected]");
    databaseHelper.addObject(
        "mntner: RIPE-NCC-HM-MNT\nmnt-by:RIPE-NCC-HM-MNT\nauth: SSO [email protected]");
    final RpslObject organisation =
        RpslObject.parse(
            ""
                + "organisation: ORG-TST-TEST\n"
                + "mnt-ref: TEST-MNT\n"
                + "mnt-ref: RIPE-NCC-HM-MNT\n"
                + "mnt-by: RIPE-NCC-HM-MNT\n"
                + "source: TEST");
    databaseHelper.addObject(organisation);

    final WhoisResources result =
        RestTest.target(
                getPort(),
                "api/user/ed7cd420-6402-11e3-949a-0800200c9a66/organisations",
                null,
                apiKey)
            .request(MediaType.APPLICATION_JSON_TYPE)
            .get(WhoisResources.class);

    final RpslObject resultOrg =
        whoisObjectMapper.map(
            result.getWhoisObjects().get(0), FormattedClientAttributeMapper.class);

    assertThat(resultOrg, is(organisation));
  }
  @Test
  public void no_organisations_found() throws InterruptedException {
    try {
      RestTest.target(getPort(), "api/user/aaaa-bbbb-cccc-dddd-1234/organisations", null, apiKey)
          .request(MediaType.APPLICATION_JSON_TYPE)
          .get(WhoisResources.class);
      fail();
    } catch (WebApplicationException expected) {
      final WhoisResources entity = expected.getResponse().readEntity(WhoisResources.class);

      assertThat(entity.getErrorMessages().size(), is(1));

      final ErrorMessage errorMessage = entity.getErrorMessages().get(0);
      assertThat(errorMessage.getSeverity(), is("Error"));
      assertThat(errorMessage.getText(), is("No organisations found"));
    }
  }
  @Test
  public void only_auth_sso_mntners_yield_results() {
    databaseHelper.addObject("mntner: SSO-MNT\nmnt-by:SSO-MNT\nauth: SSO [email protected]");
    databaseHelper.addObject(
        "mntner: MD5-MNT\nmnt-by:MD5-MNT\nauth: MD5-PW 017f750e-6eb8-4ab1-b5ec-8ad64ce9a503");
    final RpslObject organisation =
        RpslObject.parse(
            ""
                + "organisation: ORG-SSO-TEST\n"
                + "mnt-ref: SSO-MNT\n"
                + "mnt-by: SSO-MNT\n"
                + "source: TEST");
    databaseHelper.addObject(organisation);
    databaseHelper.addObject(
        ""
            + "organisation: ORG-MD5-TEST\n"
            + "mnt-ref: MD5-MNT\n"
            + "mnt-by: MD5-MNT\n"
            + "source: TEST");

    final WhoisResources result =
        RestTest.target(
                getPort(),
                "api/user/017f750e-6eb8-4ab1-b5ec-8ad64ce9a503/organisations",
                null,
                apiKey)
            .request(MediaType.APPLICATION_JSON_TYPE)
            .get(WhoisResources.class);

    final List<WhoisObject> whoisObjects = result.getWhoisObjects();
    assertThat(whoisObjects, hasSize(1));

    final RpslObject resultOrg =
        whoisObjectMapper.map(whoisObjects.get(0), FormattedClientAttributeMapper.class);

    assertThat(resultOrg, is(organisation));
  }