Search in sources :

Example 1 with ReplicationDefinition

use of org.alfresco.service.cmr.replication.ReplicationDefinition in project alfresco-remote-api by Alfresco.

the class RunningActionRestApiTest method testRunningReplicationsActionsPost.

public void testRunningReplicationsActionsPost() throws Exception {
    Response response;
    // Not allowed if you're not an admin
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
    response = sendRequest(new PostRequest(URL_RUNNING_REPLICATION_ACTIONS, "{}", JSON), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
    response = sendRequest(new PostRequest(URL_RUNNING_REPLICATION_ACTIONS, "{}", JSON), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    // If no noderef supplied, will get an error
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    response = sendRequest(new PostRequest(URL_RUNNING_REPLICATION_ACTIONS, "{}", JSON), Status.STATUS_BAD_REQUEST);
    assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus());
    // Add a running action
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    ReplicationDefinition rd = replicationService.createReplicationDefinition("Test1", "Testing");
    replicationService.saveReplicationDefinition(rd);
    String id = rd.getId();
    txn.commit();
    // Ask for it to be started
    // (It should start but fail due to missing definition parts)
    JSONObject json = new JSONObject();
    json.put("name", "Test1");
    response = sendRequest(new PostRequest(URL_RUNNING_REPLICATION_ACTIONS, json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    // Check we got back some details on it
    String jsonStr = response.getContentAsString();
    JSONObject jsonRD = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(jsonRD);
    assertEquals(id, jsonRD.get("actionId"));
    assertEquals(ReplicationDefinitionImpl.EXECUTOR_NAME, jsonRD.get("actionType"));
    assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
    assertEquals(false, jsonRD.getBoolean("cancelRequested"));
    // specify enough options for a valid transfer)
    for (int i = 0; i < 50; i++) {
        txn = transactionService.getUserTransaction();
        txn.begin();
        rd = replicationService.loadReplicationDefinition("Test1");
        txn.commit();
        if (rd.getExecutionStatus() == ActionStatus.New) {
            // Still pending, or maybe running
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        } else if (rd.getExecutionStatus() == ActionStatus.Failed) {
            // We're done
            break;
        } else {
            fail("Unexpected status in repo of " + rd.getExecutionStatus());
        }
    }
    assertEquals(ActionStatus.Failed, rd.getExecutionStatus());
    // Ensure you can't start with an invalid name
    json = new JSONObject();
    json.put("name", "MadeUpName");
    response = sendRequest(new PostRequest(URL_RUNNING_REPLICATION_ACTIONS, json.toString(), JSON), Status.STATUS_NOT_FOUND);
    assertEquals(Status.STATUS_NOT_FOUND, response.getStatus());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) UserTransaction(javax.transaction.UserTransaction) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition)

Example 2 with ReplicationDefinition

use of org.alfresco.service.cmr.replication.ReplicationDefinition in project alfresco-remote-api by Alfresco.

the class RunningActionRestApiTest method testRunningActionsPost.

public void testRunningActionsPost() throws Exception {
    Response response;
    // Not allowed if you're not an admin
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
    response = sendRequest(new PostRequest(URL_RUNNING_ACTIONS, "{}", JSON), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
    response = sendRequest(new PostRequest(URL_RUNNING_ACTIONS, "{}", JSON), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    // If no noderef supplied, will get an error
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    response = sendRequest(new PostRequest(URL_RUNNING_ACTIONS, "{}", JSON), Status.STATUS_BAD_REQUEST);
    assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus());
    // Add a running action
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    ReplicationDefinition rd = replicationService.createReplicationDefinition("Test1", "Testing");
    replicationService.saveReplicationDefinition(rd);
    String id = rd.getId();
    txn.commit();
    // Ask for it to be started
    // (It should start but fail due to missing definition parts)
    JSONObject json = new JSONObject();
    json.put("nodeRef", rd.getNodeRef().toString());
    response = sendRequest(new PostRequest(URL_RUNNING_ACTIONS, json.toString(), JSON), Status.STATUS_OK);
    assertEquals(Status.STATUS_OK, response.getStatus());
    // Check we got back some details on it
    String jsonStr = response.getContentAsString();
    JSONObject jsonRD = new JSONObject(jsonStr).getJSONObject("data");
    assertNotNull(jsonRD);
    assertEquals(id, jsonRD.get("actionId"));
    assertEquals(ReplicationDefinitionImpl.EXECUTOR_NAME, jsonRD.get("actionType"));
    assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
    assertEquals(false, jsonRD.getBoolean("cancelRequested"));
    // specify enough options for a valid transfer)
    for (int i = 0; i < 50; i++) {
        txn = transactionService.getUserTransaction();
        txn.begin();
        rd = replicationService.loadReplicationDefinition("Test1");
        txn.commit();
        if (rd.getExecutionStatus() == ActionStatus.New) {
            // Still pending, or maybe running
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        } else if (rd.getExecutionStatus() == ActionStatus.Failed) {
            // We're done
            break;
        } else {
            fail("Unexpected status in repo of " + rd.getExecutionStatus());
        }
    }
    assertEquals(ActionStatus.Failed, rd.getExecutionStatus());
    // Ensure you can't start with an invalid nodeRef
    json = new JSONObject();
    json.put("nodeRef", "XX" + rd.getNodeRef().toString() + "ZZ");
    response = sendRequest(new PostRequest(URL_RUNNING_ACTIONS, json.toString(), JSON), Status.STATUS_NOT_FOUND);
    assertEquals(Status.STATUS_NOT_FOUND, response.getStatus());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) UserTransaction(javax.transaction.UserTransaction) PostRequest(org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest) JSONObject(org.json.JSONObject) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition)

Example 3 with ReplicationDefinition

use of org.alfresco.service.cmr.replication.ReplicationDefinition 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 4 with ReplicationDefinition

use of org.alfresco.service.cmr.replication.ReplicationDefinition 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 5 with ReplicationDefinition

use of org.alfresco.service.cmr.replication.ReplicationDefinition in project alfresco-remote-api by Alfresco.

the class RunningActionRestApiTest method testRunningActionCancel.

public void testRunningActionCancel() throws Exception {
    Response response;
    // Not allowed if you're not an admin
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
    response = sendRequest(new DeleteRequest(URL_RUNNING_ACTION + "MadeUp"), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
    response = sendRequest(new DeleteRequest(URL_RUNNING_ACTION + "MadeUp"), Status.STATUS_UNAUTHORIZED);
    assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
    // If not found, you get a 404
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    response = sendRequest(new DeleteRequest(URL_RUNNING_ACTION + "MadeUp"), Status.STATUS_NOT_FOUND);
    assertEquals(Status.STATUS_NOT_FOUND, response.getStatus());
    // Create one
    ReplicationDefinition rd = replicationService.createReplicationDefinition("Test1", "Testing");
    replicationService.saveReplicationDefinition(rd);
    actionTrackingService.recordActionExecuting(rd);
    String id = rd.getId();
    String instance = Integer.toString(((ActionImpl) rd).getExecutionInstance());
    String key = "replicationActionExecutor=" + id + "=" + instance;
    assertEquals(false, actionTrackingService.isCancellationRequested(rd));
    // Request it to cancel
    response = sendRequest(new DeleteRequest(URL_RUNNING_ACTION + key), Status.STATUS_NO_CONTENT);
    assertEquals(Status.STATUS_NO_CONTENT, response.getStatus());
    // Check it was cancelled
    assertEquals(true, actionTrackingService.isCancellationRequested(rd));
    // Request again - no change
    response = sendRequest(new DeleteRequest(URL_RUNNING_ACTION + key), Status.STATUS_NO_CONTENT);
    assertEquals(Status.STATUS_NO_CONTENT, response.getStatus());
    assertEquals(true, actionTrackingService.isCancellationRequested(rd));
    // You can ask a pending action to cancel
    // At this time, it will remain in the queue though
    rd = replicationService.createReplicationDefinition("Test2", "Testing");
    replicationService.saveReplicationDefinition(rd);
    actionTrackingService.recordActionPending(rd);
    String id2 = rd.getId();
    String instance2 = Integer.toString(((ActionImpl) rd).getExecutionInstance());
    String key2 = "replicationActionExecutor=" + id2 + "=" + instance2;
    // Should be in the cache, but not not running and not cancelled
    assertEquals(false, actionTrackingService.isCancellationRequested(rd));
    assertEquals(null, rd.getExecutionStartDate());
    assertNotNull(executingActionsCache.get(key2));
    assertFalse(executingActionsCache.get(key2).isCancelRequested());
    // Ask for it to be cancelled via the webscript
    response = sendRequest(new DeleteRequest(URL_RUNNING_ACTION + key2), Status.STATUS_NO_CONTENT);
    assertEquals(Status.STATUS_NO_CONTENT, response.getStatus());
    // Should still be in the cache, not running but cancelled
    assertNotNull(executingActionsCache.get(key2));
    assertTrue(executingActionsCache.get(key2).isCancelRequested());
    assertEquals(true, actionTrackingService.isCancellationRequested(rd));
    assertEquals(null, rd.getExecutionStartDate());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) DeleteRequest(org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)

Aggregations

ReplicationDefinition (org.alfresco.service.cmr.replication.ReplicationDefinition)22 JSONObject (org.json.JSONObject)12 UserTransaction (javax.transaction.UserTransaction)11 Response (org.springframework.extensions.webscripts.TestWebScriptServer.Response)11 JSONArray (org.json.JSONArray)7 GetRequest (org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest)6 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)5 ActionImpl (org.alfresco.repo.action.ActionImpl)4 IOException (java.io.IOException)3 ExecutionSummary (org.alfresco.service.cmr.action.ExecutionSummary)3 JSONException (org.json.JSONException)3 JSONTokener (org.json.JSONTokener)3 PostRequest (org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest)3 TestPersonManager (org.alfresco.repo.security.person.TestPersonManager)2 ExecutionDetails (org.alfresco.service.cmr.action.ExecutionDetails)2 MutableAuthenticationService (org.alfresco.service.cmr.security.MutableAuthenticationService)2 PersonService (org.alfresco.service.cmr.security.PersonService)2 ApplicationContext (org.springframework.context.ApplicationContext)2 DeleteRequest (org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest)2 ArrayList (java.util.ArrayList)1