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

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.repo.model.AccessControlListDAO;
import org.sagebionetworks.repo.model.ObjectType;
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 (expected=IllegalArgumentException.class)
	public void validChangeMessageTest() throws IOException {
		Message message = MessageUtils.buildMessage(ChangeType.UPDATE, id+"", ObjectType.PRINCIPAL, "etag", System.currentTimeMillis());
		ChangeMessage changeMessage = MessageUtils.extractMessageBody(message);
		builder.build(changeMessage);
		Mockito.verify(mockAccessControlListDao).get(id);
	}
}

@@ -52,16 +52,51 @@
 utils;%0A%0A
+import static org.junit.Assert.*;%0A%0A
 import j
@@ -258,16 +258,138 @@
 eUtils;%0A
+import org.sagebionetworks.audit.utils.ObjectRecordBuilderUtils;%0Aimport org.sagebionetworks.repo.model.AccessControlList;%0A
 import o
@@ -490,16 +490,74 @@
 ctType;%0A
+import org.sagebionetworks.repo.model.audit.ObjectRecord;%0A
 import o
@@ -1771,50 +1771,8 @@
 Test
- (expected=IllegalArgumentException.class)
 %0A%09pu
@@ -1819,32 +1819,175 @@
 s IOException %7B%0A
+%09%09AccessControlList acl = new AccessControlList();%0A%09%09acl.setEtag(%22etag%22);%0A%09%09Mockito.when(mockAccessControlListDao.get(id)).thenReturn(acl);%0A%09%09%0A
 %09%09Message messag
@@ -2049,33 +2049,43 @@
  ObjectType.
-PRINCIPAL
+ACCESS_CONTROL_LIST
 , %22etag%22, Sy
@@ -2179,32 +2179,169 @@
 ody(message);%0A%09%09
+ObjectRecord expected = ObjectRecordBuilderUtils.buildObjectRecord(acl, changeMessage.getTimestamp().getTime());%0A%09%09ObjectRecord actual = 
 builder.build(ch
@@ -2406,13 +2406,47 @@
 et(id);%0A
+%09%09assertEquals(expected, actual);%0A
 %09%7D%0A%7D%0A

update test