HttpFileUploadIntegrationTest.java

  1. /**
  2.  *
  3.  * Copyright 2017-2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.httpfileupload;

  18. import static org.junit.jupiter.api.Assertions.assertArrayEquals;

  19. import java.io.BufferedInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.net.HttpURLConnection;
  26. import java.net.URL;

  27. import org.jivesoftware.smack.SmackException;
  28. import org.jivesoftware.smack.SmackException.NoResponseException;
  29. import org.jivesoftware.smack.SmackException.NotConnectedException;
  30. import org.jivesoftware.smack.XMPPException.XMPPErrorException;

  31. import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
  32. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  33. import org.igniterealtime.smack.inttest.TestNotPossibleException;
  34. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
  35. import org.igniterealtime.smack.inttest.annotations.SpecificationReference;

  36. @SpecificationReference(document = "XEP-0363", version = "0.4.0")
  37. public class HttpFileUploadIntegrationTest extends AbstractSmackIntegrationTest {

  38.     private static final int FILE_SIZE = 1024 * 128;

  39.     private final HttpFileUploadManager hfumOne;

  40.     public HttpFileUploadIntegrationTest(SmackIntegrationTestEnvironment environment) throws XMPPErrorException,
  41.                     NotConnectedException, NoResponseException, InterruptedException, TestNotPossibleException {
  42.         super(environment);
  43.         hfumOne = HttpFileUploadManager.getInstanceFor(conOne);
  44.         if (!hfumOne.discoverUploadService()) {
  45.             throw new TestNotPossibleException(
  46.                             "HttpFileUploadManager was unable to discover a HTTP File Upload service");
  47.         }
  48.         UploadService uploadService = hfumOne.getDefaultUploadService();
  49.         if (!uploadService.acceptsFileOfSize(FILE_SIZE)) {
  50.             throw new TestNotPossibleException("The upload service at " + uploadService.getAddress()
  51.                             + " does not accept files of size " + FILE_SIZE
  52.                             + ". It only accepts files with  a maximum size of " + uploadService.getMaxFileSize());
  53.         }
  54.         if (environment.configuration.sslContextFactory != null) {
  55.             hfumOne.setTlsContext(environment.configuration.sslContextFactory.createSslContext());
  56.         }
  57.     }

  58.     @SmackIntegrationTest
  59.     public void httpFileUploadTest() throws IOException, XMPPErrorException, InterruptedException, SmackException {
  60.         final int fileSize = FILE_SIZE;
  61.         File file = createNewTempFile();
  62.         FileOutputStream fos = new FileOutputStream(file.getCanonicalPath());
  63.         byte[] upBytes;
  64.         try {
  65.             upBytes = new byte[fileSize];
  66.             INSECURE_RANDOM.nextBytes(upBytes);
  67.             fos.write(upBytes);
  68.         }
  69.         finally {
  70.             fos.close();
  71.         }

  72.         URL getUrl = hfumOne.uploadFile(file, new UploadProgressListener() {
  73.             @Override
  74.             public void onUploadProgress(long uploadedBytes, long totalBytes) {
  75.                 double progress = uploadedBytes / totalBytes;
  76.                 LOGGER.fine("HTTP File Upload progress " + progress + "% (" + uploadedBytes + '/' + totalBytes + ')');
  77.             }
  78.         });

  79.         HttpURLConnection urlConnection = getHttpUrlConnectionFor(getUrl);

  80.         ByteArrayOutputStream baos = new ByteArrayOutputStream(fileSize);
  81.         byte[] buffer = new byte[4096];
  82.         int n;
  83.         try {
  84.             InputStream is = new BufferedInputStream(urlConnection.getInputStream());
  85.             while ((n = is.read(buffer)) != -1) {
  86.                 baos.write(buffer, 0, n);
  87.             }
  88.         }
  89.         finally {
  90.             urlConnection.disconnect();
  91.         }

  92.         byte[] downBytes = baos.toByteArray();

  93.         assertArrayEquals(upBytes, downBytes, "Expected the downloaded bytes to be equal to the uploaded bytes (but they were not).");
  94.     }
  95. }