@Test
  public void testCreateRecordDuplicateUserSuppliedId() {
    response = recordEndpoint.createRecord(AUTH_TOKEN, AUTH_TOKEN, PLACE, duplicateTestJson);
    assertResponseStatusIs(Response.Status.CREATED);

    response = recordEndpoint.createRecord(AUTH_TOKEN, AUTH_TOKEN, PLACE, duplicateTestJson);
    assertResponseStatusIs(Response.Status.CONFLICT);
  }
 @Test
 public void testCreateRecordConversionException() {
   response =
       recordEndpoint.createRecordUsingAuthTokenWithRecord(
           "someToken78678567", PLACE, jsonToCreateFromConversionException);
   assertResponseStatusIs(Response.Status.BAD_REQUEST);
 }
 @Test
 public void testReadRecordUnauthenticated() {
   response =
       recordEndpoint.readRecordUsingAuthTokenByTypeAndId(
           "dummyNonAuthenticatedToken", PLACE, PLACE_0001);
   assertResponseStatusIs(Response.Status.UNAUTHORIZED);
 }
 @Test
 public void testReadIncomingLinksNotFound() {
   response =
       recordEndpoint.readIncomingRecordLinks(
           AUTH_TOKEN, AUTH_TOKEN, PLACE, "place:0001_NOT_FOUND");
   assertResponseStatusIs(Response.Status.NOT_FOUND);
 }
 @Test
 public void testUpdateRecordWrongDataTypeInJson() {
   response =
       recordEndpoint.updateRecord(
           AUTH_TOKEN, AUTH_TOKEN, PLACE, PLACE_0001, jsonToUpdateWithAttributeAsChild);
   assertResponseStatusIs(Response.Status.BAD_REQUEST);
 }
  @Test
  public void testDownload() throws IOException {
    response =
        recordEndpoint.downloadFile(AUTH_TOKEN, AUTH_TOKEN, "image", "image:123456789", "master");
    String contentType = response.getHeaderString("Content-Type");
    /*
     * when we detect and store type of file in spider check it like this
     * assertEquals(contentType, "application/octet-stream");
     */
    assertEquals(contentType, "application/octet-stream");
    // assertEquals(contentType, null);
    InputStream stream = (InputStream) response.getEntity();
    assertNotNull(stream);

    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = stream.read(buffer)) != -1) {
      result.write(buffer, 0, length);
    }
    String stringFromStream = result.toString("UTF-8");

    assertEquals(stringFromStream, "a string out");
    assertResponseStatusIs(Response.Status.OK);

    String contentLenght = response.getHeaderString("Content-Length");
    assertEquals(contentLenght, "123");

    String contentDisposition = response.getHeaderString("Content-Disposition");
    assertEquals(contentDisposition, "attachment; filename=adele.png");
  }
 @Test
 public void testDeleteRecordNotFound() {
   response =
       recordEndpoint.deleteRecordUsingAuthTokenByTypeAndId(
           "someToken78678567", PLACE, "place:0001_NOT_FOUND");
   assertResponseStatusIs(Response.Status.NOT_FOUND);
 }
 @Test
 public void testUpdateRecordTypeNotFound() {
   response =
       recordEndpoint.updateRecord(
           AUTH_TOKEN, AUTH_TOKEN, PLACE + "_NOT_FOUND", PLACE_0001, jsonToUpdateWithNotFound);
   assertResponseStatusIs(Response.Status.NOT_FOUND);
 }
 @Test
 public void testDownloadNotFound() throws IOException {
   response =
       recordEndpoint.downloadFile(
           AUTH_TOKEN, AUTH_TOKEN, "image", "image:123456789_NOT_FOUND", "master");
   assertResponseStatusIs(Response.Status.NOT_FOUND);
 }
 @Test
 public void testReadRecordAbstractRecordType() {
   String type = "abstract";
   response =
       recordEndpoint.readRecord(AUTH_TOKEN, AUTH_TOKEN, type, "canBeWhatEverIdTypeIsChecked");
   assertResponseStatusIs(Response.Status.METHOD_NOT_ALLOWED);
 }
 @Test
 public void testCreateRecordAttributeAsChild() {
   response =
       recordEndpoint.createRecord(
           AUTH_TOKEN, AUTH_TOKEN, PLACE, jsonToCreateFromAttributeAsChild);
   assertResponseStatusIs(Response.Status.BAD_REQUEST);
 }
 @Test
 public void testCreateNonExistingRecordType() {
   String type = "recordType_NON_EXCISTING";
   response =
       recordEndpoint.createRecordUsingAuthTokenWithRecord(
           "dummyNonAuthorizedToken", type, jsonToCreateFrom);
   assertResponseStatusIs(Response.Status.NOT_FOUND);
 }
 @Test
 public void testCreateRecordUnauthorized() {
   setNotAuthorized();
   response =
       recordEndpoint.createRecordUsingAuthTokenWithRecord(
           "dummyNonAuthorizedToken", PLACE, jsonToCreateFrom);
   assertResponseStatusIs(Response.Status.FORBIDDEN);
 }
 @Test
 public void testUpdateRecordUnauthorized() {
   setNotAuthorized();
   response =
       recordEndpoint.updateRecordUsingAuthTokenWithRecord(
           "dummyNonAuthorizedToken", PLACE, PLACE_0001, jsonToUpdateWith);
   assertResponseStatusIs(Response.Status.FORBIDDEN);
 }
 @Test
 public void testDeleteRecordUnauthorized() {
   setNotAuthorized();
   response =
       recordEndpoint.deleteRecordUsingAuthTokenByTypeAndId(
           "dummyNonAuthorizedToken", PLACE, PLACE_0001);
   assertResponseStatusIs(Response.Status.FORBIDDEN);
 }
 @Test
 public void testReadIncomingLinksUnauthorized() {
   setNotAuthorized();
   response =
       recordEndpoint.readIncomingRecordLinksUsingAuthTokenByTypeAndId(
           "dummyNonAuthorizedToken", PLACE, PLACE_0001);
   assertResponseStatusIs(Response.Status.FORBIDDEN);
 }
 @Test
 public void testDownloadUnauthorized() throws IOException {
   setNotAuthorized();
   response =
       recordEndpoint.downloadFileUsingAuthTokenWithStream(
           "dummyNonAuthorizedToken", "image", "image:123456789", "master");
   assertResponseStatusIs(Response.Status.FORBIDDEN);
 }
  private void expectTokenForReadListToPrefereblyBeHeaderThanQuery(
      String headerAuthToken, String queryAuthToken, String authTokenExpected) {
    SpiderInstanceFactorySpy factorySpy = setupInstanceProviderWithFactorySpy();

    response = recordEndpoint.readRecordList(headerAuthToken, queryAuthToken, PLACE);

    SpiderRecordListReaderSpy spiderListReaderSpy = factorySpy.spiderRecordListReaderSpy;
    assertEquals(spiderListReaderSpy.authToken, authTokenExpected);
  }
  private void expectTokenForDeleteToPrefereblyBeHeaderThanQuery(
      String headerAuthToken, String queryAuthToken, String authTokenExpected) {
    SpiderInstanceFactorySpy factorySpy = setupInstanceProviderWithFactorySpy();

    response = recordEndpoint.deleteRecord(headerAuthToken, queryAuthToken, PLACE, "place:0002");

    SpiderRecordDeleterSpy spiderDeleterSpy = factorySpy.spiderRecordDeleterSpy;
    assertEquals(spiderDeleterSpy.authToken, authTokenExpected);
  }
  private void expectTokenForCreateToPrefereblyBeHeaderThanQuery(
      String headerAuthToken, String queryAuthToken, String authTokenExpected) {
    SpiderInstanceFactorySpy factorySpy = setupInstanceProviderWithFactorySpy();

    response =
        recordEndpoint.createRecord(headerAuthToken, queryAuthToken, PLACE, jsonToCreateFrom);

    SpiderCreatorSpy spiderCreatorSpy = factorySpy.spiderCreatorSpy;
    assertEquals(spiderCreatorSpy.authToken, authTokenExpected);
  }
  @Test
  public void testCreateRecordUnexpectedError() {
    DependencyProviderForTest spiderDependencyProvider = new DependencyProviderForTest();
    spiderDependencyProvider.setDataValidator(new DataValidatorReturnNullPointer());
    SpiderInstanceFactory factory =
        SpiderInstanceFactoryImp.usingDependencyProvider(spiderDependencyProvider);
    SpiderInstanceProvider.setSpiderInstanceFactory(factory);

    response = recordEndpoint.createRecord(AUTH_TOKEN, AUTH_TOKEN, PLACE, jsonToCreateFrom);
    assertResponseStatusIs(Response.Status.INTERNAL_SERVER_ERROR);
  }
  private void expectTokenForDownloadToPrefereblyBeHeaderThanQuery(
      String headerAuthToken, String queryAuthToken, String authTokenExpected) {
    SpiderInstanceFactorySpy factorySpy = setupInstanceProviderWithFactorySpy();

    response =
        recordEndpoint.downloadFile(
            headerAuthToken, queryAuthToken, "image", "image:123456789", "master");

    SpiderDownloaderSpy spiderDownloaderSpy = factorySpy.spiderDownloaderSpy;
    assertEquals(spiderDownloaderSpy.authToken, authTokenExpected);
  }
  @Test
  public void testUploadUnauthorized() {
    setNotAuthorized();

    InputStream stream = new ByteArrayInputStream("a string".getBytes(StandardCharsets.UTF_8));

    response =
        recordEndpoint.uploadFileUsingAuthTokenWithStream(
            "dummyNonAuthorizedToken", "image", "image:123456789", stream, "someFile.tif");

    assertResponseStatusIs(Response.Status.FORBIDDEN);
  }
  @Test
  public void testCreateRecordNotValid() {
    // uses always invalid validator
    DependencyProviderForTest spiderDependencyProvider = new DependencyProviderForTest();
    spiderDependencyProvider.setDataValidator(new DataValidatorAlwaysInvalidSpy());
    SpiderInstanceFactory factory =
        SpiderInstanceFactoryImp.usingDependencyProvider(spiderDependencyProvider);
    SpiderInstanceProvider.setSpiderInstanceFactory(factory);

    response = recordEndpoint.createRecord(AUTH_TOKEN, AUTH_TOKEN, PLACE, jsonToCreateFrom);
    assertResponseStatusIs(Response.Status.BAD_REQUEST);
  }
  @Test
  public void testUploadStreamMissing() {
    FormDataContentDispositionBuilder builder =
        FormDataContentDisposition.name("multipart;form-data");
    builder.fileName("adele1.png");
    FormDataContentDisposition formDataContentDisposition = builder.build();

    response =
        recordEndpoint.uploadFile(
            AUTH_TOKEN, AUTH_TOKEN, "image", "image:123456789", null, formDataContentDisposition);

    assertResponseStatusIs(Response.Status.BAD_REQUEST);
  }
  @Test
  public void testUpload() throws ParseException {
    InputStream stream = new ByteArrayInputStream("a string".getBytes(StandardCharsets.UTF_8));

    FormDataContentDispositionBuilder builder =
        FormDataContentDisposition.name("multipart;form-data");
    builder.fileName("adele1.png");
    FormDataContentDisposition formDataContentDisposition = builder.build();

    response =
        recordEndpoint.uploadFile(
            AUTH_TOKEN, AUTH_TOKEN, "image", "image:123456789", stream, formDataContentDisposition);

    String entity = (String) response.getEntity();

    assertEquals(
        entity,
        "{\"record\":{\"data\":{\"children\":[{\"children\":["
            + "{\"name\":\"id\",\"value\":\"image:123456789\"},"
            + "{\"name\":\"type\",\"value\":\"image\"},"
            + "{\"name\":\"createdBy\",\"value\":\"12345\"},"
            + "{\"children\":["
            + "{\"name\":\"linkedRecordType\",\"value\":\"system\"},"
            + "{\"name\":\"linkedRecordId\",\"value\":\"cora\"}],\"actionLinks\":"
            + "{\"read\":{\"requestMethod\":\"GET\",\"rel\":\"read\","
            + "\"url\":\"http://localhost:8080/therest/rest/record/system/cora\","
            + "\"accept\":\"application/uub+record+json\"}},\"name\":\"dataDivider\"}],"
            + "\"name\":\"recordInfo\"},{\"children\":[{\"children\":[{\"name\":\"streamId\","
            + "\"value\":\"1\"},{\"name\":\"filename\",\"value\":\"adele1.png\"}"
            + ",{\"name\":\"filesize\",\"value\":\"8\"}"
            + ",{\"name\":\"mimeType\",\"value\":\"application/octet-stream\"}]"
            + ",\"actionLinks\":{\"read\":{\"requestMethod\":\"GET\",\"rel\":\"read\","
            + "\"url\":\"http://localhost:8080/therest/rest/record/image/image:123456789/master\","
            + "\"accept\":\"application/octet-stream\"}},\"name\":\"master\"}],"
            + "\"name\":\"resourceInfo\"}],\"name\":\"binary\"},\"actionLinks\":{\"read\":"
            + "{\"requestMethod\":\"GET\",\"rel\":\"read\","
            + "\"url\":\"http://localhost:8080/therest/rest/record/image/image:123456789\","
            + "\"accept\":\"application/uub+record+json\"},"
            + "\"upload\":{\"requestMethod\":\"POST\",\"rel\":\"upload\","
            + "\"contentType\":\"multipart/form-data\",\"url\":"
            + "\"http://localhost:8080/therest/rest/record/image/image:123456789/master\"},"
            + "\"update\":{\"requestMethod\":\"POST\","
            + "\"rel\":\"update\",\"contentType\":\"application/uub+record+json\","
            + "\"url\":\"http://localhost:8080/therest/rest/record/image/image:123456789\","
            + "\"accept\":\"application/uub+record+json\"},\"delete\":"
            + "{\"requestMethod\":\"DELETE\",\"rel\":\"delete\","
            + "\"url\":\"http://localhost:8080/therest/rest/record/image/image:123456789\"}}}}");

    assertResponseStatusIs(Response.Status.OK);
  }
  @Test
  public void testUploadNotAChildOfBinary() {
    InputStream stream = new ByteArrayInputStream("a string".getBytes(StandardCharsets.UTF_8));

    FormDataContentDispositionBuilder builder =
        FormDataContentDisposition.name("multipart;form-data");
    builder.fileName("adele1.png");
    FormDataContentDisposition formDataContentDisposition = builder.build();

    response =
        recordEndpoint.uploadFile(
            AUTH_TOKEN, AUTH_TOKEN, PLACE, "image:123456789", stream, formDataContentDisposition);

    assertResponseStatusIs(Response.Status.METHOD_NOT_ALLOWED);
  }
  private void expectTokenForUploadToPrefereblyBeHeaderThanQuery(
      String headerAuthToken, String queryAuthToken, String authTokenExpected) {
    SpiderInstanceFactorySpy factorySpy = setupInstanceProviderWithFactorySpy();

    FormDataContentDispositionBuilder builder =
        FormDataContentDisposition.name("multipart;form-data");
    builder.fileName("adele1.png");
    FormDataContentDisposition formDataContentDisposition = builder.build();
    InputStream stream = new ByteArrayInputStream("a string".getBytes(StandardCharsets.UTF_8));

    response =
        recordEndpoint.uploadFile(
            headerAuthToken,
            queryAuthToken,
            "image",
            "image:123456789",
            stream,
            formDataContentDisposition);

    SpiderUploaderSpy spiderUploaderSpy = factorySpy.spiderUploaderSpy;
    assertEquals(spiderUploaderSpy.authToken, authTokenExpected);
  }
  @Test
  public void testReadIncomingRecordLinks() {
    response = recordEndpoint.readIncomingRecordLinks(AUTH_TOKEN, AUTH_TOKEN, PLACE, PLACE_0001);
    String entity = (String) response.getEntity();

    assertEquals(
        entity,
        "{\"dataList\":{\"fromNo\":\"1\",\"data\":["
            + "{\"children\":[{\"children\":["
            + "{\"name\":\"linkedRecordType\",\"value\":\"place\"}"
            + ",{\"name\":\"linkedRecordId\",\"value\":\"place:0002\"}]"
            + ",\"actionLinks\":{"
            + "\"read\":{\"requestMethod\":\"GET\",\"rel\":\"read\""
            + ",\"url\":\"http://localhost:8080/therest/rest/record/place/place:0002\""
            + ",\"accept\":\"application/uub+record+json\"}}"
            + ",\"name\":\"from\"},{\"children\":["
            + "{\"name\":\"linkedRecordType\",\"value\":\"place\"}"
            + ",{\"name\":\"linkedRecordId\",\"value\":\"place:0001\"}]"
            + ",\"name\":\"to\"}],\"name\":\"recordToRecordLink\"}]"
            + ",\"totalNo\":\"1\""
            + ",\"containDataOfType\":\"recordToRecordLink\",\"toNo\":\"1\"}}");

    assertResponseStatusIs(Response.Status.OK);
  }
 @Test
 public void testDownloadBadRequest() throws IOException {
   response = recordEndpoint.downloadFile(AUTH_TOKEN, AUTH_TOKEN, "image", "image:123456789", "");
   assertResponseStatusIs(Response.Status.BAD_REQUEST);
 }