Skip to content

Commit

Permalink
fix deploy action
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyu55 committed Jan 11, 2024
1 parent 8e63ce1 commit bee3dc9
Showing 1 changed file with 90 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -1,164 +1,165 @@
package com.weibo.rill.flow.sample

import com.alibaba.fastjson.JSONObject
import net.sf.json.groovy.JsonSlurper
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.ContentType
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClientBuilder
import spock.lang.Specification
import spock.lang.Stepwise
import spock.lang.Timeout

@Stepwise
class SampleApiTest extends Specification {

@Timeout(10)

def "choice sample add dag task"() {
def "run choice sample task"() {
when:
String url = "http://localhost:8080/flow/bg/manage/descriptor/add_descriptor.json?business_id=rillFlowSample&feature_name=choiceSample&alias=release";
String contentType = "text/plain";
String requestData = readFileContent("../docs/samples/choice-sample.yaml");
def responseJson = sendPostRequest("http://localhost:8080/flow/bg/manage/descriptor/add_descriptor.json?business_id=rillFlowSample&feature_name=choiceSample&alias=release", "text/plain", readFileContent("../docs/samples/choice-sample.yaml"))

then:
JSONObject responseJson = sendApiRequest(url, contentType, requestData);
responseJson.status == 200
responseJson.content.ret == true

expect:
responseJson.statusCode == 200
responseJson.getJSONObject("responseContent").getBoolean("ret") == true

}

def "choice sample submit dag task"() {
when:
String url = "http://localhost:8080/flow/submit.json?descriptor_id=rillFlowSample:choiceSample";
String contentType = "application/json";
String requestData = "{\"input_num\":10}";
def submitResponseJson = sendPostRequest("http://localhost:8080/flow/submit.json?descriptor_id=rillFlowSample:choiceSample", "application/json", "{\"input_num\":10}")

then:
JSONObject responseJson = sendApiRequest(url, contentType, requestData);
submitResponseJson.status == 200
submitResponseJson.content.execution_id != ""

expect:
responseJson.statusCode == 200
responseJson.getJSONObject("responseContent").getString("execution_id") != ""
assert checkDagStatus(submitResponseJson.content.execution_id)
}

def "call api sample add dag task"() {
def "run call api sample task"() {
when:
String url = "http://localhost:8080/flow/bg/manage/descriptor/add_descriptor.json?business_id=rillFlowSample&feature_name=callApiSample&alias=release";
String contentType = "text/plain";
String requestData = readFileContent("../docs/samples/call-api-sample.yaml");
def responseJson = sendPostRequest("http://localhost:8080/flow/bg/manage/descriptor/add_descriptor.json?business_id=rillFlowSample&feature_name=callApiSample&alias=release", "text/plain", readFileContent("../docs/samples/call-api-sample.yaml"))

then:
JSONObject responseJson = sendApiRequest(url, contentType, requestData);

expect:
responseJson.statusCode == 200
responseJson.getJSONObject("responseContent").getBoolean("ret") == true
}
responseJson.status == 200
responseJson.content.ret == true

def "call api sample submit dag task"() {
when:
String url = "http://localhost:8080/flow/submit.json?descriptor_id=rillFlowSample:callApiSample";
String contentType = "application/json";
String requestData = "{\"input_num\":10}";
def submitResponseJson = sendPostRequest("http://localhost:8080/flow/submit.json?descriptor_id=rillFlowSample:callApiSample", "application/json", "{\"input_num\":10}")

then:
JSONObject responseJson = sendApiRequest(url, contentType, requestData);
submitResponseJson.status == 200
submitResponseJson.content.execution_id != ""

expect:
responseJson.statusCode == 200
responseJson.getJSONObject("responseContent").getString("execution_id") != ""
assert checkDagStatus(submitResponseJson.content.execution_id)

}

def "parallel async sample add dag task"() {
def "run parallel async sample task"() {
when:
String url = "http://localhost:8080/flow/bg/manage/descriptor/add_descriptor.json?business_id=rillFlowSample&feature_name=parallelAsyncTask&alias=release";
String contentType = "text/plain";
String requestData = readFileContent("../docs/samples/parallel-async-dag.yaml");
def responseJson = sendPostRequest("http://localhost:8080/flow/bg/manage/descriptor/add_descriptor.json?business_id=rillFlowSample&feature_name=parallelAsyncTask&alias=release", "text/plain", readFileContent("../docs/samples/parallel-async-dag.yaml"))

then:
JSONObject responseJson = sendApiRequest(url, contentType, requestData);

expect:
responseJson.statusCode == 200
responseJson.getJSONObject("responseContent").getBoolean("ret") == true
}
responseJson.status == 200
responseJson.content.ret == true

def "parallel async sample submit dag task"() {
when:
String url = "http://localhost:8080/flow/submit.json?descriptor_id=rillFlowSample:parallelAsyncTask";
String contentType = "application/json";
String requestData = "{\"rand_num\":20}";
def submitResponseJson = sendPostRequest("http://localhost:8080/flow/submit.json?descriptor_id=rillFlowSample:parallelAsyncTask", "application/json", "{\"rand_num\":20}")

then:
JSONObject responseJson = sendApiRequest(url, contentType, requestData);
submitResponseJson.status == 200
submitResponseJson.content.execution_id != ""

expect:
responseJson.statusCode == 200
responseJson.getJSONObject("responseContent").getString("execution_id") != ""
assert checkDagStatus(submitResponseJson.content.execution_id)

}

def "ref sample add dag task"() {
def "run ref sample task"() {
when:
String url = "http://localhost:8080/flow/bg/manage/descriptor/add_descriptor.json?business_id=rillFlowSample&feature_name=subdagTask&alias=release";
String contentType = "text/plain";
String requestData = readFileContent("../docs/samples/ref-dag.yaml");
def responseJson = sendPostRequest("http://localhost:8080/flow/bg/manage/descriptor/add_descriptor.json?business_id=rillFlowSample&feature_name=subdagTask&alias=release", "text/plain", readFileContent("../docs/samples/ref-dag.yaml"))

then:
JSONObject responseJson = sendApiRequest(url, contentType, requestData);

expect:
responseJson.statusCode == 200
responseJson.getJSONObject("responseContent").getBoolean("ret") == true
}
responseJson.status == 200
responseJson.content.ret == true

def "ref sample submit dag task"() {
when:
String url = "http://localhost:8080/flow/submit.json?descriptor_id=rillFlowSample:subdagTask";
String contentType = "application/json";
String requestData = "{\"parent_rand_num\":20}";
def submitResponseJson = sendPostRequest("http://localhost:8080/flow/submit.json?descriptor_id=rillFlowSample:subdagTask", "application/json", "{\"parent_rand_num\":20}")

then:
JSONObject responseJson = sendApiRequest(url, contentType, requestData);
submitResponseJson.status == 200
submitResponseJson.content.execution_id != ""

expect:
responseJson.statusCode == 200
responseJson.getJSONObject("responseContent").getString("execution_id") != ""
assert checkDagStatus(submitResponseJson.content.execution_id)

}

private String readFileContent(String filePath) {
try {
// 读取文件内容并返回
return new File(filePath).text;
return new File(filePath).text
} catch (IOException e) {
e.printStackTrace();
return null;
e.printStackTrace()
return null
}
}

private JSONObject sendApiRequest(String url, String contentType, String requestData) {
HttpClient httpClient = HttpClientBuilder.create().build();
JSONObject jsonObject = new JSONObject();
private boolean checkDagStatus(String executionId) {
def i = 0
while (i < 10) {
def getResponseJson = sendGetRequest("http://localhost:8080/flow/get.json?execution_id=" + executionId, "application/json")
if (getResponseJson.content.ret.dag_status == "SUCCEED") {
return true
}
i++
Thread.sleep(700)
}
return false
}

private Map<String, Object> sendPostRequest(String url, String contentType, String requestData) {
HttpClient httpClient = HttpClientBuilder.create().build()
def result = [:]
try {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", contentType);
HttpPost httpPost = new HttpPost(url)
httpPost.addHeader("Content-Type", contentType)

httpPost.setEntity(new StringEntity(requestData, ContentType.create(contentType)))

httpPost.setEntity(new StringEntity(requestData, ContentType.create(contentType)));
HttpResponse response = httpClient.execute(httpPost)
HttpEntity entity = response.getEntity()

int statusCode = response.getStatusLine().getStatusCode()
def responseContent = new JsonSlurper().parse(entity.content)
result.status = statusCode
result.content = responseContent
result as Map<String, Object>
} catch (Exception e) {
e.printStackTrace()
return null
}
}

private Map<String, Object> sendGetRequest(String url, String contentType) {
HttpClient httpClient = HttpClientBuilder.create().build()
def result = [:]
try {
HttpGet httpGet = new HttpGet(url)
httpGet.addHeader("Content-Type", contentType)

HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
HttpResponse response = httpClient.execute(httpGet)
HttpEntity entity = response.getEntity()

int statusCode = response.getStatusLine().getStatusCode();
String responseContent = new JsonSlurper().parse(entity.content).toString()
jsonObject.put("statusCode", statusCode);
jsonObject.put("responseContent", responseContent);
return jsonObject;
int statusCode = response.getStatusLine().getStatusCode()
def responseContent = new JsonSlurper().parse(entity.content)
result.status = statusCode
result.content = responseContent
result as Map<String, Object>
} catch (Exception e) {
e.printStackTrace();
return null;
e.printStackTrace()
return null
}
}
}

0 comments on commit bee3dc9

Please sign in to comment.