package org.sagebionetworks.repo.web.service.metadata;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.sagebionetworks.repo.manager.table.TableEntityManager;
import org.sagebionetworks.repo.model.UserInfo;
import org.sagebionetworks.repo.model.entity.IdAndVersion;
import org.sagebionetworks.repo.model.table.TableEntity;
import org.springframework.test.util.ReflectionTestUtils;

import com.google.common.collect.Lists;

public class TableEntityMetadataProviderTest  {
	
	@Mock
	TableEntityManager tableEntityManager;
	
	TableEntityMetadataProvider provider;
	
	String entityId;
	IdAndVersion idAndVersion;
	TableEntity table;
	List<String> columnIds;
	
	UserInfo userInfo;	
	
	@Before
	public void before(){
		MockitoAnnotations.initMocks(this);
		
		provider = new TableEntityMetadataProvider();
		ReflectionTestUtils.setField(provider, "tableEntityManager", tableEntityManager);
		
		columnIds = Lists.newArrayList("123");
		
		entityId = "syn123";
		idAndVersion = IdAndVersion.parse(entityId);
		table = new TableEntity();
		table.setId(entityId);
		table.setColumnIds(columnIds);
		
		userInfo = new UserInfo(false, 55L);
	}
	
	@Test
	public void testDeleteEntity(){
		// call under test
		provider.entityDeleted(entityId);
		verify(tableEntityManager).setTableAsDeleted(entityId);
	}
	
	@Test
	public void testCreate(){
		// call under test
		provider.entityCreated(userInfo, table);
		verify(tableEntityManager).setTableSchema(userInfo, columnIds, entityId);
	}
	
	@Test
	public void testUpdateNoNewVersion(){
		boolean wasNewVersionCreated = false;
		// call under test
		provider.entityUpdated(userInfo, table, wasNewVersionCreated);
		verify(tableEntityManager).setTableSchema(userInfo, columnIds, entityId);
	}
	
	@Test (expected=IllegalArgumentException.class)
	public void testUpdateWithNewVersion(){
		boolean wasNewVersionCreated = true;
		// call under test
		provider.entityUpdated(userInfo, table, wasNewVersionCreated);
		verify(tableEntityManager).setTableSchema(userInfo, columnIds, entityId);
	}
	
	/**
	 * When returning a TableEntity, ideally, we would include the schema
	 * that matches table's version.  However, all tables created before
	 * we added support for table versions do not have a schema for each version.
	 * Nor do newly created tables.  As a compromise, we always return the 'current'
	 * schema regardless of the table's version. 
	 */
	@Test
	public void testAddTypeSpecificMetadata(){
		TableEntity testEntity = new TableEntity();
		testEntity.setId(entityId);
		testEntity.setVersionNumber(33L);
		IdAndVersion expectedId = IdAndVersion.parse("syn123");
		when(tableEntityManager.getTableSchema(expectedId)).thenReturn(columnIds);
		provider.addTypeSpecificMetadata(testEntity, null, null); //the other parameters are not used at all
		verify(tableEntityManager).getTableSchema(expectedId);
		assertEquals(columnIds, testEntity.getColumnIds());
	}

}

@@ -2329,382 +2329,16 @@
 %0A%09%0D%0A
-%09/**%0D%0A%09 * When returning a TableEntity, ideally, we would include the schema%0D%0A%09 * that matches table's version.  However, all tables created before%0D%0A%09 * we added support for table versions do not have a schema for each version.%0D%0A%09 * Nor do newly created tables.  As a compromise, we always return the 'current'%0D%0A%09 * schema regardless of the table's version. %0D%0A%09 */%0D%0A
 %09@Test%0D%0A
 %09pub
@@ -2325,32 +2325,32 @@
 %0A%09%7D%0D%0A%09%0D%0A%09@Test%0D%0A
+
 %09public void tes
@@ -2493,16 +2493,16 @@
 (33L);%0D%0A
-
 %09%09IdAndV
@@ -2547,16 +2547,19 @@
 (%22syn123
+.33
 %22);%0D%0A%09%09w

test fix