Search in sources :

Example 1 with CreateFunctionResponse

use of com.aliyuncs.fc.response.CreateFunctionResponse in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testCRUDHelper.

private void testCRUDHelper(boolean testTrigger) throws ParseException, InterruptedException {
    // Create Service
    CreateServiceResponse createSResp = createService(SERVICE_NAME);
    assertFalse(Strings.isNullOrEmpty(createSResp.getRequestId()));
    assertFalse(Strings.isNullOrEmpty(createSResp.getServiceId()));
    assertEquals(SERVICE_NAME, createSResp.getServiceName());
    assertEquals(SERVICE_DESC_OLD, createSResp.getDescription());
    assertEquals(ROLE, createSResp.getRole());
    GetServiceResponse svcOldResp = client.getService(new GetServiceRequest(SERVICE_NAME));
    // Update Service
    UpdateServiceRequest updateSReq = new UpdateServiceRequest(SERVICE_NAME);
    updateSReq.setDescription(SERVICE_DESC_NEW);
    Thread.sleep(1000L);
    UpdateServiceResponse updateSResp = client.updateService(updateSReq);
    verifyUpdate(svcOldResp.getServiceName(), updateSResp.getServiceName(), svcOldResp.getServiceId(), updateSResp.getServiceId(), svcOldResp.getLastModifiedTime(), updateSResp.getLastModifiedTime(), svcOldResp.getCreatedTime(), updateSResp.getCreatedTime(), svcOldResp.getDescription(), updateSResp.getDescription());
    // Get Service
    GetServiceRequest getSReq = new GetServiceRequest(SERVICE_NAME);
    GetServiceResponse getSResp = client.getService(getSReq);
    assertEquals(SERVICE_NAME, getSResp.getServiceName());
    assertEquals(svcOldResp.getServiceId(), getSResp.getServiceId());
    assertEquals(ROLE, getSResp.getRole());
    // Create Function
    CreateFunctionResponse createFResp = createFunction(FUNCTION_NAME);
    assertFalse(Strings.isNullOrEmpty(createFResp.getRequestId()));
    assertFalse(Strings.isNullOrEmpty(createFResp.getFunctionId()));
    assertEquals(FUNCTION_NAME, createFResp.getFunctionName());
    assertEquals(FUNCTION_DESC_OLD, createFResp.getDescription());
    // List Functions
    ListFunctionsRequest listFReq = new ListFunctionsRequest(SERVICE_NAME);
    ListFunctionsResponse listFResp = client.listFunctions(listFReq);
    assertFalse(Strings.isNullOrEmpty(listFResp.getRequestId()));
    assertEquals(1, listFResp.getFunctions().length);
    FunctionMetadata funcOld = listFResp.getFunctions()[0];
    assertEquals(FUNCTION_NAME, funcOld.getFunctionName());
    // Update Function
    UpdateFunctionRequest updateFReq = new UpdateFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    updateFReq.setDescription(FUNCTION_DESC_NEW);
    Thread.sleep(1000L);
    client.updateFunction(updateFReq);
    listFResp = client.listFunctions(listFReq);
    assertFalse(Strings.isNullOrEmpty(listFResp.getRequestId()));
    assertEquals(1, listFResp.getFunctions().length);
    FunctionMetadata funcNew = listFResp.getFunctions()[0];
    verifyUpdate(funcOld.getFunctionName(), funcNew.getFunctionName(), funcOld.getFunctionId(), funcNew.getFunctionId(), funcOld.getLastModifiedTime(), funcNew.getLastModifiedTime(), funcOld.getCreatedTime(), funcNew.getCreatedTime(), funcOld.getDescription(), funcNew.getDescription());
    // Get Function
    GetFunctionRequest getFReq = new GetFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    GetFunctionResponse getFResp = client.getFunction(getFReq);
    assertFalse(Strings.isNullOrEmpty(getFResp.getRequestId()));
    assertEquals(FUNCTION_NAME, getFResp.getFunctionName());
    // Get Function Code
    GetFunctionCodeRequest getFCReq = new GetFunctionCodeRequest(SERVICE_NAME, FUNCTION_NAME);
    GetFunctionCodeResponse getFCResp = client.getFunctionCode(getFCReq);
    assertFalse(Strings.isNullOrEmpty(getFResp.getRequestId()));
    String crc64 = fetchFromURL(getFCResp.getCodeUrl());
    assertEquals(crc64, getFCResp.getCodeChecksum());
    // Invoke Function
    InvokeFunctionRequest invkReq = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    InvokeFunctionResponse invkResp = client.invokeFunction(invkReq);
    assertTrue(!Strings.isNullOrEmpty(invkResp.getRequestId()));
    assertEquals("hello world", new String(invkResp.getContent()));
    // Invoke Function Async
    invkReq.setInvocationType(Const.INVOCATION_TYPE_ASYNC);
    invkResp = client.invokeFunction(invkReq);
    assertEquals(HttpURLConnection.HTTP_ACCEPTED, invkResp.getStatus());
    if (testTrigger) {
        // Create Trigger
        String tfPrefix = "prefix";
        String tfSuffix = "suffix";
        createTrigger(TRIGGER_NAME, tfPrefix, tfSuffix);
        // List Triggers
        ListTriggersRequest listTReq = new ListTriggersRequest(SERVICE_NAME, FUNCTION_NAME);
        ListTriggersResponse listTResp = client.listTriggers(listTReq);
        assertFalse(Strings.isNullOrEmpty(listTResp.getRequestId()));
        assertEquals(1, listTResp.getTriggers().length);
        TriggerMetadata triggerOld = listTResp.getTriggers()[0];
        assertEquals(TRIGGER_NAME, triggerOld.getTriggerName());
        // Update Trigger
        String newInvocationRole = INVOCATION_ROLE + "_new";
        String tfPrefixNew = "prefix_new";
        String tfSuffixNew = "suffix_new";
        String[] eventsNew = new String[] { "oss:ObjectCreated:PutObject" };
        OSSTriggerConfig updateTriggerConfig = new OSSTriggerConfig(new String[] { "oss:ObjectCreated:PutObject" }, tfPrefixNew, tfSuffixNew);
        UpdateTriggerRequest updateTReq = new UpdateTriggerRequest(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
        updateTReq.setInvocationRole(newInvocationRole);
        updateTReq.setTriggerConfig(updateTriggerConfig);
        UpdateTriggerResponse updateTResp = updateTrigger(updateTReq);
        assertEquals(triggerOld.getTriggerName(), updateTResp.getTriggerName());
        assertNotEquals(triggerOld.getInvocationRole(), updateTResp.getInvocationRole());
        assertEquals(triggerOld.getSourceArn(), updateTResp.getSourceArn());
        Gson gson = new Gson();
        OSSTriggerConfig tcOld = gson.fromJson(gson.toJson(triggerOld.getTriggerConfig()), OSSTriggerConfig.class);
        OSSTriggerConfig tcNew = gson.fromJson(gson.toJson(updateTResp.getTriggerConfig()), OSSTriggerConfig.class);
        assertFalse(Arrays.deepEquals(tcOld.getEvents(), tcNew.getEvents()));
        assertNotEquals(tcOld.getFilter().getKey().getPrefix(), tcNew.getFilter().getKey().getPrefix());
        assertNotEquals(tcOld.getFilter().getKey().getSuffix(), tcNew.getFilter().getKey().getSuffix());
        assertEquals(triggerOld.getCreatedTime(), updateTResp.getCreatedTime());
        assertEquals(triggerOld.getTriggerType(), updateTResp.getTriggerType());
        assertNotEquals(triggerOld.getInvocationRole(), updateTResp.getInvocationRole());
        Date dateOld = DATE_FORMAT.parse(triggerOld.getLastModifiedTime());
        Date dateNew = DATE_FORMAT.parse(updateTResp.getLastModifiedTime());
        assertTrue(dateOld.before(dateNew));
        // Get Trigger
        GetTriggerRequest getTReq = new GetTriggerRequest(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
        GetTriggerResponse getTResp = client.getTrigger(getTReq);
        OSSTriggerConfig getTConfig = gson.fromJson(gson.toJson(getTResp.getTriggerConfig()), OSSTriggerConfig.class);
        assertFalse(Strings.isNullOrEmpty(getTResp.getRequestId()));
        assertEquals(TRIGGER_NAME, getTResp.getTriggerName());
        assertEquals(OSS_SOURCE_ARN, getTResp.getSourceARN());
        assertEquals(TRIGGER_TYPE_OSS, getTResp.getTriggerType());
        assertEquals(newInvocationRole, getTResp.getInvocationRole());
        assertEquals(tfPrefixNew, getTConfig.getFilter().getKey().getPrefix());
        assertEquals(tfSuffixNew, getTConfig.getFilter().getKey().getSuffix());
        assertTrue(Arrays.deepEquals(eventsNew, getTConfig.getEvents()));
        // Delete Trigger
        deleteTrigger(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
    }
    // Delete Function
    DeleteFunctionRequest deleteFReq = new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    int numFunctionsOld = listFResp.getFunctions().length;
    DeleteFunctionResponse deleteFResp = client.deleteFunction(deleteFReq);
    assertFalse(Strings.isNullOrEmpty(deleteFResp.getRequestId()));
    listFResp = client.listFunctions(listFReq);
    try {
        getFunction(FUNCTION_NAME, listFResp.getFunctions());
        fail("Function " + FUNCTION_NAME + " failed to be deleted");
    } catch (RuntimeException e) {
        int numFunctionsNew = (listFResp.getFunctions() == null) ? 0 : listFResp.getFunctions().length;
        assertEquals(numFunctionsOld, numFunctionsNew + 1);
    }
    GetFunctionResponse getFResp2 = null;
    try {
        getFResp2 = client.getFunction(getFReq);
        fail("Get Function " + FUNCTION_NAME + " should have no function returned after delete");
    } catch (ClientException e) {
        assertNull(getFResp2);
    }
    // Delete Service
    DeleteServiceRequest deleteSReq = new DeleteServiceRequest(SERVICE_NAME);
    DeleteServiceResponse deleteSResp = client.deleteService(deleteSReq);
    assertFalse(Strings.isNullOrEmpty(deleteSResp.getRequestId()));
    GetServiceResponse getSResp2 = null;
    try {
        getSResp2 = client.getService(getSReq);
        fail("Get service " + FUNCTION_NAME + " should have no service returned after delete");
    } catch (ClientException e) {
        assertNull(getSResp2);
    }
}
Also used : UpdateTriggerResponse(com.aliyuncs.fc.response.UpdateTriggerResponse) ListFunctionsResponse(com.aliyuncs.fc.response.ListFunctionsResponse) DeleteServiceRequest(com.aliyuncs.fc.request.DeleteServiceRequest) ListTriggersRequest(com.aliyuncs.fc.request.ListTriggersRequest) GetFunctionResponse(com.aliyuncs.fc.response.GetFunctionResponse) Gson(com.google.gson.Gson) DeleteFunctionRequest(com.aliyuncs.fc.request.DeleteFunctionRequest) InvokeFunctionRequest(com.aliyuncs.fc.request.InvokeFunctionRequest) CreateFunctionResponse(com.aliyuncs.fc.response.CreateFunctionResponse) ListTriggersResponse(com.aliyuncs.fc.response.ListTriggersResponse) GetFunctionCodeResponse(com.aliyuncs.fc.response.GetFunctionCodeResponse) GetTriggerResponse(com.aliyuncs.fc.response.GetTriggerResponse) GetFunctionCodeRequest(com.aliyuncs.fc.request.GetFunctionCodeRequest) CreateServiceResponse(com.aliyuncs.fc.response.CreateServiceResponse) DeleteFunctionResponse(com.aliyuncs.fc.response.DeleteFunctionResponse) DeleteServiceResponse(com.aliyuncs.fc.response.DeleteServiceResponse) GetServiceRequest(com.aliyuncs.fc.request.GetServiceRequest) ListFunctionsRequest(com.aliyuncs.fc.request.ListFunctionsRequest) TriggerMetadata(com.aliyuncs.fc.model.TriggerMetadata) InvokeFunctionResponse(com.aliyuncs.fc.response.InvokeFunctionResponse) UpdateTriggerRequest(com.aliyuncs.fc.request.UpdateTriggerRequest) GetTriggerRequest(com.aliyuncs.fc.request.GetTriggerRequest) GetFunctionRequest(com.aliyuncs.fc.request.GetFunctionRequest) OSSTriggerConfig(com.aliyuncs.fc.model.OSSTriggerConfig) UpdateFunctionRequest(com.aliyuncs.fc.request.UpdateFunctionRequest) FunctionMetadata(com.aliyuncs.fc.model.FunctionMetadata) GetServiceResponse(com.aliyuncs.fc.response.GetServiceResponse) UpdateServiceResponse(com.aliyuncs.fc.response.UpdateServiceResponse) ClientException(com.aliyuncs.fc.exceptions.ClientException) UpdateServiceRequest(com.aliyuncs.fc.request.UpdateServiceRequest)

Example 2 with CreateFunctionResponse

use of com.aliyuncs.fc.response.CreateFunctionResponse in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testListFunctions.

@Test
public void testListFunctions() {
    final int numServices = 10;
    final int limit = 3;
    // Create service
    createService(SERVICE_NAME);
    // Create multiple functions under the test service
    for (int i = 0; i < numServices; i++) {
        CreateFunctionResponse createFResp = createFunction(FUNCTION_NAME + i);
        assertFalse(Strings.isNullOrEmpty(createFResp.getRequestId()));
    }
    ListFunctionsRequest listRequest = new ListFunctionsRequest(SERVICE_NAME);
    listRequest.setLimit(limit);
    listRequest.setPrefix(FUNCTION_NAME);
    ListFunctionsResponse listResponse = client.listFunctions(listRequest);
    int numCalled = 1;
    String nextToken = listResponse.getNextToken();
    while (nextToken != null) {
        listRequest.setNextToken(nextToken);
        listResponse = client.listFunctions(listRequest);
        nextToken = listResponse.getNextToken();
        numCalled++;
    }
    assertEquals(numServices / limit + 1, numCalled);
}
Also used : CreateFunctionResponse(com.aliyuncs.fc.response.CreateFunctionResponse) ListFunctionsResponse(com.aliyuncs.fc.response.ListFunctionsResponse) ListFunctionsRequest(com.aliyuncs.fc.request.ListFunctionsRequest) Test(org.junit.Test)

Aggregations

ListFunctionsRequest (com.aliyuncs.fc.request.ListFunctionsRequest)2 CreateFunctionResponse (com.aliyuncs.fc.response.CreateFunctionResponse)2 ListFunctionsResponse (com.aliyuncs.fc.response.ListFunctionsResponse)2 ClientException (com.aliyuncs.fc.exceptions.ClientException)1 FunctionMetadata (com.aliyuncs.fc.model.FunctionMetadata)1 OSSTriggerConfig (com.aliyuncs.fc.model.OSSTriggerConfig)1 TriggerMetadata (com.aliyuncs.fc.model.TriggerMetadata)1 DeleteFunctionRequest (com.aliyuncs.fc.request.DeleteFunctionRequest)1 DeleteServiceRequest (com.aliyuncs.fc.request.DeleteServiceRequest)1 GetFunctionCodeRequest (com.aliyuncs.fc.request.GetFunctionCodeRequest)1 GetFunctionRequest (com.aliyuncs.fc.request.GetFunctionRequest)1 GetServiceRequest (com.aliyuncs.fc.request.GetServiceRequest)1 GetTriggerRequest (com.aliyuncs.fc.request.GetTriggerRequest)1 InvokeFunctionRequest (com.aliyuncs.fc.request.InvokeFunctionRequest)1 ListTriggersRequest (com.aliyuncs.fc.request.ListTriggersRequest)1 UpdateFunctionRequest (com.aliyuncs.fc.request.UpdateFunctionRequest)1 UpdateServiceRequest (com.aliyuncs.fc.request.UpdateServiceRequest)1 UpdateTriggerRequest (com.aliyuncs.fc.request.UpdateTriggerRequest)1 CreateServiceResponse (com.aliyuncs.fc.response.CreateServiceResponse)1 DeleteFunctionResponse (com.aliyuncs.fc.response.DeleteFunctionResponse)1