package org.opencds.cqf.ruler.test;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.opencds.cqf.ruler.Application;
import org.opencds.cqf.ruler.behavior.IdCreator;
import org.opencds.cqf.ruler.behavior.ResourceCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum;


@Import(Application.class)
@TestPropertySource(properties = { 
	"scheduling_disabled=true",
	"spring.main.allow-bean-definition-overriding=true",
	"spring.batch.job.enabled=false",
	"hapi.fhir.allow_external_references=true",
	"hapi.fhir.enforce_referential_integrity_on_write=false",
	"allow_placeholder_references=true",
	"spring.datasource.url=jdbc:h2:mem:db",
	"spring.main.lazy-initialization=true" })
@TestInstance(Lifecycle.PER_CLASS)
public class RestIntegrationTest implements ResourceLoader, ResourceCreator, IdCreator {

	@Autowired
	TestDbService myDbService;

	@Autowired
	private FhirContext myCtx;

	@Autowired
	DaoRegistry myDaoRegistry;

	@LocalServerPort
	private int myPort;

	private IGenericClient myClient;

	@Override
	public FhirContext getFhirContext() {
		return myCtx;
	}

	@Override
	public DaoRegistry getDaoRegistry() {
		return myDaoRegistry;
	}

	protected IGenericClient getClient() {
		return myClient;
	}

	protected int getPort() {
		return myPort;
	}

	@BeforeEach
	void baseBeforeEach() {
		myCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
		myCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);
		String ourServerBase = "http://localhost:" + myPort + "/fhir/";
		myClient = myCtx.newRestfulGenericClient(ourServerBase);
	}

	@AfterAll
	void baseAfterAll() {
		myDbService.resetDatabase();
	}
}

@@ -1075,21 +1075,37 @@
 lse%22,%0A%09%22
-allow
+hapi.fhir.auto_create
 _placeho
@@ -1114,30 +1114,74 @@
 er_reference
-s=true
+_targets=true%22,%0A%09%22hapi.fhir.client_id_strategy=ANY
 %22,%0A%09%22spring.

Fix config settings