package com.bullhornsdk.data;

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

import com.google.common.collect.Sets;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.junit.Test;

import com.bullhornsdk.data.model.entity.core.standard.JobOrder;
import com.bullhornsdk.data.model.entity.core.standard.Note;
import com.bullhornsdk.data.model.entity.core.standard.Person;
import com.bullhornsdk.data.model.entity.core.standard.Placement;
import com.bullhornsdk.data.model.entity.embedded.OneToMany;
import com.bullhornsdk.data.model.response.crud.CrudResponse;

public class TestStandardBullhornApiRestNoteAdd extends BaseTest {
	
	public TestStandardBullhornApiRestNoteAdd() {
		super();
	}

	@Test
	public void testAddNoteAndAssociateToEntity() {

		Note note = getNote();

		CrudResponse response = bullhornData.addNoteAndAssociateWithEntity(note);

		runAssertions(response);
	}

	private void runAssertions(CrudResponse response) {
		assertNotNull("response is null", response);
		assertFalse("Validation failed", response.hasValidationErrors());
		assertFalse("Insert failed", response.isError());
		assertTrue("Note note added properly", response.getChangedEntityId() != null);

		Note addedNote = bullhornData.findEntity(Note.class, response.getChangedEntityId(), Sets.newHashSet("jobOrder(id)", "personReference(id)", "id"));
		assertTrue(addedNote.getJobOrder().getId().equals(testEntities.getJobOrderId()));
		assertTrue(addedNote.getPersonReference().getId().equals(testEntities.getCandidateId()));
	}

	private Note getNote() {
		Note note = new Note();
		note.setComments("Adding from unit test on " + DateTime.now().toString(ISODateTimeFormat.basicOrdinalDateTimeNoMillis()));
		note.setAction("Inteview Scheduled");
		note.setIsDeleted(false);
		// the note has to have a commentingPerson
		Person commentingPerson = new Person(testEntities.getCorporateUserId());
		note.setCommentingPerson(commentingPerson);
		// set job to associate with job
		JobOrder job = new JobOrder(testEntities.getJobOrderId());
		note.setJobOrder(job);
		// set placements to associate with placements
		Placement placement = new Placement(testEntities.getPlacementId());
		OneToMany<Placement> placements = new OneToMany<Placement>(placement);
		note.setPlacements(placements);

		// set personReference to associate with Person
		Person personReference = new Person(testEntities.getCandidateId());
		note.setPersonReference(personReference);

		return note;
	}

}

@@ -158,16 +158,74 @@
 tTrue;%0A%0A
+import com.bullhornsdk.data.model.entity.core.standard.*;%0A
 import c
@@ -255,16 +255,16 @@
 t.Sets;%0A
-
 import o
@@ -361,263 +361,8 @@
 t;%0A%0A
-import com.bullhornsdk.data.model.entity.core.standard.JobOrder;%0Aimport com.bullhornsdk.data.model.entity.core.standard.Note;%0Aimport com.bullhornsdk.data.model.entity.core.standard.Person;%0Aimport com.bullhornsdk.data.model.entity.core.standard.Placement;%0A
 impo
@@ -548,17 +548,16 @@
 eTest %7B%0A
-%09
 %0A%09public
@@ -2190,16 +2190,240 @@
 ements);
+%0A%09%09// set jobShifts to associate with jobShifts%0A        JobShift jobShift = new JobShift(testEntities.getJobShiftId());%0A        OneToMany%3CJobShift%3E jobShifts = new OneToMany%3C%3E(jobShift);%0A        note.setJobShifts(jobShifts);
 %0A%0A%09%09// s

Add jobShift association to getNote method