|
17 | 17 | import org.eclipse.sw360.datahandler.thrift.RequestStatus; |
18 | 18 | import org.eclipse.sw360.datahandler.thrift.Source; |
19 | 19 | import org.eclipse.sw360.datahandler.thrift.attachments.Attachment; |
| 20 | +import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent; |
20 | 21 | import org.eclipse.sw360.datahandler.thrift.components.Component; |
21 | 22 | import org.eclipse.sw360.datahandler.thrift.users.User; |
22 | 23 | import org.eclipse.sw360.rest.resourceserver.TestHelper; |
|
29 | 30 | import org.springframework.boot.test.mock.mockito.MockBean; |
30 | 31 | import org.springframework.boot.test.web.client.TestRestTemplate; |
31 | 32 | import org.springframework.boot.test.web.server.LocalServerPort; |
| 33 | +import org.springframework.core.io.ByteArrayResource; |
| 34 | +import org.springframework.core.io.Resource; |
32 | 35 | import org.springframework.data.rest.webmvc.ResourceNotFoundException; |
33 | 36 | import org.springframework.http.*; |
34 | 37 |
|
35 | 38 | import org.springframework.test.context.junit4.SpringRunner; |
36 | 39 | import org.springframework.test.web.servlet.MockMvc; |
| 40 | +import org.springframework.util.LinkedMultiValueMap; |
| 41 | +import org.springframework.util.MultiValueMap; |
37 | 42 |
|
38 | 43 | import java.io.IOException; |
| 44 | +import java.io.InputStream; |
| 45 | +import java.nio.charset.StandardCharsets; |
39 | 46 | import java.util.ArrayList; |
40 | 47 | import java.util.Arrays; |
41 | 48 | import java.util.HashMap; |
|
60 | 67 | import static org.mockito.BDDMockito.then; |
61 | 68 | import static org.mockito.ArgumentMatchers.any; |
62 | 69 | import static org.mockito.ArgumentMatchers.eq; |
63 | | -import static org.mockito.Mockito.never; |
64 | | -import static org.mockito.Mockito.when; |
65 | | -import static org.mockito.Mockito.doThrow; |
| 70 | +import static org.mockito.Mockito.*; |
66 | 71 |
|
67 | 72 | @RunWith(SpringRunner.class) |
68 | 73 | public class ComponentTest extends TestIntegrationBase { |
@@ -114,6 +119,71 @@ public void should_get_all_components() throws Exception { |
114 | 119 | TestHelper.checkResponse(response.getBody(), "components", 1); |
115 | 120 | } |
116 | 121 |
|
| 122 | + @Test |
| 123 | + public void should_download_attachment_form_component() throws Exception { |
| 124 | + String componentId = "abc"; |
| 125 | + String attachmentId = "def"; |
| 126 | + |
| 127 | + AttachmentContent attachmentContent = TestHelper.getDummyAttachmentContent(); |
| 128 | + |
| 129 | + given(this.componentServiceMock.getComponentForUserById(eq(componentId), any())) |
| 130 | + .willReturn(component); |
| 131 | + given(this.attachmentServiceMock.getAttachmentContent(attachmentId)) |
| 132 | + .willReturn(attachmentContent); |
| 133 | + |
| 134 | + InputStream mockInputStream = mock(InputStream.class); |
| 135 | + given(this.attachmentServiceMock.getStreamToAttachments(any(), any(), any())) |
| 136 | + .willReturn(mockInputStream); |
| 137 | + |
| 138 | + doCallRealMethod().when(attachmentServiceMock) |
| 139 | + .downloadAttachmentWithContext(any(), any(), any(), any()); |
| 140 | + |
| 141 | + HttpHeaders headers = getHeaders(port); |
| 142 | + headers.add("Accept", "application/octet-stream"); |
| 143 | + |
| 144 | + ResponseEntity<String> response = new TestRestTemplate().exchange( |
| 145 | + "http://localhost:" + port + "/api/components/" + componentId + "/attachments/" + attachmentId, |
| 146 | + HttpMethod.GET, |
| 147 | + new HttpEntity<>(null, headers), |
| 148 | + String.class |
| 149 | + ); |
| 150 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 151 | + assertEquals("application/pdf", response.getHeaders().getContentType().toString()); |
| 152 | + assertEquals("attachment; filename=\"dummy.txt\"", response.getHeaders().get("Content-Disposition").get(0)); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void should_add_attachment_to_component() throws Exception{ |
| 157 | + String componentId = "abc"; |
| 158 | + |
| 159 | + given(componentServiceMock.getComponentForUserById(eq(componentId), any())).willReturn(component); |
| 160 | + given(attachmentServiceMock.uploadAttachment(any(), any(), any())).willReturn(TestHelper.getDummyAttachmentsListForTest().getFirst()) |
| 161 | + ; |
| 162 | + given(componentServiceMock.updateComponent(any(), any())).willReturn(RequestStatus.SUCCESS); |
| 163 | + Resource fileResource = new ByteArrayResource("Dummy file content".getBytes(StandardCharsets.UTF_8)) { |
| 164 | + @Override |
| 165 | + public String getFilename() { |
| 166 | + return "test.txt"; |
| 167 | + } |
| 168 | + }; |
| 169 | + |
| 170 | + Attachment attachment = TestHelper.getDummyAttachmentsListForTest().getFirst(); |
| 171 | + MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
| 172 | + body.add("file", fileResource); |
| 173 | + body.add("attachment", attachment); |
| 174 | + HttpHeaders headers = getHeaders(port); |
| 175 | + headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
| 176 | + ResponseEntity<String> response = new TestRestTemplate().exchange( |
| 177 | + "http://localhost:" + port + "/api/components/" + componentId + "/attachments" , |
| 178 | + HttpMethod.POST, |
| 179 | + new HttpEntity<>(body, headers), |
| 180 | + String.class); |
| 181 | + |
| 182 | + System.out.println("Response is" + response); |
| 183 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 184 | + |
| 185 | + } |
| 186 | + |
117 | 187 | @Test |
118 | 188 | public void should_get_all_components_empty_list() throws IOException, TException { |
119 | 189 | given(this.componentServiceMock.getComponentsForUser(any())).willReturn(new ArrayList<>()); |
|
0 commit comments