package com.cloudmine.api.integration;

import com.cloudmine.api.*;
import com.cloudmine.api.rest.UserCMWebService;
import com.cloudmine.api.rest.callbacks.CMObjectResponseCallback;
import com.cloudmine.api.rest.callbacks.CreationResponseCallback;
import com.cloudmine.api.rest.options.CMRequestOptions;
import com.cloudmine.api.rest.options.CMSharedDataOptions;
import com.cloudmine.api.rest.response.CMObjectResponse;
import com.cloudmine.api.rest.response.CreationResponse;
import com.cloudmine.test.ServiceTestBase;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static com.cloudmine.test.AsyncTestResultsCoordinator.waitThenAssertTestResults;
import static com.cloudmine.test.TestServiceCallback.testCallback;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

/**
 * <br>
 * Copyright CloudMine LLC. All rights reserved<br>
 * See LICENSE file included with SDK for details.
 */
public class CMAccessListIntegrationTest extends ServiceTestBase {

    @Test
    public void testStoreAccessList() {
        final JavaCMUser anotherUser = randomUser();
        anotherUser.createUser(hasSuccess);
        waitThenAssertTestResults();

        final JavaCMUser user = user();
        user.createUser(testCallback());
        waitThenAssertTestResults();
        user.login(hasSuccess);
        waitThenAssertTestResults();

        List<String> userObjectIds = Arrays.asList("freddy", "teddy", "george", "puddin");
        CMAccessList list = new CMAccessList(user, CMAccessPermission.READ, CMAccessPermission.UPDATE);
        list.grantAccessTo(userObjectIds);
        list.grantAccessTo(anotherUser);
        list.grantPermissions(CMAccessPermission.READ);
        list.save(testCallback(new CreationResponseCallback() {
            @Override
            public void onCompletion(CreationResponse response) {
                assertTrue(response.wasSuccess());
//                assertEquals(list.getObjectId(), response.getObjectId());
            }
        }));
        waitThenAssertTestResults();


        final SimpleCMObject anObject = new SimpleCMObject();
        anObject.add("aSecret", true);
        anObject.grantAccess(list);
        System.out.println("AO: " + anObject.transportableRepresentation());
        anObject.saveWithUser(user, hasSuccessAndHasModified(anObject));
        waitThenAssertTestResults();

        anotherUser.login(hasSuccess);
        waitThenAssertTestResults();
        CMSessionToken token = anotherUser.getSessionToken();

        UserCMWebService userWebService = service.getUserWebService(token);
        userWebService.asyncLoadObject(anObject.getObjectId(), testCallback(new CMObjectResponseCallback() {
            @Override
            public void onCompletion(CMObjectResponse response) {
                assertTrue(response.hasSuccess());
                assertEquals(1, response.getObjects().size());
                CMObject loadedObject = response.getCMObject(anObject.getObjectId());
                assertEquals(anObject, loadedObject);
            }
        }), CMRequestOptions.CMRequestOptions(CMSharedDataOptions.SHARED_OPTIONS));
        waitThenAssertTestResults();

        CMRequestOptions requestOptions = new CMRequestOptions(CMSharedDataOptions.getShared());
        userWebService.asyncLoadObjects(testCallback(new CMObjectResponseCallback() {
            public void onCompletion(CMObjectResponse response) {

                assertTrue(response.hasSuccess());
                assertEquals(1, response.getObjects().size());
                CMObject loadedObject = response.getCMObject(anObject.getObjectId());
                assertEquals(anObject, loadedObject);
            }

        }), requestOptions);
        waitThenAssertTestResults();
    }

    @Test
    public void testGetAccessList() {
        JavaCMUser user = user();
        final CMAccessList list = new CMAccessList(user, CMAccessPermission.CREATE);
        list.grantAccessTo("whatever");
        list.save(hasSuccess);
        waitThenAssertTestResults();

        user.loadAccessLists(testCallback(new CMObjectResponseCallback() {
            public void onCompletion(CMObjectResponse response) {
                assertTrue(response.wasSuccess());
                CMObject loadedList = response.getCMObject(list.getObjectId());
                assertEquals(list, loadedList);
            }
        }));
        waitThenAssertTestResults();
    }
}

@@ -1122,624 +1122,140 @@
 r = 
-randomUser();%0A        anotherUser.createUser(hasSuccess);%0A        waitThenAssertTestResults();%0A%0A        final JavaCMUser user = user();%0A        user.createUser(testCallback());%0A        waitThenAssertTestResults();%0A        user.login(hasSuccess);%0A        waitThenAssertTestResults();%0A%0A        List%3CString%3E userObjectIds = Arrays.asList(%22freddy%22, %22teddy%22, %22george%22, %22puddin%22);%0A        CMAccessList list = new CMAccessList(user, CMAccessPermission.READ, CMAccessPermission.UPDATE);%0A        list.grantAccessTo(userObjectIds);%0A        list.grantAccessTo(anotherUser);%0A        list.grantPermissions(CMAccessPermission.READ
+createOtherUser();%0A%0A        final JavaCMUser user = createMainUser();%0A%0A        CMAccessList list = getCmAccessList(anotherUser, user
 );%0A 
@@ -1642,289 +1642,33 @@
 t = 
-new SimpleCMObject();%0A        anObject.add(%22aSecret%22, true);%0A        anObject.grantAccess(list);%0A        System.out.println(%22AO: %22 + anObject.transportableRepresentation());%0A        anObject.saveWithUser(user, hasSuccessAndHasModified(anObject));%0A        waitThenAssertTestResults(
+insertAnObject(user, list
 );%0A%0A
@@ -3057,24 +3057,1307 @@
 s();%0A    %7D%0A%0A
+    protected SimpleCMObject insertAnObject(JavaCMUser user, CMAccessList list) %7B%0A        final SimpleCMObject anObject = new SimpleCMObject();%0A        anObject.add(%22aSecret%22, true);%0A        anObject.grantAccess(list);%0A        anObject.saveWithUser(user, hasSuccessAndHasModified(anObject));%0A        waitThenAssertTestResults();%0A        return anObject;%0A    %7D%0A%0A    protected CMAccessList getCmAccessList(JavaCMUser anotherUser, JavaCMUser user) %7B%0A        List%3CString%3E userObjectIds = Arrays.asList(%22freddy%22, %22teddy%22, %22george%22, %22puddin%22);%0A        CMAccessList list = new CMAccessList(user, CMAccessPermission.READ, CMAccessPermission.UPDATE);%0A        list.grantAccessTo(userObjectIds);%0A        list.grantAccessTo(anotherUser);%0A        list.grantPermissions(CMAccessPermission.READ);%0A        return list;%0A    %7D%0A%0A    protected JavaCMUser createMainUser() %7B%0A        final JavaCMUser user = user();%0A        user.createUser(testCallback());%0A        waitThenAssertTestResults();%0A        user.login(hasSuccess);%0A        waitThenAssertTestResults();%0A        return user;%0A    %7D%0A%0A    protected JavaCMUser createOtherUser() %7B%0A        final JavaCMUser anotherUser = randomUser();%0A        anotherUser.createUser(hasSuccess);%0A        waitThenAssertTestResults();%0A        return anotherUser;%0A    %7D%0A%0A
     @Test%0A  

Refactor so easier to test Android specific methods