Skip to content

Commit

Permalink
[SCB-486] add edge download test case
Browse files Browse the repository at this point in the history
  • Loading branch information
wujimin committed Apr 21, 2018
1 parent 2a44417 commit b60ac59
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,39 @@

package org.apache.servicecomb.demo.edge.business;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.servicecomb.demo.edge.model.AppClientDataRsp;
import org.apache.servicecomb.demo.edge.model.ChannelRequestBase;
import org.apache.servicecomb.demo.edge.model.ResultWithInstance;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestSchema(schemaId = "news-v2")
@RequestMapping(path = "/business/v2")
public class Impl {
File tempDir = new File("target/downloadTemp");

public Impl() throws IOException {
FileUtils.forceMkdir(tempDir);
}

@RequestMapping(path = "/channel/news/subscribe", method = RequestMethod.POST)
public AppClientDataRsp subscribeNewsColumn(@RequestBody ChannelRequestBase request) {
AppClientDataRsp response = new AppClientDataRsp();
Expand All @@ -46,4 +67,30 @@ public ResultWithInstance add(int x, int y) {
public ResultWithInstance dec(int x, int y) {
return ResultWithInstance.create(x - y);
}

@GetMapping(path = "/download")
@ApiResponses({
@ApiResponse(code = 200, response = File.class, message = ""),
})
public ResponseEntity<InputStream> download() throws IOException {
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=download.txt")
.body(new ByteArrayInputStream("download".getBytes(StandardCharsets.UTF_8)));
}

protected File createTempFile() throws IOException {
File file = new File(tempDir, "bigFile.txt");
file.delete();
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
randomAccessFile.setLength(10 * 1024 * 1024);
randomAccessFile.close();
return file;
}

@GetMapping(path = "/bigFile")
public File bigFile() throws IOException {
return createTempFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

package org.apache.servicecomb.demo.edge.consumer;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.servicecomb.core.Endpoint;
import org.apache.servicecomb.core.endpoint.EndpointsCache;
Expand All @@ -34,6 +36,7 @@
import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -75,6 +78,9 @@ public Consumer() {
public void run() {
prepareEdge();

testDownload();
testDownloadBigFile();

invoke("/v1/add", 2, 1, addV1Result);
invoke("/v1/add", 3, 1, addV1Result);
invoke("/v1/add", 4, 1, addV1Result);
Expand All @@ -100,6 +106,36 @@ public void run() {
checkResult("v2/dec", decV2Result, "2.0.0");
}

protected void testDownloadBigFile() {
String url = edgePrefix + "/v2/bigFile";
AtomicInteger size = new AtomicInteger();

template.execute(url, HttpMethod.GET, req -> {
}, resp -> {
byte[] buf = new byte[1 * 1024 * 1024];
try (InputStream is = resp.getBody()) {
for (;;) {
int len = is.read(buf);
if (len == -1) {
break;
}

size.addAndGet(len);
}
}
return null;
});
Assert.isTrue(size.get() == 10 * 1024 * 1024);
System.out.println("test download bigFile finished");
}

protected void testDownload() {
String url = edgePrefix + "/v2/download";
String content = template.getForObject(url, String.class);
Assert.isTrue("download".equals(content));
System.out.println("test download finished");
}

private void checkResult(String name, List<ResultWithInstance> results, String... expectedVersions) {
Set<String> versions = new HashSet<>();
Set<String> remained = new HashSet<>(Arrays.asList(expectedVersions));
Expand Down

0 comments on commit b60ac59

Please sign in to comment.