package ca.uhn.fhir.jpa.empi.provider;

import ca.uhn.fhir.jpa.entity.EmpiLink;
import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;
import org.hl7.fhir.r4.model.Person;
import org.hl7.fhir.r4.model.Practitioner;
import org.hl7.fhir.r4.model.StringType;
import org.junit.Before;
import org.junit.Test;

import javax.annotation.Nonnull;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.matchesPattern;
import static org.junit.Assert.fail;

public class EmpiProviderClearLinkR4Test extends BaseLinkR4Test {


	protected Practitioner myPractitioner;
	protected StringType myPractitionerId;

	@Before
	public void before() {
		super.before();
		myPractitioner = createPractitionerAndUpdateLinks(new Practitioner());
		myPractitionerId = new StringType(myPractitioner.getIdElement().getValue());
	}

	@Test
	public void testClearAllLinks() {
		assertLinkCount(2);
		myEmpiProviderR4.clearEmpiLinks(null);
		assertLinkCount(0);
		assertNoLinksExist();
	}

	private void assertNoLinksExist() {
		assertNoPatientLinksExist();
		assertNoPractitionerLinksExist();
	}

	private void assertNoPatientLinksExist() {
		assertThat(getPatientLinks(), hasSize(0));
	}

	private void assertNoPractitionerLinksExist() {
		assertThat(getPractitionerLinks(), hasSize(0));
	}

	@Test
	public void testClearPatientLinks() {
		assertLinkCount(2);
		myEmpiProviderR4.clearEmpiLinks(new StringType("patient"));
		assertLinkCount(1);

		assertNoLinksExist();
	}

	@Test
	public void testClearPractitionerLinks() {
		myEmpiProviderR4.updateLink(myPersonId, myPatientId, MATCH_RESULT, myRequestDetails);
		try {
			myEmpiProviderR4.updateLink(myPersonId, myPatientId, NO_MATCH_RESULT, myRequestDetails);
			fail();
		} catch (ResourceVersionConflictException e) {
			assertThat(e.getMessage(), matchesPattern("Requested resource Person/\\d+/_history/1 is not the latest version.  Latest version is Person/\\d+/_history/2"));
		}
	}

	@Test
	public void testClearInvalidTargetType() {
		myEmpiProviderR4.updateLink(myPersonId, myPatientId, MATCH_RESULT, myRequestDetails);
		Person person = myEmpiProviderR4.updateLink(myVersionlessPersonId, myPatientId, NO_MATCH_RESULT, myRequestDetails);
		assertThat(person.getLink(), hasSize(0));
	}


	@Nonnull
	protected EmpiLink getOnlyPractitionerLink() {
		return myEmpiLinkDaoSvc.findEmpiLinkByTarget(myPractitioner).get();
	}

	@Nonnull
	protected List<EmpiLink> getPractitionerLinks() {
		return myEmpiLinkDaoSvc.findEmpiLinksByTarget(myPractitioner);
	}
}

@@ -77,84 +77,8 @@
 nk;%0A
-import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;%0A
 impo
@@ -110,16 +110,16 @@
 Person;%0A
+
 import o
@@ -211,22 +211,38 @@
 g.junit.
+jupiter.api.
 Before
+Each
 ;%0Aimport
@@ -252,16 +252,28 @@
 g.junit.
+jupiter.api.
 Test;%0A%0Ai
@@ -429,97 +429,8 @@
 ize;
-%0Aimport static org.hamcrest.Matchers.matchesPattern;%0Aimport static org.junit.Assert.fail;
 %0A%0Apu
@@ -584,16 +584,20 @@
 %09@Before
+Each
 %0A%09public
@@ -896,30 +896,8 @@
 l);%0A
-%09%09assertLinkCount(0);%0A
 %09%09as
@@ -1366,33 +1366,17 @@
 sert
-LinkCount(1);%0A%0A%09%09assertNo
+NoPatient
 Link
@@ -1446,418 +1446,125 @@
 %7B%0A%09%09
-myEmpiProviderR4.updateLink(myPersonId, myPatientId, MATCH_RESULT, myRequestDetails);%0A%09%09try %7B%0A%09%09%09myEmpiProviderR4.updateLink(myPersonId, myPatientId, NO_MATCH_RESULT, myRequestDetails);%0A%09%09%09fail();%0A%09%09%7D catch (ResourceVersionConflictException e) %7B%0A%09%09%09assertThat(e.getMessage(), matchesPattern(%22Requested resource Person/%5C%5Cd+/_history/1 is not the latest version.  Latest version is Person/%5C%5Cd+/_history/2%22));%0A%09%09%7D
+assertLinkCount(2);%0A%09%09myEmpiProviderR4.clearEmpiLinks(new StringType(%22patient%22));%0A%09%09assertNoPractitionerLinksExist();
 %0A%09%7D%0A

Fix tests to cover other scenarios