Search in sources :

Example 1 with CreateFunctionRequest

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

the class FunctionComputeClientTest method testCreateFunctionValidate.

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

Example 2 with CreateFunctionRequest

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

the class FunctionComputeClientTest method createFunction.

private CreateFunctionResponse createFunction(String functionName) {
    CreateFunctionRequest createFuncReq = new CreateFunctionRequest(SERVICE_NAME);
    createFuncReq.setFunctionName(functionName);
    createFuncReq.setDescription(FUNCTION_DESC_OLD);
    createFuncReq.setMemorySize(128);
    createFuncReq.setHandler("hello_world.handler");
    createFuncReq.setRuntime("nodejs4.4");
    createFuncReq.setCode(new Code().setOssBucketName(CODE_BUCKET).setOssObjectName(CODE_OBJECT));
    createFuncReq.setTimeout(10);
    return client.createFunction(createFuncReq);
}
Also used : CreateFunctionRequest(com.aliyuncs.fc.request.CreateFunctionRequest) Code(com.aliyuncs.fc.model.Code)

Example 3 with CreateFunctionRequest

use of com.aliyuncs.fc.request.CreateFunctionRequest 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

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