1 package com.atlassian.marketplace.client.impl;
2
3 import java.io.File;
4 import java.net.URI;
5
6 import com.atlassian.marketplace.client.MpacException;
7 import com.atlassian.marketplace.client.api.ArtifactId;
8 import com.atlassian.marketplace.client.api.ImageId;
9 import com.atlassian.marketplace.client.model.Links;
10 import com.atlassian.marketplace.client.util.UriBuilder;
11
12 import org.junit.AfterClass;
13 import org.junit.Before;
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16
17 import static com.atlassian.marketplace.client.TestObjects.HOST_BASE;
18 import static com.atlassian.marketplace.client.api.ImagePurpose.LOGO;
19 import static com.atlassian.marketplace.client.impl.ClientTester.FAKE_ASSETS_PATH;
20 import static com.atlassian.marketplace.client.model.ModelBuilders.links;
21 import static org.apache.commons.io.FileUtils.writeStringToFile;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.equalTo;
24
25 public class AssetsImplTest extends ApiImplTestBase
26 {
27 private static final URI FAKE_ARTIFACT_URI = URI.create("/artifact");
28 private static final URI FAKE_ASSET_URI = URI.create("/asset");
29 private static final String FAKE_IMAGE_PATH = "/image/{imageType}";
30 private static final Links ASSETS_LINKS = links()
31 .put("artifact", FAKE_ARTIFACT_URI)
32 .putTemplate("imageByType", FAKE_IMAGE_PATH)
33 .build();
34 private static final InternalModel.MinimalLinks ASSETS_REP = new InternalModel.MinimalLinks(ASSETS_LINKS);
35 private static final Links RESPONSE_LINKS = links()
36 .put("self", FAKE_ASSET_URI)
37 .build();
38 private static final InternalModel.MinimalLinks ASSET_RESPONSE = new InternalModel.MinimalLinks(RESPONSE_LINKS);
39 private static final String FILE_DATA = "foo";
40
41 private static File file;
42
43 @BeforeClass
44 public static void makeTempFile() throws Exception
45 {
46 file = File.createTempFile("AssetsImplTest", "txt");
47 writeStringToFile(file, FILE_DATA);
48 }
49
50 @AfterClass
51 public static void cleanupFile()
52 {
53 file.delete();
54 }
55
56 @Before
57 public void setupAssetsRoot() throws Exception
58 {
59 tester.mockResource(assetsApi(), ASSETS_REP);
60 }
61
62 @Test
63 public void uploadArtifactPostsData() throws Exception
64 {
65 tester.mockPostResourceResponse(artifactUploadUri(), ASSET_RESPONSE);
66 tester.client.assets().uploadAddonArtifact(file);
67 assertThat(tester.getReceivedData(artifactUploadUri(), "POST"), equalTo(FILE_DATA));
68 }
69
70 @Test
71 public void uploadImagePostsData() throws Exception
72 {
73 tester.mockPostResourceResponse(imageUploadUri(), ASSET_RESPONSE);
74 tester.client.assets().uploadImage(file, LOGO);
75 assertThat(tester.getReceivedData(imageUploadUri(), "POST"), equalTo(FILE_DATA));
76 }
77
78 @Test
79 public void uploadArtifactReturnsAssetId() throws Exception
80 {
81 tester.mockPostResourceResponse(artifactUploadUri(), ASSET_RESPONSE);
82 assertThat(tester.client.assets().uploadAddonArtifact(file), equalTo(ArtifactId.fromUri(FAKE_ASSET_URI)));
83 }
84
85 @Test
86 public void uploadImageReturnsAssetId() throws Exception
87 {
88 tester.mockPostResourceResponse(imageUploadUri(), ASSET_RESPONSE);
89 assertThat(tester.client.assets().uploadImage(file, LOGO), equalTo(ImageId.fromUri(FAKE_ASSET_URI)));
90 }
91
92 @Test
93 public void uploadArtifactThrowsExceptionIfServerReturnsError() throws Exception
94 {
95 tester.mockPostResource(artifactUploadUri(), 400);
96 try
97 {
98 tester.client.assets().uploadAddonArtifact(file);
99 }
100 catch (MpacException.ServerError e)
101 {
102 assertThat(e.getStatus(), equalTo(400));
103 }
104 }
105
106 @Test
107 public void uploadImageThrowsExceptionIfServerReturnsError() throws Exception
108 {
109 tester.mockPostResource(imageUploadUri(), 400);
110 try
111 {
112 tester.client.assets().uploadImage(file, LOGO);
113 }
114 catch (MpacException.ServerError e)
115 {
116 assertThat(e.getStatus(), equalTo(400));
117 }
118 }
119
120 private URI artifactUploadUri()
121 {
122 return uploadUri(FAKE_ARTIFACT_URI);
123 }
124
125 private URI imageUploadUri()
126 {
127 return uploadUri(URI.create(FAKE_IMAGE_PATH.replace("{imageType}", "logo")));
128 }
129
130 private URI uploadUri(URI resourceUri)
131 {
132 return UriBuilder.fromUri(tester.apiBase.resolve(resourceUri)).queryParam("file", file.getName()).build();
133 }
134
135 private URI assetsApi()
136 {
137 return URI.create(HOST_BASE + FAKE_ASSETS_PATH);
138 }
139 }