Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. import com.adobe.qe.toughday.api.core.AbstractTest;
  2. import com.adobe.qe.toughday.api.annotations.Description;
  3. import com.adobe.qe.toughday.api.annotations.Tag;
  4. import com.adobe.qe.toughday.api.annotations.ConfigArgGet;
  5. import com.adobe.qe.toughday.api.annotations.ConfigArgSet;
  6. import com.adobe.qe.toughday.api.annotations.After;
  7. import com.adobe.qe.toughday.api.annotations.Before;
  8. import com.adobe.qe.toughday.tests.composite.AuthoringTest;
  9. import com.adobe.qe.toughday.tests.sequential.AEMTestBase;
  10. import com.adobe.qe.toughday.tests.samplecontent.SampleContent;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.apache.http.HttpStatus;
  13. import org.apache.http.entity.mime.HttpMultipartMode;
  14. import org.apache.http.entity.mime.MultipartEntity;
  15. import org.apache.http.entity.mime.content.FileBody;
  16. import org.apache.http.entity.mime.content.StringBody;
  17. import org.apache.sling.testing.clients.ClientException;
  18. import org.apache.sling.testing.clients.Constants;
  19.  
  20. import javax.imageio.ImageIO;
  21. import java.awt.*;
  22. import java.awt.image.BufferedImage;
  23. import java.io.*;
  24. import java.nio.charset.Charset;
  25. import java.util.Random;
  26. import java.util.concurrent.atomic.AtomicInteger;
  27.  
  28. @Tag(tags = { "author" })
  29. @Description(desc = "Test for uploading assets under the same path." +
  30. " Due to OAK limitations, performance will decrease over time." +
  31. " If you are not looking for this specific scenario, please consider using CreateAssetTreeTest.")
  32. public class UploadImageTest extends AEMTestBase {
  33.  
  34. private String fileName = AuthoringTest.DEFAULT_ASSET_NAME;
  35. private String resourcePath = AuthoringTest.DEFAULT_RESOURCE_PATH;
  36. private String mimeType = AuthoringTest.DEFAULT_MIME_TYPE; //TODO do we really need this?
  37. private String parentPath = SampleContent.TOUGHDAY_DAM_FOLDER;
  38.  
  39. public static ThreadLocal<File> lastCreated = new ThreadLocal<>();
  40. public static Random rnd = new Random();
  41. public static final AtomicInteger nextNumber = new AtomicInteger(0);
  42.  
  43. private BufferedImage img;
  44. private InputStream imageStream;
  45.  
  46. public UploadImageTest() {}
  47.  
  48. private UploadImageTest(String fileName, String resourcePath, String mimeType, String parentPath) {
  49. this.resourcePath = resourcePath;
  50. this.mimeType = mimeType;
  51. this.parentPath = parentPath;
  52. this.fileName = fileName;
  53. }
  54.  
  55. @Before
  56. private void before() throws ClientException, IOException {
  57. String nextFileName = fileName + nextNumber.getAndIncrement() + ".png";
  58.  
  59. // image processing: read, add noise and save to file
  60. imageStream = UploadImageTest.getImage(this.resourcePath);
  61. img = ImageIO.read(imageStream);
  62. addNoise(img);
  63. File last = new File(workspace, nextFileName);
  64. ImageIO.write(img, "png", last);
  65. lastCreated.set(last);
  66. }
  67.  
  68. @Override
  69. public void test() throws Throwable {
  70. MultipartEntity multiPartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
  71. try {
  72. multiPartEntity.addPart("file", new FileBody(lastCreated.get()));
  73.  
  74. multiPartEntity.addPart(Constants.PARAMETER_CHARSET, new StringBody(Constants.CHARSET_UTF8));
  75. multiPartEntity.addPart("fileName", new StringBody(lastCreated.get().getName(),
  76. Charset.forName(Constants.CHARSET_UTF8)));
  77. } catch (UnsupportedEncodingException e) {
  78. throw new ClientException("Could not create Multipart Post!", e);
  79. }
  80.  
  81. String currentParentPath = StringUtils.stripEnd(getCommunication("parentPath", parentPath), "/");
  82.  
  83. try {
  84. logger().debug("{}: Trying to upload image={}{}", Thread.currentThread().getName(), currentParentPath, lastCreated.get().getName());
  85.  
  86. benchmark().measure(this, "UploadImage", getDefaultClient()).doPost(currentParentPath + ".createasset.html", multiPartEntity, HttpStatus.SC_OK);
  87. } catch (Throwable e) {
  88. logger().warn("{}: Failed to upload image={}{}", Thread.currentThread().getName(), currentParentPath, lastCreated.get().getName());
  89. logger().debug(Thread.currentThread().getName() + ": ERROR: ", e);
  90.  
  91. throw e;
  92. }
  93.  
  94. logger().debug("{}: Successfully uploaded image={}{}", Thread.currentThread().getName(), currentParentPath, lastCreated.get().getName());
  95. }
  96.  
  97. @After
  98. private void after() {
  99. if (!lastCreated.get().delete()) {
  100. throw new RuntimeException("Cannot delete file " + lastCreated.get().getName());
  101. }
  102. }
  103.  
  104.  
  105. @Override
  106. public AbstractTest newInstance() {
  107. return new UploadImageTest(fileName, resourcePath, mimeType, parentPath);
  108. }
  109.  
  110.  
  111. @ConfigArgSet(required = false, defaultValue = AuthoringTest.DEFAULT_ASSET_NAME, desc = "The name of the file to be created")
  112. public void setFileName(String fileName) {
  113. this.fileName = fileName;
  114. }
  115.  
  116. @ConfigArgGet
  117. public String getFileName() {
  118. return this.fileName;
  119. }
  120.  
  121. @ConfigArgSet(required = false, defaultValue = AuthoringTest.DEFAULT_RESOURCE_PATH,
  122. desc = "The image resource path either in the classpath or the filesystem")
  123. public void setResourcePath(String resourcePath) {
  124. this.resourcePath = resourcePath;
  125. }
  126.  
  127. @ConfigArgGet
  128. public String getResourcePath() {
  129. return this.resourcePath;
  130. }
  131.  
  132. @ConfigArgSet(required = false, defaultValue = AuthoringTest.DEFAULT_MIME_TYPE, desc = "The mime type of the uploaded image")
  133. public void setMimeType(String mimeType) {
  134. this.mimeType = mimeType;
  135. }
  136.  
  137. @ConfigArgGet
  138. public String getMimeType() {
  139. return this.mimeType;
  140. }
  141.  
  142. @ConfigArgSet(required = false, defaultValue = SampleContent.TOUGHDAY_DAM_FOLDER, desc = "The path where the image is uploaded")
  143. public void setParentPath(String parentPath) {
  144. this.parentPath = parentPath;
  145. }
  146.  
  147. @ConfigArgGet
  148. public String getParentPath() {
  149. return this.parentPath;
  150. }
  151.  
  152. /**
  153. * Get an InputStream of an image, either from the filesystem or from the resources.
  154. * @param filename
  155. * @return
  156. * @throws ClientException if filename is not found either on the filesystem or in the resources
  157. */
  158. public static InputStream getImage(String filename) throws ClientException {
  159. InputStream in;
  160. try {
  161. in = new FileInputStream(filename);
  162. } catch (FileNotFoundException e) {
  163. // try the classpath
  164. in = UploadImageTest.class.getClassLoader().getResourceAsStream(filename);
  165. if (null == in) {
  166. throw new ClientException("Could not find " + filename + " in classpath or in path");
  167. }
  168. }
  169. return in;
  170. }
  171.  
  172. /**
  173. * Add noise to a {@see BufferedImage}
  174. * @param img
  175. */
  176. public static void addNoise(BufferedImage img) {
  177. for (int i = 0; i < 200; i++) {
  178. int x = rnd.nextInt(img.getWidth());
  179. int y = rnd.nextInt(img.getHeight());
  180. img.setRGB(x, y, Color.CYAN.getRGB());
  181. }
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement