package org.sagebionetworks.repo.model.message;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sagebionetworks.repo.model.ObjectType;
import org.sagebionetworks.repo.model.UserGroup;
import org.sagebionetworks.repo.model.UserGroupDAO;
import org.sagebionetworks.repo.model.dbo.dao.DBOChangeDAO;
import org.sagebionetworks.repo.web.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:jdomodels-test-context.xml" })
public class BroadcastMessageDaoImplTest {
	
	@Autowired
	BroadcastMessageDao broadcastMessageDao;
	@Autowired
	DBOChangeDAO changeDao;
	@Autowired
	private UserGroupDAO userGroupDAO;
	
	UserGroup user;
	ChangeMessage changeMessage;
	
	@Before
	public void before(){
		
		user = new UserGroup();
		user.setIsIndividual(true);
		user.setId(userGroupDAO.create(user).toString());
		
		changeDao.deleteAllChanges();
		
		changeMessage = new ChangeMessage();
		changeMessage.setChangeType(ChangeType.CREATE);
		changeMessage.setObjectEtag("etag");
		changeMessage.setObjectId("123");
		changeMessage.setObjectType(ObjectType.THREAD);
		changeMessage = changeDao.replaceChange(changeMessage);
	}
	
	@After
	public void after(){
		changeDao.deleteAllChanges();
		if(user != null){
			userGroupDAO.delete(user.getId());
		}
	}
	
	@Test
	public void testWasBroadcastDoesNotExist(){
		long doesNotExist = -1;
		// call under test
		boolean wasBroadcast = broadcastMessageDao.wasBroadcast(doesNotExist);
		assertFalse(wasBroadcast);
	}
	
	@Test
	public void setBroadcastHappy(){
		// should not be broadcast yet.
		assertFalse(broadcastMessageDao.wasBroadcast(changeMessage.getChangeNumber()));
		// call under test.
		broadcastMessageDao.setBroadcast(changeMessage.getChangeNumber());
		// should be broadcast
		assertTrue(broadcastMessageDao.wasBroadcast(changeMessage.getChangeNumber()));
	}
	
	@Test (expected=NotFoundException.class)
	public void setBroadcastChangeDoesNotExist(){
		Long changeDoesNotExist = -1L;
		// call under test.
		broadcastMessageDao.setBroadcast(changeDoesNotExist);
	}

}

@@ -2279,220 +2279,8 @@
 %0A%09%7D%0D
-%0A%09%0D%0A%09@Test (expected=NotFoundException.class)%0D%0A%09public void setBroadcastChangeDoesNotExist()%7B%0D%0A%09%09Long changeDoesNotExist = -1L;%0D%0A%09%09// call under test.%0D%0A%09%09broadcastMessageDao.setBroadcast(changeDoesNotExist);%0D%0A%09%7D%0D
 %0A%0D%0A%7D

remove test