package org.sagebionetworks.object.snapshot.worker.utils;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.sagebionetworks.asynchronous.workers.sqs.MessageUtils;
import org.sagebionetworks.audit.utils.ObjectRecordBuilderUtils;
import org.sagebionetworks.repo.model.AccessControlList;
import org.sagebionetworks.repo.model.AccessControlListDAO;
import org.sagebionetworks.repo.model.ObjectType;
import org.sagebionetworks.repo.model.audit.AclRecord;
import org.sagebionetworks.repo.model.audit.ObjectRecord;
import org.sagebionetworks.repo.model.message.ChangeMessage;
import org.sagebionetworks.repo.model.message.ChangeType;

import com.amazonaws.services.sqs.model.Message;

public class AclObjectRecordBuilderTest {
	
	private AccessControlListDAO mockAccessControlListDao;
	private AclObjectRecordBuilder builder;
	private long id = 123L;

	@Before
	public void setup() {
		mockAccessControlListDao = Mockito.mock(AccessControlListDAO.class);
		builder = new AclObjectRecordBuilder(mockAccessControlListDao);
	}

	@Test (expected=IllegalArgumentException.class)
	public void deleteAclTest() throws IOException {
		Message message = MessageUtils.buildMessage(ChangeType.DELETE, id+"", ObjectType.ACCESS_CONTROL_LIST, "etag", System.currentTimeMillis());
		ChangeMessage changeMessage = MessageUtils.extractMessageBody(message);
		builder.build(changeMessage);
	}
	
	@Test (expected=IllegalArgumentException.class)
	public void invalidChangeMessageTest() throws IOException {
		Message message = MessageUtils.buildMessage(ChangeType.UPDATE, id+"", ObjectType.PRINCIPAL, "etag", System.currentTimeMillis());
		ChangeMessage changeMessage = MessageUtils.extractMessageBody(message);
		builder.build(changeMessage);
	}
	
	@Test
	public void validChangeMessageTest() throws IOException {
		AccessControlList acl = new AccessControlList();
		acl.setEtag("etag");
		Mockito.when(mockAccessControlListDao.get(id)).thenReturn(acl);
		Mockito.when(mockAccessControlListDao.getOwnerType(id)).thenReturn(ObjectType.ENTITY);
		
		Message message = MessageUtils.buildMessage(ChangeType.UPDATE, id+"", ObjectType.ACCESS_CONTROL_LIST, "etag", System.currentTimeMillis());
		ChangeMessage changeMessage = MessageUtils.extractMessageBody(message);
		AclRecord record = AclObjectRecordBuilder.buildAclRecord(acl, ObjectType.ENTITY);
		ObjectRecord expected = ObjectRecordBuilderUtils.buildObjectRecord(record, changeMessage.getTimestamp().getTime());
		ObjectRecord actual = builder.build(changeMessage);
		Mockito.verify(mockAccessControlListDao).get(id);
		Mockito.verify(mockAccessControlListDao).getOwnerType(id);
		assertEquals(expected, actual);
	}
}

@@ -114,16 +114,65 @@
 ception;
+%0Aimport java.util.Date;%0Aimport java.util.HashSet;
 %0A%0Aimport
@@ -539,16 +539,70 @@
 ctType;%0A
+import org.sagebionetworks.repo.model.ResourceAccess;%0A
 import o
@@ -2837,11 +2837,971 @@
 ual);%0A%09%7D
+%0A%0A%09@Test%0A%09public void buildAclRecordTest() %7B%0A%09%09AccessControlList acl = new AccessControlList();%0A%09%09acl.setCreatedBy(%22createdBy%22);%0A%09%09acl.setCreationDate(new Date(0));%0A%09%09acl.setEtag(%22etag%22);%0A%09%09acl.setId(%22id%22);%0A%09%09acl.setModifiedBy(%22modifiedBy%22);%0A%09%09acl.setModifiedOn(new Date());%0A%09%09acl.setResourceAccess(new HashSet%3CResourceAccess%3E());%0A%09%09acl.setUri(%22uri%22);%0A%09%09AclRecord record = AclObjectRecordBuilder.buildAclRecord(acl, ObjectType.EVALUATION);%0A%09%09assertEquals(ObjectType.EVALUATION, record.getOwnerType());%0A%09%09assertEquals(acl.getCreatedBy(), record.getCreatedBy());%0A%09%09assertEquals(acl.getCreationDate(), record.getCreationDate());%0A%09%09assertEquals(acl.getEtag(), record.getEtag());%0A%09%09assertEquals(acl.getModifiedBy(), record.getModifiedBy());%0A%09%09assertEquals(acl.getModifiedOn(), record.getModifiedOn());%0A%09%09assertEquals(acl.getId(), record.getId());%0A%09%09assertEquals(acl.getResourceAccess(), record.getResourceAccess());%0A%09%09assertEquals(acl.getUri(), record.getUri());%0A%09%7D
 %0A%7D%0A

add test