package org.sagebionetworks.repo.manager.discussion;

import static org.junit.Assert.*;

import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sagebionetworks.repo.manager.NodeManager;
import org.sagebionetworks.repo.manager.UserManager;
import org.sagebionetworks.repo.model.DomainType;
import org.sagebionetworks.repo.model.EntityType;
import org.sagebionetworks.repo.model.Node;
import org.sagebionetworks.repo.model.UserInfo;
import org.sagebionetworks.repo.model.AuthorizationConstants.BOOTSTRAP_PRINCIPAL;
import org.sagebionetworks.repo.model.auth.NewUser;
import org.sagebionetworks.repo.model.dbo.persistence.DBOCredential;
import org.sagebionetworks.repo.model.dbo.persistence.DBOTermsOfUseAgreement;
import org.sagebionetworks.repo.model.discussion.CreateDiscussionThread;
import org.sagebionetworks.repo.model.discussion.Forum;
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:test-context.xml" })
public class DiscussionThreadManagerImplAutowiredTest {

	@Autowired
	public NodeManager nodeManager;
	@Autowired
	public UserManager userManager;
	@Autowired
	public ForumManager forumManager;
	@Autowired
	public DiscussionThreadManager threadManager;

	private UserInfo adminUserInfo;
	private UserInfo userInfo;
	private String projectId;
	private Forum forum;
	private CreateDiscussionThread createThread;

	@Before
	public void before() {
		DBOTermsOfUseAgreement tou = new DBOTermsOfUseAgreement();
		tou.setDomain(DomainType.SYNAPSE);
		tou.setAgreesToTermsOfUse(Boolean.TRUE);

		adminUserInfo = userManager.getUserInfo(BOOTSTRAP_PRINCIPAL.THE_ADMIN_USER.getPrincipalId());
		UserInfo.validateUserInfo(adminUserInfo);

		DBOCredential cred = new DBOCredential();
		cred.setSecretKey("");
		NewUser nu = new NewUser();
		nu.setEmail(UUID.randomUUID().toString() + "@test.com");
		nu.setUserName(UUID.randomUUID().toString());
		userInfo = userManager.createUser(adminUserInfo, nu, cred, tou);
		userInfo.getGroups().add(BOOTSTRAP_PRINCIPAL.CERTIFIED_USERS.getPrincipalId());

		Node newNode = new Node();
		newNode.setName("project");
		newNode.setNodeType(EntityType.project);
		projectId = nodeManager.createNewNode(newNode, userInfo);
		forum = forumManager.getForumMetadata(userInfo, projectId);

		createThread = new CreateDiscussionThread();
		createThread.setForumId(forum.getId());
		createThread.setTitle("Title");
		createThread.setMessageMarkdown("<header>This is a message.</header>");
	}

	@After
	public void cleanup() {
		nodeManager.delete(adminUserInfo, projectId);
		userManager.deletePrincipal(adminUserInfo, userInfo.getId());
	}

	@Test
	public void test() throws Exception {
		assertNotNull(threadManager.createThread(userInfo, createThread));
		assertNotNull(threadManager.createThread(userInfo, createThread).getMessageUrl());
	}
}

@@ -82,16 +82,102 @@
 ert.*;%0A%0A
+import java.io.BufferedReader;%0Aimport java.io.InputStreamReader;%0Aimport java.net.URL;%0A
 import j
@@ -191,16 +191,16 @@
 l.UUID;%0A
-
 %0Aimport 
@@ -949,16 +949,89 @@
 Thread;%0A
+import org.sagebionetworks.repo.model.discussion.DiscussionThreadBundle;%0A
 import o
@@ -1793,16 +1793,89 @@
 eThread;
+%0A%09private String messageMarkdown = %22%3Cheader%3EThis is a message.%3C/header%3E%22;
 %0A%0A%09@Befo
@@ -2926,45 +2926,23 @@
 own(
-%22%3Cheader%3EThis is a message.%3C/header%3E%22
+messageMarkdown
 );%0A%09
@@ -3141,30 +3141,48 @@
 ion %7B%0A%09%09
-assertNotNull(
+DiscussionThreadBundle bundle = 
 threadMa
@@ -3227,93 +3227,420 @@
 ead)
-);%0A%09%09assertNotNull(threadManager.createThread(userInfo, createThread).getMessageUrl()
+;%0A%09%09assertNotNull(bundle);%0A%09%09String urlString = bundle.getMessageUrl();%0A%09%09assertNotNull(urlString);%0A%09%09URL url = new URL(urlString);%0A%09%09BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));%0A%09%09String inputLine;%0A%09%09boolean matched = false;%0A%09%09while ((inputLine = in.readLine()) != null) %7B%0A%09%09%09assertEquals(inputLine, messageMarkdown);%0A%09%09%09matched = true;%0A%09%09%7D%0A%09%09assertTrue(matched);%0A%09%09in.close(
 );%0A%09

complete the integration test