Search in sources :

Example 1 with InvokeFunctionRequest

use of com.aliyuncs.fc.request.InvokeFunctionRequest in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testInvokeFunctionValidate.

@Test
public void testInvokeFunctionValidate() {
    try {
        InvokeFunctionRequest request = new InvokeFunctionRequest(SERVICE_NAME, null);
        client.invokeFunction(request);
        fail("ClientException is expected");
    } catch (ClientException e) {
        assertTrue(e.getMessage().contains(VALIDATE_MSG));
    }
    try {
        InvokeFunctionRequest request = new InvokeFunctionRequest(SERVICE_NAME, "");
        client.invokeFunction(request);
        fail("ClientException is expected");
    } catch (ClientException e) {
        assertTrue(e.getMessage().contains(VALIDATE_MSG));
    }
    try {
        InvokeFunctionRequest request = new InvokeFunctionRequest("", FUNCTION_NAME);
        client.invokeFunction(request);
        fail("ClientException is expected");
    } catch (ClientException e) {
        assertTrue(e.getMessage().contains(VALIDATE_MSG));
    }
    try {
        InvokeFunctionRequest request = new InvokeFunctionRequest(null, FUNCTION_NAME);
        client.invokeFunction(request);
        fail("ClientException is expected");
    } catch (ClientException e) {
        assertTrue(e.getMessage().contains(VALIDATE_MSG));
    }
}
Also used : InvokeFunctionRequest(com.aliyuncs.fc.request.InvokeFunctionRequest) ClientException(com.aliyuncs.fc.exceptions.ClientException) Test(org.junit.Test)

Example 2 with InvokeFunctionRequest

use of com.aliyuncs.fc.request.InvokeFunctionRequest in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testCreateFunctionSetZipFile.

@Test
public void testCreateFunctionSetZipFile() throws IOException {
    createService(SERVICE_NAME);
    // Create a function
    CreateFunctionRequest createFuncReq = new CreateFunctionRequest(SERVICE_NAME);
    createFuncReq.setFunctionName(FUNCTION_NAME);
    createFuncReq.setDescription("Function for test");
    createFuncReq.setMemorySize(128);
    createFuncReq.setHandler("hello_world.handler");
    createFuncReq.setRuntime("nodejs4.4");
    // Setup code directory
    String tmpDir = "/tmp/fc_test_" + UUID.randomUUID();
    String funcFilePath = tmpDir + "/" + "hello_world.js";
    new File(tmpDir).mkdir();
    PrintWriter out = new PrintWriter(funcFilePath);
    out.println("'use strict'; module.exports.handler = function(event, context, callback) {console.log('hello world'); callback(null, 'hello world');};");
    out.close();
    String zipFilePath = tmpDir + "/" + "hello_world.zip";
    ZipUtils.zipDir(new File(tmpDir), zipFilePath);
    File zipFile = new File(zipFilePath);
    byte[] buffer = new byte[(int) zipFile.length()];
    FileInputStream fis = new FileInputStream(zipFilePath);
    fis.read(buffer);
    fis.close();
    Code code = new Code().setZipFile(buffer);
    createFuncReq.setCode(code);
    createFuncReq.setTimeout(10);
    client.createFunction(createFuncReq);
    // Invoke the function
    InvokeFunctionRequest request = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    InvokeFunctionResponse response = client.invokeFunction(request);
    assertEquals("hello world", new String(response.getContent()));
    // Cleanups
    client.deleteFunction(new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME));
    client.deleteService(new DeleteServiceRequest(SERVICE_NAME));
    new File(zipFilePath).delete();
    new File(funcFilePath).delete();
    new File(tmpDir).delete();
}
Also used : InvokeFunctionRequest(com.aliyuncs.fc.request.InvokeFunctionRequest) InvokeFunctionResponse(com.aliyuncs.fc.response.InvokeFunctionResponse) CreateFunctionRequest(com.aliyuncs.fc.request.CreateFunctionRequest) DeleteServiceRequest(com.aliyuncs.fc.request.DeleteServiceRequest) DeleteFunctionRequest(com.aliyuncs.fc.request.DeleteFunctionRequest) File(java.io.File) Code(com.aliyuncs.fc.model.Code) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

InvokeFunctionRequest (com.aliyuncs.fc.request.InvokeFunctionRequest)2 Test (org.junit.Test)2 ClientException (com.aliyuncs.fc.exceptions.ClientException)1 Code (com.aliyuncs.fc.model.Code)1 CreateFunctionRequest (com.aliyuncs.fc.request.CreateFunctionRequest)1 DeleteFunctionRequest (com.aliyuncs.fc.request.DeleteFunctionRequest)1 DeleteServiceRequest (com.aliyuncs.fc.request.DeleteServiceRequest)1 InvokeFunctionResponse (com.aliyuncs.fc.response.InvokeFunctionResponse)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 PrintWriter (java.io.PrintWriter)1