Search in sources :

Example 1 with ActionImpl

use of org.alfresco.repo.action.ActionImpl in project alfresco-remote-api by Alfresco.

the class RunningActionRestApiTest method testRunningActionsGet.

public void testRunningActionsGet() throws Exception {
    Response response;
    // Not allowed if you're not an admin
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    // If nothing running, you don't get anything back
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    String jsonStr = response.getContentAsString();
    JSONObject json = new JSONObject(jsonStr);
    JSONArray results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(0, results.length());
    // Add a running action, it should show up
    ReplicationDefinition rd = replicationService.createReplicationDefinition("Test1", "Testing");
    replicationService.saveReplicationDefinition(rd);
    actionTrackingService.recordActionExecuting(rd);
    String id = rd.getId();
    String instance = Integer.toString(((ActionImpl) rd).getExecutionInstance());
    String startedAt = ISO8601DateFormat.format(rd.getExecutionStartDate());
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(1, results.length());
    JSONObject jsonRD = (JSONObject) results.get(0);
    assertNotNull(jsonRD);
    assertEquals(id, jsonRD.get("actionId"));
    assertEquals(ReplicationDefinitionImpl.EXECUTOR_NAME, jsonRD.get("actionType"));
    assertEquals(instance, jsonRD.get("actionInstance"));
    assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
    assertEquals(startedAt, jsonRD.get("startedAt"));
    assertEquals(false, jsonRD.getBoolean("cancelRequested"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + id + "=" + instance, jsonRD.get("details"));
    // Ensure we didn't get any unexpected data back,
    // only the keys we should have done
    JSONArray keys = jsonRD.names();
    for (int i = 0; i < keys.length(); i++) {
        String key = keys.getString(0);
        if (key.equals("actionId") || key.equals("actionType") || key.equals("actionInstance") || key.equals("actionNodeRef") || key.equals("startedAt") || key.equals("cancelRequested") || key.equals("details")) {
        // All good
        } else {
            fail("Unexpected key '" + key + "' found in json, raw json is\n" + jsonStr);
        }
    }
    // Change the status to pending cancel, and re-check
    actionTrackingService.requestActionCancellation(rd);
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(1, results.length());
    jsonRD = (JSONObject) results.get(0);
    assertNotNull(jsonRD);
    assertEquals(id, jsonRD.get("actionId"));
    assertEquals(ReplicationDefinitionImpl.EXECUTOR_NAME, jsonRD.get("actionType"));
    assertEquals(instance, jsonRD.get("actionInstance"));
    assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
    assertEquals(startedAt, jsonRD.get("startedAt"));
    assertEquals(true, jsonRD.getBoolean("cancelRequested"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + id + "=" + instance, jsonRD.get("details"));
    // Add a 2nd and 3rd - one pending, one running
    rd = replicationService.createReplicationDefinition("Test2", "2nd Testing");
    replicationService.saveReplicationDefinition(rd);
    actionTrackingService.recordActionExecuting(rd);
    String id2 = rd.getId();
    String instance2 = Integer.toString(((ActionImpl) rd).getExecutionInstance());
    String startedAt2 = ISO8601DateFormat.format(rd.getExecutionStartDate());
    rd = replicationService.createReplicationDefinition("AnotherTest", "3rd Testing");
    replicationService.saveReplicationDefinition(rd);
    actionTrackingService.recordActionPending(rd);
    String id3 = rd.getId();
    // Check we got all 3
    boolean has1 = false;
    boolean has2 = false;
    boolean has3 = false;
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(3, results.length());
    for (int i = 0; i < 3; i++) {
        jsonRD = (JSONObject) results.get(i);
        if (jsonRD.get("actionId").equals(id)) {
            has1 = true;
        }
        if (jsonRD.get("actionId").equals(id2)) {
            has2 = true;
        }
        if (jsonRD.get("actionId").equals(id3)) {
            has3 = true;
        }
    }
    assertTrue(has1);
    assertTrue(has2);
    assertTrue(has3);
    // Remove one, check it goes
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    actionTrackingService.recordActionComplete(rd);
    txn.commit();
    has1 = false;
    has2 = false;
    has3 = false;
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(2, results.length());
    for (int i = 0; i < 2; i++) {
        jsonRD = (JSONObject) results.get(i);
        if (jsonRD.get("actionId").equals(id)) {
            has1 = true;
        }
        if (jsonRD.get("actionId").equals(id2)) {
            has2 = true;
        }
        if (jsonRD.get("actionId").equals(id3)) {
            has3 = true;
        }
    }
    assertTrue(has1);
    assertTrue(has2);
    assertFalse(has3);
    // Check we correctly filter by node ID
    rd = replicationService.loadReplicationDefinition("Test1");
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS + "?nodeRef=" + rd.getNodeRef().toString()), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(1, results.length());
    jsonRD = (JSONObject) results.get(0);
    assertNotNull(jsonRD);
    assertEquals(id, jsonRD.get("actionId"));
    assertEquals(ReplicationDefinitionImpl.EXECUTOR_NAME, jsonRD.get("actionType"));
    assertEquals(instance, jsonRD.get("actionInstance"));
    assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
    assertEquals(startedAt, jsonRD.get("startedAt"));
    assertEquals(true, jsonRD.getBoolean("cancelRequested"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + id + "=" + instance, jsonRD.get("details"));
    // Check the other one
    rd = replicationService.loadReplicationDefinition("Test2");
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS + "?nodeRef=" + rd.getNodeRef().toString()), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(1, results.length());
    jsonRD = (JSONObject) results.get(0);
    assertNotNull(jsonRD);
    assertEquals(id2, jsonRD.get("actionId"));
    assertEquals(ReplicationDefinitionImpl.EXECUTOR_NAME, jsonRD.get("actionType"));
    assertEquals(instance2, jsonRD.get("actionInstance"));
    assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
    assertEquals(startedAt2, jsonRD.get("startedAt"));
    assertEquals(false, jsonRD.getBoolean("cancelRequested"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + id2 + "=" + instance2, jsonRD.get("details"));
    // Check we correctly filter by type
    ActionImpl alt1 = new ActionImpl(null, "12345", "MadeUp1");
    ActionImpl alt2 = new ActionImpl(null, "54321", "MadeUp2");
    actionTrackingService.recordActionExecuting(alt1);
    actionTrackingService.recordActionExecuting(alt2);
    String startAtAlt2 = ISO8601DateFormat.format(alt2.getExecutionStartDate());
    String instanceAlt2 = Integer.toString(alt2.getExecutionInstance());
    // Goes up to 4 not by type
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(4, results.length());
    // 2 replication actions
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS + "?type=replicationActionExecutor"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(2, results.length());
    // 0 if doesn't exist
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS + "?type=MadeUp4"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(0, results.length());
    // 1 each of the made up ones
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS + "?type=MadeUp1"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(1, results.length());
    response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS + "?type=MadeUp2"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(1, results.length());
    // Check the details of one of these
    jsonRD = (JSONObject) results.get(0);
    assertNotNull(jsonRD);
    assertEquals("54321", jsonRD.get("actionId"));
    assertEquals("MadeUp2", jsonRD.get("actionType"));
    assertEquals(instanceAlt2, jsonRD.get("actionInstance"));
    assertEquals(JSONObject.NULL, jsonRD.get("actionNodeRef"));
    assertEquals(startAtAlt2, jsonRD.get("startedAt"));
    assertEquals(false, jsonRD.getBoolean("cancelRequested"));
    assertEquals("/" + URL_RUNNING_ACTION + "MadeUp2=54321=" + instanceAlt2, jsonRD.get("details"));
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) UserTransaction(javax.transaction.UserTransaction) JSONObject(org.json.JSONObject) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray) ActionImpl(org.alfresco.repo.action.ActionImpl)

Example 2 with ActionImpl

use of org.alfresco.repo.action.ActionImpl in project alfresco-remote-api by Alfresco.

the class RunningActionRestApiTest method testRunningReplicationActionsGet.

public void testRunningReplicationActionsGet() throws Exception {
    Response response;
    // Not allowed if you're not an admin
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
    response = sendRequest(new GetRequest(URL_RUNNING_REPLICATION_ACTIONS), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
    response = sendRequest(new GetRequest(URL_RUNNING_REPLICATION_ACTIONS), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    // If nothing running, you don't get anything back
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    response = sendRequest(new GetRequest(URL_RUNNING_REPLICATION_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    String jsonStr = response.getContentAsString();
    JSONObject json = new JSONObject(jsonStr);
    JSONArray results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(0, results.length());
    // Add a running action, it should show up
    ReplicationDefinition rd = replicationService.createReplicationDefinition("Test1", "Testing");
    replicationService.saveReplicationDefinition(rd);
    actionTrackingService.recordActionExecuting(rd);
    String id = rd.getId();
    String instance = Integer.toString(((ActionImpl) rd).getExecutionInstance());
    String startedAt = ISO8601DateFormat.format(rd.getExecutionStartDate());
    response = sendRequest(new GetRequest(URL_RUNNING_REPLICATION_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(1, results.length());
    JSONObject jsonRD = (JSONObject) results.get(0);
    assertNotNull(jsonRD);
    assertEquals(id, jsonRD.get("actionId"));
    assertEquals(ReplicationDefinitionImpl.EXECUTOR_NAME, jsonRD.get("actionType"));
    assertEquals(instance, jsonRD.get("actionInstance"));
    assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
    assertEquals(startedAt, jsonRD.get("startedAt"));
    assertEquals(false, jsonRD.getBoolean("cancelRequested"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + id + "=" + instance, jsonRD.get("details"));
    // Ensure we didn't get any unexpected data back,
    // only the keys we should have done
    JSONArray keys = jsonRD.names();
    for (int i = 0; i < keys.length(); i++) {
        String key = keys.getString(0);
        if (key.equals("actionId") || key.equals("actionType") || key.equals("actionInstance") || key.equals("actionNodeRef") || key.equals("startedAt") || key.equals("cancelRequested") || key.equals("details")) {
        // All good
        } else {
            fail("Unexpected key '" + key + "' found in json, raw json is\n" + jsonStr);
        }
    }
    // Change the status to pending cancel, and re-check
    actionTrackingService.requestActionCancellation(rd);
    response = sendRequest(new GetRequest(URL_RUNNING_REPLICATION_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(1, results.length());
    jsonRD = (JSONObject) results.get(0);
    assertNotNull(jsonRD);
    assertEquals(id, jsonRD.get("actionId"));
    assertEquals(ReplicationDefinitionImpl.EXECUTOR_NAME, jsonRD.get("actionType"));
    assertEquals(instance, jsonRD.get("actionInstance"));
    assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
    assertEquals(startedAt, jsonRD.get("startedAt"));
    assertEquals(true, jsonRD.getBoolean("cancelRequested"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + id + "=" + instance, jsonRD.get("details"));
    // Add a 2nd and 3rd, one running and one pending
    rd = replicationService.createReplicationDefinition("Test2", "2nd Testing");
    replicationService.saveReplicationDefinition(rd);
    actionTrackingService.recordActionExecuting(rd);
    String id2 = rd.getId();
    @SuppressWarnings("unused") String instance2 = Integer.toString(((ActionImpl) rd).getExecutionInstance());
    @SuppressWarnings("unused") String startedAt2 = ISO8601DateFormat.format(rd.getExecutionStartDate());
    rd = replicationService.createReplicationDefinition("AnotherTest", "3rd Testing");
    replicationService.saveReplicationDefinition(rd);
    actionTrackingService.recordActionPending(rd);
    String id3 = rd.getId();
    // Check we got all 3
    boolean has1 = false;
    boolean has2 = false;
    boolean has3 = false;
    response = sendRequest(new GetRequest(URL_RUNNING_REPLICATION_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(3, results.length());
    for (int i = 0; i < 3; i++) {
        jsonRD = (JSONObject) results.get(i);
        if (jsonRD.get("actionId").equals(id)) {
            has1 = true;
        }
        if (jsonRD.get("actionId").equals(id2)) {
            has2 = true;
        }
        if (jsonRD.get("actionId").equals(id3)) {
            has3 = true;
        }
    }
    assertTrue(has1);
    assertTrue(has2);
    assertTrue(has3);
    // Remove one, check it goes
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    actionTrackingService.recordActionComplete(rd);
    txn.commit();
    has1 = false;
    has2 = false;
    has3 = false;
    response = sendRequest(new GetRequest(URL_RUNNING_REPLICATION_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(2, results.length());
    for (int i = 0; i < 2; i++) {
        jsonRD = (JSONObject) results.get(i);
        if (jsonRD.get("actionId").equals(id)) {
            has1 = true;
        }
        if (jsonRD.get("actionId").equals(id2)) {
            has2 = true;
        }
        if (jsonRD.get("actionId").equals(id3)) {
            has3 = true;
        }
    }
    assertTrue(has1);
    assertTrue(has2);
    assertFalse(has3);
    // Check we correctly filter by name
    rd = replicationService.loadReplicationDefinition("Test1");
    response = sendRequest(new GetRequest(URL_RUNNING_REPLICATION_ACTIONS + "?name=Test1"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(1, results.length());
    jsonRD = (JSONObject) results.get(0);
    assertNotNull(jsonRD);
    assertEquals(id, jsonRD.get("actionId"));
    assertEquals(ReplicationDefinitionImpl.EXECUTOR_NAME, jsonRD.get("actionType"));
    assertEquals(instance, jsonRD.get("actionInstance"));
    assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
    assertEquals(startedAt, jsonRD.get("startedAt"));
    assertEquals(true, jsonRD.getBoolean("cancelRequested"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + id + "=" + instance, jsonRD.get("details"));
    // Check we correctly filter by type
    ActionImpl alt1 = new ActionImpl(null, "12345", "MadeUp1");
    ActionImpl alt2 = new ActionImpl(null, "54321", "MadeUp2");
    actionTrackingService.recordActionExecuting(alt1);
    actionTrackingService.recordActionExecuting(alt2);
    @SuppressWarnings("unused") String startAtAlt2 = ISO8601DateFormat.format(alt2.getExecutionStartDate());
    @SuppressWarnings("unused") String instanceAlt2 = Integer.toString(alt2.getExecutionInstance());
    // 2 replication actions
    response = sendRequest(new GetRequest(URL_RUNNING_REPLICATION_ACTIONS), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr);
    results = json.getJSONArray("data");
    assertNotNull(results);
    assertEquals(2, results.length());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) UserTransaction(javax.transaction.UserTransaction) JSONObject(org.json.JSONObject) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray) ActionImpl(org.alfresco.repo.action.ActionImpl)

Example 3 with ActionImpl

use of org.alfresco.repo.action.ActionImpl in project alfresco-remote-api by Alfresco.

the class ReplicationRestApiTest method testReplicationDefinitionGet.

public void testReplicationDefinitionGet() throws Exception {
    Response response;
    // Not allowed if you're not an admin
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
    response = sendRequest(new GetRequest(URL_DEFINITION + "madeup"), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
    response = sendRequest(new GetRequest(URL_DEFINITION + "madeup"), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    // If an invalid name is given, you get a 404
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    response = sendRequest(new GetRequest(URL_DEFINITION + "madeup"), 404);
    assertEquals(Status.STATUS_NOT_FOUND, response.getStatus());
    // Add a definition, it should show up
    ReplicationDefinition rd = replicationService.createReplicationDefinition("Test1", "Testing");
    replicationService.saveReplicationDefinition(rd);
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test1"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    String jsonStr = response.getContentAsString();
    JSONObject json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    // Check
    assertEquals("Test1", json.get("name"));
    assertEquals("Testing", json.get("description"));
    assertEquals("New", json.get("status"));
    assertEquals(JSONObject.NULL, json.get("startedAt"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals(JSONObject.NULL, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    // Payload is empty
    assertEquals(0, json.getJSONArray("payload").length());
    // Ensure we didn't get any unexpected data back
    JSONArray keys = json.names();
    for (int i = 0; i < keys.length(); i++) {
        String key = keys.getString(0);
        if (key.equals("name") || key.equals("description") || key.equals("status") || key.equals("startedAt") || key.equals("endedAt") || key.equals("failureMessage") || key.equals("executionDetails") || key.equals("payload") || key.equals("transferLocalReport") || key.equals("transferRemoteReport") || key.equals("enabled") || key.equals("targetName") || key.equals("schedule")) {
        // All good
        } else {
            fail("Unexpected key '" + key + "' found in json, raw json is\n" + jsonStr);
        }
    }
    // Mark it as pending, and check
    actionTrackingService.recordActionPending(rd);
    String actionId = rd.getId();
    int instanceId = ((ActionImpl) rd).getExecutionInstance();
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test1"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertEquals("Test1", json.get("name"));
    assertEquals("Testing", json.get("description"));
    assertEquals("Pending", json.get("status"));
    assertEquals(JSONObject.NULL, json.get("startedAt"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    // Payload is empty
    assertEquals(0, json.getJSONArray("payload").length());
    // Change the status to running, and re-check
    actionTrackingService.recordActionExecuting(rd);
    assertEquals(actionId, rd.getId());
    assertEquals(instanceId, ((ActionImpl) rd).getExecutionInstance());
    String startedAt = ISO8601DateFormat.format(rd.getExecutionStartDate());
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test1"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertEquals("Test1", json.get("name"));
    assertEquals("Testing", json.get("description"));
    assertEquals("Running", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    // Payload is empty
    assertEquals(0, json.getJSONArray("payload").length());
    // Cancel it
    actionTrackingService.requestActionCancellation(rd);
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test1"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertEquals("Test1", json.get("name"));
    assertEquals("Testing", json.get("description"));
    assertEquals("CancelRequested", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    // Payload is empty
    assertEquals(0, json.getJSONArray("payload").length());
    // Add some payload details, ensure that they get expanded
    // as they should be
    rd.getPayload().add(repositoryHelper.getCompanyHome());
    rd.getPayload().add(dataDictionary);
    replicationService.saveReplicationDefinition(rd);
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test1"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertEquals("Test1", json.get("name"));
    assertEquals("Testing", json.get("description"));
    assertEquals("CancelRequested", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    // Check Payload
    assertEquals(2, json.getJSONArray("payload").length());
    JSONObject payload = json.getJSONArray("payload").getJSONObject(0);
    assertEquals(repositoryHelper.getCompanyHome().toString(), payload.get("nodeRef"));
    assertEquals(true, payload.get("isFolder"));
    assertEquals("Company Home", payload.get("name"));
    assertEquals("/Company Home", payload.get("path"));
    payload = json.getJSONArray("payload").getJSONObject(1);
    assertEquals(dataDictionary.toString(), payload.get("nodeRef"));
    assertEquals(true, payload.get("isFolder"));
    assertEquals("Data Dictionary", payload.get("name"));
    assertEquals("/Company Home/Data Dictionary", payload.get("path"));
    // Add a deleted NodeRef too, will be silently ignored
    // by the webscript layer
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    NodeRef deleted = nodeService.createNode(dataDictionary, ContentModel.ASSOC_CONTAINS, QName.createQName("IwillBEdeleted"), ContentModel.TYPE_CONTENT).getChildRef();
    nodeService.deleteNode(deleted);
    txn.commit();
    rd.getPayload().add(deleted);
    replicationService.saveReplicationDefinition(rd);
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test1"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertEquals("Test1", json.get("name"));
    assertEquals("Testing", json.get("description"));
    assertEquals("CancelRequested", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    // Check Payload
    assertEquals(2, json.getJSONArray("payload").length());
    payload = json.getJSONArray("payload").getJSONObject(0);
    assertEquals("Company Home", payload.get("name"));
    payload = json.getJSONArray("payload").getJSONObject(1);
    assertEquals("Data Dictionary", payload.get("name"));
    // Add a 2nd and 3rd definition
    rd = replicationService.createReplicationDefinition("Test2", "2nd Testing");
    replicationService.saveReplicationDefinition(rd);
    rd = replicationService.createReplicationDefinition("Test3", "3rd Testing");
    rd.setLocalTransferReport(repositoryHelper.getRootHome());
    rd.setRemoteTransferReport(repositoryHelper.getCompanyHome());
    rd.setEnabled(false);
    // Have the 3rd one flagged as having failed
    txn = transactionService.getUserTransaction();
    txn.begin();
    replicationService.saveReplicationDefinition(rd);
    actionTrackingService.recordActionExecuting(rd);
    actionTrackingService.recordActionFailure(rd, new Exception("Test Failure"));
    txn.commit();
    Thread.sleep(50);
    replicationService.saveReplicationDefinition(rd);
    // Original one comes back unchanged
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test1"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertEquals("Test1", json.get("name"));
    assertEquals("Testing", json.get("description"));
    assertEquals("CancelRequested", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    // Check Payload
    assertEquals(2, json.getJSONArray("payload").length());
    payload = json.getJSONArray("payload").getJSONObject(0);
    assertEquals(repositoryHelper.getCompanyHome().toString(), payload.get("nodeRef"));
    assertEquals(true, payload.get("isFolder"));
    assertEquals("Company Home", payload.get("name"));
    assertEquals("/Company Home", payload.get("path"));
    payload = json.getJSONArray("payload").getJSONObject(1);
    assertEquals(dataDictionary.toString(), payload.get("nodeRef"));
    assertEquals(true, payload.get("isFolder"));
    assertEquals("Data Dictionary", payload.get("name"));
    assertEquals("/Company Home/Data Dictionary", payload.get("path"));
    // They show up things as expected
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test2"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertEquals("Test2", json.get("name"));
    assertEquals("2nd Testing", json.get("description"));
    assertEquals("New", json.get("status"));
    assertEquals(JSONObject.NULL, json.get("startedAt"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals(JSONObject.NULL, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    // And the 3rd one, which is failed
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test3"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    startedAt = ISO8601DateFormat.format(rd.getExecutionStartDate());
    String endedAt = ISO8601DateFormat.format(rd.getExecutionEndDate());
    assertEquals("Test3", json.get("name"));
    assertEquals("3rd Testing", json.get("description"));
    assertEquals("Failed", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(endedAt, json.getJSONObject("endedAt").get("iso8601"));
    assertEquals("Test Failure", json.get("failureMessage"));
    assertEquals(JSONObject.NULL, json.get("executionDetails"));
    assertEquals(repositoryHelper.getRootHome().toString(), json.get("transferLocalReport"));
    assertEquals(repositoryHelper.getCompanyHome().toString(), json.get("transferRemoteReport"));
    assertEquals(false, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    // When pending/running, the previous end time, transfer reports and
    // failure details are hidden
    rd = replicationService.loadReplicationDefinition("Test3");
    assertEquals(0, actionTrackingService.getExecutingActions(rd).size());
    actionTrackingService.recordActionPending(rd);
    assertEquals(1, actionTrackingService.getExecutingActions(rd).size());
    instanceId = ((ActionImpl) rd).getExecutionInstance();
    actionId = rd.getId();
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test3"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertEquals("Test3", json.get("name"));
    assertEquals("3rd Testing", json.get("description"));
    assertEquals("Pending", json.get("status"));
    assertEquals(JSONObject.NULL, json.get("startedAt"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(false, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    actionTrackingService.recordActionExecuting(rd);
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test3"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    startedAt = ISO8601DateFormat.format(rd.getExecutionStartDate());
    assertEquals("Test3", json.get("name"));
    assertEquals("3rd Testing", json.get("description"));
    assertEquals("Running", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(false, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    actionTrackingService.requestActionCancellation(rd);
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test3"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    startedAt = ISO8601DateFormat.format(rd.getExecutionStartDate());
    assertEquals("Test3", json.get("name"));
    assertEquals("3rd Testing", json.get("description"));
    assertEquals("CancelRequested", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(false, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    // These show up again when no longer running
    txn = transactionService.getUserTransaction();
    txn.begin();
    actionTrackingService.recordActionComplete(rd);
    txn.commit();
    response = sendRequest(new GetRequest(URL_DEFINITION + "Test3"), 200);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    startedAt = ISO8601DateFormat.format(rd.getExecutionStartDate());
    endedAt = ISO8601DateFormat.format(rd.getExecutionEndDate());
    assertEquals("Test3", json.get("name"));
    assertEquals("3rd Testing", json.get("description"));
    assertEquals("Completed", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(endedAt, json.getJSONObject("endedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals(JSONObject.NULL, json.get("executionDetails"));
    assertEquals(repositoryHelper.getRootHome().toString(), json.get("transferLocalReport"));
    assertEquals(repositoryHelper.getCompanyHome().toString(), json.get("transferRemoteReport"));
    assertEquals(false, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) UserTransaction(javax.transaction.UserTransaction) NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.JSONObject) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) GetRequest(org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest) JSONArray(org.json.JSONArray) ActionImpl(org.alfresco.repo.action.ActionImpl)

Example 4 with ActionImpl

use of org.alfresco.repo.action.ActionImpl in project alfresco-remote-api by Alfresco.

the class ReplicationRestApiTest method testReplicationDefinitionPut.

public void testReplicationDefinitionPut() throws Exception {
    Response response;
    // Not allowed if you're not an admin
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
    response = sendRequest(new PutRequest(URL_DEFINITION + "MadeUp", "", JSON), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
    response = sendRequest(new PutRequest(URL_DEFINITION + "MadeUp", "", JSON), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    // Ensure there aren't any to start with
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    assertEquals(0, replicationService.loadReplicationDefinitions().size());
    // You need to specify a real definition
    response = sendRequest(new PutRequest(URL_DEFINITION + "MadeUp", "", JSON), Status.STATUS_NOT_FOUND);
    assertEquals(Status.STATUS_NOT_FOUND, response.getStatus());
    // Create one, and change it
    ReplicationDefinition rd = replicationService.createReplicationDefinition("Test", "Testing");
    replicationService.saveReplicationDefinition(rd);
    response = sendRequest(new PutRequest(URL_DEFINITION + "Test", "{}", JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    // Check we got the right information back on it
    String jsonStr = response.getContentAsString();
    JSONObject json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals("Test", json.get("name"));
    assertEquals("Testing", json.get("description"));
    assertEquals("New", json.get("status"));
    assertEquals(JSONObject.NULL, json.get("startedAt"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals(JSONObject.NULL, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    // Ensure we didn't get any unexpected data back
    JSONArray keys = json.names();
    for (int i = 0; i < keys.length(); i++) {
        String key = keys.getString(0);
        if (key.equals("name") || key.equals("description") || key.equals("status") || key.equals("startedAt") || key.equals("endedAt") || key.equals("failureMessage") || key.equals("executionDetails") || key.equals("payload") || key.equals("transferLocalReport") || key.equals("transferRemoteReport") || key.equals("enabled") || key.equals("targetName") || key.equals("schedule")) {
        // All good
        } else {
            fail("Unexpected key '" + key + "' found in json, raw json is\n" + jsonStr);
        }
    }
    // Change some details, and see them updated in both
    // the JSON and on the object in the repo
    json = new JSONObject();
    json.put("description", "Updated Description");
    json.put("enabled", false);
    response = sendRequest(new PutRequest(URL_DEFINITION + "Test", json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals("Test", json.get("name"));
    assertEquals("Updated Description", json.get("description"));
    assertEquals("New", json.get("status"));
    assertEquals(JSONObject.NULL, json.get("startedAt"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals(JSONObject.NULL, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(false, json.get("enabled"));
    assertEquals(JSONObject.NULL, json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    rd = replicationService.loadReplicationDefinition("Test");
    assertEquals("Test", rd.getReplicationName());
    assertEquals("Updated Description", rd.getDescription());
    assertEquals(ActionStatus.New, rd.getExecutionStatus());
    assertEquals(null, rd.getExecutionStartDate());
    assertEquals(null, rd.getExecutionEndDate());
    assertEquals(null, rd.getExecutionFailureMessage());
    assertEquals(null, rd.getLocalTransferReport());
    assertEquals(null, rd.getRemoteTransferReport());
    assertEquals(null, rd.getTargetName());
    assertEquals(0, rd.getPayload().size());
    assertEquals(false, rd.isEnabled());
    // Create a 2nd definition, and check that the correct
    // one gets updated
    rd = replicationService.createReplicationDefinition("Test2", "Testing2");
    rd.setTargetName("Target");
    replicationService.saveReplicationDefinition(rd);
    json = new JSONObject();
    json.put("description", "Updated Description 2");
    json.put("enabled", false);
    response = sendRequest(new PutRequest(URL_DEFINITION + "Test2", json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    // Check the response we got
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals("Test2", json.get("name"));
    assertEquals("Updated Description 2", json.get("description"));
    assertEquals("New", json.get("status"));
    assertEquals(JSONObject.NULL, json.get("startedAt"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals(JSONObject.NULL, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(false, json.get("enabled"));
    assertEquals("Target", json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    // Check the 1st definition
    rd = replicationService.loadReplicationDefinition("Test");
    assertEquals("Test", rd.getReplicationName());
    assertEquals("Updated Description", rd.getDescription());
    assertEquals(ActionStatus.New, rd.getExecutionStatus());
    assertEquals(null, rd.getExecutionStartDate());
    assertEquals(null, rd.getExecutionEndDate());
    assertEquals(null, rd.getExecutionFailureMessage());
    assertEquals(null, rd.getLocalTransferReport());
    assertEquals(null, rd.getRemoteTransferReport());
    assertEquals(null, rd.getTargetName());
    assertEquals(0, rd.getPayload().size());
    assertEquals(false, rd.isEnabled());
    // Check the 2nd definition
    rd = replicationService.loadReplicationDefinition("Test2");
    assertEquals("Test2", rd.getReplicationName());
    assertEquals("Updated Description 2", rd.getDescription());
    assertEquals(ActionStatus.New, rd.getExecutionStatus());
    assertEquals(null, rd.getExecutionStartDate());
    assertEquals(null, rd.getExecutionEndDate());
    assertEquals(null, rd.getExecutionFailureMessage());
    assertEquals(null, rd.getLocalTransferReport());
    assertEquals(null, rd.getRemoteTransferReport());
    assertEquals("Target", rd.getTargetName());
    assertEquals(0, rd.getPayload().size());
    assertEquals(false, rd.isEnabled());
    // Mark it as running, then change some details and
    // see it change as expected
    rd = replicationService.loadReplicationDefinition("Test");
    actionTrackingService.recordActionExecuting(rd);
    replicationService.saveReplicationDefinition(rd);
    String startedAt = ISO8601DateFormat.format(rd.getExecutionStartDate());
    String actionId = rd.getId();
    int instanceId = ((ActionImpl) rd).getExecutionInstance();
    json = new JSONObject();
    json.put("enabled", true);
    json.put("targetName", "Another Target");
    response = sendRequest(new PutRequest(URL_DEFINITION + "Test", json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals("Test", json.get("name"));
    assertEquals("Updated Description", json.get("description"));
    assertEquals("Running", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals("Another Target", json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    // Change the payload, and see the right information in
    // the response JSON for it
    JSONArray payloadRefs = new JSONArray();
    payloadRefs.put(repositoryHelper.getCompanyHome().toString());
    payloadRefs.put(dataDictionary.toString());
    json = new JSONObject();
    json.put("payload", payloadRefs);
    response = sendRequest(new PutRequest(URL_DEFINITION + "Test", json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals("Test", json.get("name"));
    assertEquals("Updated Description", json.get("description"));
    assertEquals("Running", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals("Another Target", json.get("targetName"));
    assertEquals(2, json.getJSONArray("payload").length());
    JSONObject payload = json.getJSONArray("payload").getJSONObject(0);
    assertEquals(repositoryHelper.getCompanyHome().toString(), payload.get("nodeRef"));
    assertEquals(true, payload.get("isFolder"));
    assertEquals("Company Home", payload.get("name"));
    assertEquals("/Company Home", payload.get("path"));
    payload = json.getJSONArray("payload").getJSONObject(1);
    assertEquals(dataDictionary.toString(), payload.get("nodeRef"));
    assertEquals(true, payload.get("isFolder"));
    assertEquals("Data Dictionary", payload.get("name"));
    assertEquals("/Company Home/Data Dictionary", payload.get("path"));
    // Remove the payload again
    json = new JSONObject();
    payloadRefs = new JSONArray();
    json.put("payload", payloadRefs);
    response = sendRequest(new PutRequest(URL_DEFINITION + "Test", json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals("Test", json.get("name"));
    assertEquals("Updated Description", json.get("description"));
    assertEquals("Running", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals("Another Target", json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    // Rename to a taken name, won't be allowed
    json = new JSONObject();
    json.put("name", "Test2");
    response = sendRequest(new PutRequest(URL_DEFINITION + "Test", json.toString(), JSON), Status.STATUS_BAD_REQUEST);
    assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus());
    // Rename to a spare name, will be updated
    json = new JSONObject();
    json.put("name", "Renamed");
    response = sendRequest(new PutRequest(URL_DEFINITION + "Test", json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals("Renamed", json.get("name"));
    assertEquals("Updated Description", json.get("description"));
    assertEquals("Running", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals("Another Target", json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
    // Check the repo too
    assertEquals(null, replicationService.loadReplicationDefinition("Test"));
    assertNotNull(replicationService.loadReplicationDefinition("Renamed"));
    // Rename can both rename + change details
    json = new JSONObject();
    json.put("name", "Renamed Again");
    json.put("description", "Was Renamed");
    json.put("targetName", "New Target");
    response = sendRequest(new PutRequest(URL_DEFINITION + "Renamed", json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    jsonStr = response.getContentAsString();
    json = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(json);
    assertEquals("Renamed Again", json.get("name"));
    assertEquals("Was Renamed", json.get("description"));
    assertEquals("Running", json.get("status"));
    assertEquals(startedAt, json.getJSONObject("startedAt").get("iso8601"));
    assertEquals(JSONObject.NULL, json.get("endedAt"));
    assertEquals(JSONObject.NULL, json.get("failureMessage"));
    assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" + actionId + "=" + instanceId, json.get("executionDetails"));
    assertEquals(JSONObject.NULL, json.get("transferLocalReport"));
    assertEquals(JSONObject.NULL, json.get("transferRemoteReport"));
    assertEquals(true, json.get("enabled"));
    assertEquals("New Target", json.get("targetName"));
    assertEquals(0, json.getJSONArray("payload").length());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) JSONArray(org.json.JSONArray) ActionImpl(org.alfresco.repo.action.ActionImpl) PutRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)

Example 5 with ActionImpl

use of org.alfresco.repo.action.ActionImpl in project alfresco-remote-api by Alfresco.

the class AbstractRuleWebScript method parseJsonAction.

protected ActionImpl parseJsonAction(JSONObject jsonAction) throws JSONException {
    ActionImpl result = null;
    String actionId = jsonAction.has("id") ? jsonAction.getString("id") : GUID.generate();
    if (jsonAction.getString("actionDefinitionName").equalsIgnoreCase("composite-action")) {
        result = new CompositeActionImpl(null, actionId);
    } else {
        result = new ActionImpl(null, actionId, jsonAction.getString("actionDefinitionName"));
    }
    // Post Action Queue parameter
    if (jsonAction.has("actionedUponNode")) {
        NodeRef actionedUponNode = new NodeRef(jsonAction.getString("actionedUponNode"));
        result.setNodeRef(actionedUponNode);
    }
    if (jsonAction.has("description")) {
        result.setDescription(jsonAction.getString("description"));
    }
    if (jsonAction.has("title")) {
        result.setTitle(jsonAction.getString("title"));
    }
    if (jsonAction.has("parameterValues")) {
        JSONObject jsonParameterValues = jsonAction.getJSONObject("parameterValues");
        result.setParameterValues(parseJsonParameterValues(jsonParameterValues, result.getActionDefinitionName(), true));
    }
    if (jsonAction.has("executeAsync")) {
        result.setExecuteAsynchronously(jsonAction.getBoolean("executeAsync"));
    }
    if (jsonAction.has("runAsUser")) {
        result.setRunAsUser(jsonAction.getString("runAsUser"));
    }
    if (jsonAction.has("actions")) {
        JSONArray jsonActions = jsonAction.getJSONArray("actions");
        for (int i = 0; i < jsonActions.length(); i++) {
            JSONObject innerJsonAction = jsonActions.getJSONObject(i);
            Action innerAction = parseJsonAction(innerJsonAction);
            // we assume that only composite-action contains actions json array, so should be no cast exception
            ((CompositeActionImpl) result).addAction(innerAction);
        }
    }
    if (jsonAction.has("conditions")) {
        JSONArray jsonConditions = jsonAction.getJSONArray("conditions");
        for (int i = 0; i < jsonConditions.length(); i++) {
            JSONObject jsonCondition = jsonConditions.getJSONObject(i);
            // parse action conditions
            ActionCondition actionCondition = parseJsonActionCondition(jsonCondition);
            result.getActionConditions().add(actionCondition);
        }
    }
    if (jsonAction.has("compensatingAction")) {
        Action compensatingAction = parseJsonAction(jsonAction.getJSONObject("compensatingAction"));
        result.setCompensatingAction(compensatingAction);
    }
    return result;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Action(org.alfresco.service.cmr.action.Action) JSONObject(org.json.JSONObject) ActionImpl(org.alfresco.repo.action.ActionImpl) CompositeActionImpl(org.alfresco.repo.action.CompositeActionImpl) JSONArray(org.json.JSONArray) CompositeActionImpl(org.alfresco.repo.action.CompositeActionImpl) ActionCondition(org.alfresco.service.cmr.action.ActionCondition)

Aggregations

ActionImpl (org.alfresco.repo.action.ActionImpl)7 JSONObject (org.json.JSONObject)7 JSONArray (org.json.JSONArray)6 ReplicationDefinition (org.alfresco.service.cmr.replication.ReplicationDefinition)4 Response (org.springframework.extensions.webscripts.TestWebScriptServer.Response)4 UserTransaction (javax.transaction.UserTransaction)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 GetRequest (org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)3 CompositeActionImpl (org.alfresco.repo.action.CompositeActionImpl)2 Action (org.alfresco.service.cmr.action.Action)2 ActionCondition (org.alfresco.service.cmr.action.ActionCondition)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONException (org.json.JSONException)1 JSONTokener (org.json.JSONTokener)1 PutRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1