package org.sagebionetworks.repo.model.dbo.dao;

import static org.junit.Assert.assertEquals;

import java.util.Date;

import org.junit.Test;
import org.sagebionetworks.repo.model.AccessApproval;
import org.sagebionetworks.repo.model.TermsOfUseAccessApproval;
import org.sagebionetworks.repo.model.dbo.persistence.DBOAccessApproval;
import org.sagebionetworks.schema.ObjectSchema;
import org.sagebionetworks.schema.adapter.JSONEntity;
import org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl;

public class AccessApprovalUtilsTest {

	private static AccessApproval createDTO() {
		TermsOfUseAccessApproval dto = new TermsOfUseAccessApproval();
		dto.setId(101L);
		dto.setEtag("0");
		dto.setCreatedBy("555");
		dto.setCreatedOn(new Date());
		dto.setModifiedBy("666");
		dto.setModifiedOn(new Date());	
		dto.setAccessorId("777");
		dto.setConcreteType("org.sagebionetworks.repo.model.TermsOfUseAccessApproval");
		dto.setRequirementId(888L);
		return dto;
	}
	
	@Test
	public void testRoundtrip() throws Exception {
		AccessApproval dto = createDTO();
			
		DBOAccessApproval dbo = new DBOAccessApproval();
		String jsonString = (String) AccessApproval.class.getField(JSONEntity.EFFECTIVE_SCHEMA).get(null);
		AccessApprovalUtils.copyDtoToDbo(dto, dbo);
		AccessApproval dto2 =  AccessApprovalUtils.copyDboToDto(dbo);
		assertEquals(dto, dto2);
	}

	@Test
	public void testRoundtripWithNulls() throws Exception {
		AccessApproval dto = createDTO();
		dto.setId(null);
		DBOAccessApproval dbo = new DBOAccessApproval();
		String jsonString = (String) AccessApproval.class.getField(JSONEntity.EFFECTIVE_SCHEMA).get(null);
		ObjectSchema schema = new ObjectSchema(new JSONObjectAdapterImpl(jsonString));
		AccessApprovalUtils.copyDtoToDbo(dto, dbo);
		AccessApproval dto2 =  AccessApprovalUtils.copyDboToDto(dbo);
		assertEquals(dto, dto2);
	}
}

@@ -77,23 +77,74 @@
 ert.
-assertEqual
+*;%0Aimport static org.mockito.Mockito.*;%0A%0Aimport java.util.Array
 s;%0A
-%0A
 impo
@@ -166,30 +166,125 @@
 te;%0A
-%0Aimport org.junit.Test
+import java.util.List;%0A%0Aimport org.junit.Test;%0Aimport org.mockito.Mockito;%0Aimport org.sagebionetworks.ids.IdGenerator
 ;%0Aim
@@ -1997,11 +1997,983 @@
 to2);%0A%09%7D
+%0A%0A%09@Test%0A%09public void testCopyDtosToDbosForCreation() %7B%0A%09%09IdGenerator mockIdGenerator = Mockito.mock(IdGenerator.class);%0A%09%09AccessApproval dto = createDTO();%0A%09%09when(mockIdGenerator.generateNewId()).thenReturn(1L);%0A%09%09List%3CDBOAccessApproval%3E dbos = AccessApprovalUtils.copyDtosToDbos(Arrays.asList(dto), true, mockIdGenerator);%0A%09%09assertNotNull(dbos);%0A%09%09assertEquals(1, dbos.size());%0A%09%09DBOAccessApproval dbo = dbos.get(0);%0A%09%09AccessApproval newDto = AccessApprovalUtils.copyDboToDto(dbo);%0A%09%09assertFalse(newDto.equals(dto));%0A%09%09dto.setId(newDto.getId());%0A%09%09dto.setEtag(newDto.getEtag());%0A%09%09assertEquals(newDto, dto);%0A%09%7D%0A%0A%09@Test%0A%09public void testCopyDtosToDbosNotForCreation() %7B%0A%09%09AccessApproval dto = createDTO();%0A%09%09List%3CDBOAccessApproval%3E dbos = AccessApprovalUtils.copyDtosToDbos(Arrays.asList(dto), false, null);%0A%09%09List%3CAccessApproval%3E dtos = AccessApprovalUtils.copyDbosToDtos(dbos);%0A%09%09assertNotNull(dtos);%0A%09%09assertEquals(1, dtos.size());%0A%09%09assertEquals(dtos.get(0), dto);%0A%09%7D
 %0A%7D%0A

add test