Skip to content

Commit e464254

Browse files
mishraditiGMishx
authored andcommitted
feat(rest): Added tests for upload and download components
Signed-of-by Aditi Mishra <aditimishra91924@gmail.com>
1 parent 00d3cb1 commit e464254

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/TestHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.fasterxml.jackson.databind.JsonNode;
1515
import com.fasterxml.jackson.databind.ObjectMapper;
1616
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
17+
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent;
1718
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentType;
1819
import org.eclipse.sw360.datahandler.thrift.components.ClearingState;
1920
import org.eclipse.sw360.datahandler.thrift.components.Release;
@@ -23,6 +24,7 @@
2324
import org.eclipse.sw360.rest.resourceserver.attachment.AttachmentInfo;
2425
import org.eclipse.sw360.rest.resourceserver.core.MultiStatus;
2526
import org.springframework.http.HttpStatus;
27+
import org.springframework.http.MediaType;
2628
import org.springframework.http.ResponseEntity;
2729

2830
import java.nio.charset.StandardCharsets;
@@ -155,6 +157,13 @@ public static List<Attachment> getDummyAttachmentsListForTest() {
155157
return attachments;
156158
}
157159

160+
public static AttachmentContent getDummyAttachmentContent() {
161+
AttachmentContent content = new AttachmentContent();
162+
content.setContentType(MediaType.APPLICATION_PDF_VALUE);
163+
content.setFilename("dummy.txt");
164+
return content;
165+
}
166+
158167
public static List<AttachmentInfo> getDummyAttachmentInfoListForTest() {
159168
Source source1 = new Source(Source._Fields.RELEASE_ID, release1Id);
160169
Source source2 = new Source(Source._Fields.RELEASE_ID, releaseId2);

rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/integration/ComponentTest.java

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
1818
import org.eclipse.sw360.datahandler.thrift.Source;
1919
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
20+
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent;
2021
import org.eclipse.sw360.datahandler.thrift.components.Component;
2122
import org.eclipse.sw360.datahandler.thrift.users.User;
2223
import org.eclipse.sw360.rest.resourceserver.TestHelper;
@@ -29,13 +30,19 @@
2930
import org.springframework.boot.test.mock.mockito.MockBean;
3031
import org.springframework.boot.test.web.client.TestRestTemplate;
3132
import org.springframework.boot.test.web.server.LocalServerPort;
33+
import org.springframework.core.io.ByteArrayResource;
34+
import org.springframework.core.io.Resource;
3235
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
3336
import org.springframework.http.*;
3437

3538
import org.springframework.test.context.junit4.SpringRunner;
3639
import org.springframework.test.web.servlet.MockMvc;
40+
import org.springframework.util.LinkedMultiValueMap;
41+
import org.springframework.util.MultiValueMap;
3742

3843
import java.io.IOException;
44+
import java.io.InputStream;
45+
import java.nio.charset.StandardCharsets;
3946
import java.util.ArrayList;
4047
import java.util.Arrays;
4148
import java.util.HashMap;
@@ -60,9 +67,7 @@
6067
import static org.mockito.BDDMockito.then;
6168
import static org.mockito.ArgumentMatchers.any;
6269
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.*;
6671

6772
@RunWith(SpringRunner.class)
6873
public class ComponentTest extends TestIntegrationBase {
@@ -114,6 +119,71 @@ public void should_get_all_components() throws Exception {
114119
TestHelper.checkResponse(response.getBody(), "components", 1);
115120
}
116121

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+
117187
@Test
118188
public void should_get_all_components_empty_list() throws IOException, TException {
119189
given(this.componentServiceMock.getComponentsForUser(any())).willReturn(new ArrayList<>());

0 commit comments

Comments
 (0)