Search in sources :

Example 1 with HttpMethod

use of com.aliyuncs.fc.model.HttpMethod in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testCRUDHttpTrigger.

@Test
public void testCRUDHttpTrigger() throws ParseException, InterruptedException, IOException {
    // create service
    createService(SERVICE_NAME);
    // Create Function
    createFunction(FUNCTION_NAME);
    // create http trigger
    createHttpTrigger(TRIGGER_NAME, ANONYMOUS, new HttpMethod[] { GET, POST });
    // List Triggers
    TriggerMetadata[] triggers = listTriggers(SERVICE_NAME, FUNCTION_NAME);
    assertEquals(1, triggers.length);
    TriggerMetadata trigger = triggers[0];
    assertEquals(TRIGGER_NAME, trigger.getTriggerName());
    assertEquals("http", trigger.getTriggerType());
    // retrieve http trigger
    GetTriggerRequest getTReq = new GetTriggerRequest(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
    GetTriggerResponse getTResp = client.getTrigger(getTReq);
    HttpTriggerConfig triggerConfig = gson.fromJson(gson.toJson(getTResp.getTriggerConfig()), HttpTriggerConfig.class);
    assertFalse(Strings.isNullOrEmpty(getTResp.getRequestId()));
    assertEquals(TRIGGER_NAME, getTResp.getTriggerName());
    assertEquals(TRIGGER_TYPE_HTTP, getTResp.getTriggerType());
    assertTrue(deepEquals(new HttpMethod[] { GET, POST }, triggerConfig.getMethods()));
    // update http trigger
    GetTriggerResponse triggerOld = getTResp;
    HttpTriggerConfig updateTriggerConfig = new HttpTriggerConfig(FUNCTION, new HttpMethod[] { POST });
    UpdateTriggerRequest updateTReq = new UpdateTriggerRequest(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
    updateTReq.setTriggerConfig(updateTriggerConfig);
    Thread.sleep(1000);
    UpdateTriggerResponse updateTResp = updateTrigger(updateTReq);
    assertEquals(triggerOld.getTriggerName(), updateTResp.getTriggerName());
    Gson gson = new Gson();
    HttpTriggerConfig tcOld = gson.fromJson(gson.toJson(triggerOld.getTriggerConfig()), HttpTriggerConfig.class);
    HttpTriggerConfig tcNew = gson.fromJson(gson.toJson(updateTResp.getTriggerConfig()), HttpTriggerConfig.class);
    assertFalse(deepEquals(tcOld.getMethods(), tcNew.getMethods()));
    assertNotEquals(tcOld.getAuthType(), tcNew.getAuthType());
    assertEquals(triggerOld.getCreatedTime(), updateTResp.getCreatedTime());
    assertEquals(triggerOld.getTriggerType(), updateTResp.getTriggerType());
    Date dateOld = DATE_FORMAT.parse(triggerOld.getLastModifiedTime());
    Date dateNew = DATE_FORMAT.parse(updateTResp.getLastModifiedTime());
    assertTrue(dateOld.before(dateNew));
    // delete http trigger
    deleteTrigger(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
    getTReq = new GetTriggerRequest(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
    try {
        client.getTrigger(getTReq);
    } catch (ClientException e) {
        assertEquals(404, e.getStatusCode());
    }
    cleanUpFunctions(SERVICE_NAME);
    cleanupService(SERVICE_NAME);
}
Also used : Gson(com.google.gson.Gson) ClientException(com.aliyuncs.fc.exceptions.ClientException) HttpMethod(com.aliyuncs.fc.model.HttpMethod)

Aggregations

ClientException (com.aliyuncs.fc.exceptions.ClientException)1 HttpMethod (com.aliyuncs.fc.model.HttpMethod)1 Gson (com.google.gson.Gson)1