11package ca .uhn .fhir .jpa .starter ;
22
3- import static java .time .temporal .ChronoUnit .SECONDS ;
43import static org .junit .jupiter .api .Assertions .assertEquals ;
54
65import ca .uhn .fhir .context .FhirContext ;
76import ca .uhn .fhir .jpa .search .lastn .ElasticsearchSvcImpl ;
87import ca .uhn .fhir .jpa .starter .elastic .ElasticsearchBootSvcImpl ;
8+ import ca .uhn .fhir .jpa .test .config .TestElasticsearchContainerHelper ;
99import ca .uhn .fhir .rest .client .api .IGenericClient ;
1010import ca .uhn .fhir .rest .client .api .ServerValidationModeEnum ;
1111import ca .uhn .fhir .rest .client .interceptor .LoggingInterceptor ;
1919import jakarta .annotation .PreDestroy ;
2020
2121import java .io .IOException ;
22- import java .time .Duration ;
2322import java .util .Date ;
2423import java .util .GregorianCalendar ;
2524
5049import org .springframework .core .env .Environment ;
5150import org .springframework .test .context .ActiveProfiles ;
5251import org .springframework .test .context .ContextConfiguration ;
52+ import org .springframework .test .context .DynamicPropertyRegistry ;
53+ import org .springframework .test .context .DynamicPropertySource ;
5354import org .springframework .test .context .junit .jupiter .SpringExtension ;
5455import org .springframework .util .StringUtils ;
5556import org .testcontainers .elasticsearch .ElasticsearchContainer ;
5859
5960@ ExtendWith (SpringExtension .class )
6061@ Testcontainers
61- @ ActiveProfiles ("test" )
62+ @ ActiveProfiles ("test,elastic " )
6263@ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT , classes = {Application .class }, properties = {
6364 "spring.datasource.url=jdbc:h2:mem:dbr4" ,
6465
6566 // Override the default exclude configuration for the Elasticsearch client.
6667 "spring.autoconfigure.exclude=" ,
6768
6869 "hapi.fhir.fhir_version=r4" ,
69- "hapi.fhir.lastn_enabled=true" ,
70- "hapi.fhir.store_resource_in_lucene_index_enabled=true" ,
71- "hapi.fhir.advanced_lucene_indexing=true" ,
72- "hapi.fhir.search_index_full_text_enabled=true" ,
7370
7471 // Because the port is set randomly, we will set the rest_url using the Initializer.
7572 // "elasticsearch.rest_url='http://localhost:9200'",
76-
77- "spring.elasticsearch.uris=http://localhost:9200" ,
78- "spring.elasticsearch.username=elastic" ,
79- "spring.elasticsearch.password=changeme" ,
80- "spring.main.allow-bean-definition-overriding=true" ,
81- "spring.jpa.properties.hibernate.search.enabled=true" ,
82- "spring.jpa.properties.hibernate.search.backend.type=elasticsearch" ,
83- "spring.jpa.properties.hibernate.search.backend.hosts=localhost:9200" ,
84- "spring.jpa.properties.hibernate.search.backend.protocol=http" ,
85- "spring.jpa.properties.hibernate.search.backend.analysis.configurer=ca.uhn.fhir.jpa.search.HapiHSearchAnalysisConfigurers$HapiElasticsearchAnalysisConfigurer" })
73+ })
8674@ ContextConfiguration (initializers = ElasticsearchLastNR4IT .Initializer .class )
8775class ElasticsearchLastNR4IT {
8876 private IGenericClient ourClient ;
8977 private FhirContext ourCtx ;
9078
9179 @ Container
92- public static ElasticsearchContainer embeddedElastic = getEmbeddedElasticSearch ();
80+ public static ElasticsearchContainer embeddedElastic = TestElasticsearchContainerHelper . getEmbeddedElasticSearch ();
9381
9482 @ Autowired
9583 private ElasticsearchBootSvcImpl myElasticsearchSvc ;
@@ -102,6 +90,15 @@ public void stop() {
10290 @ LocalServerPort
10391 private int port ;
10492
93+ @ DynamicPropertySource
94+ static void configureProperties (DynamicPropertyRegistry registry ) {
95+ registry .add ("spring.datasource.password" , embeddedElastic ::getHttpHostAddress );
96+ }
97+
98+
99+ @ Autowired
100+ ConfigurableApplicationContext configurableApplicationContext ;
101+
105102 @ Test
106103 void testLastN () throws IOException , InterruptedException {
107104 Patient pt = new Patient ();
@@ -133,12 +130,17 @@ void testLastN() throws IOException, InterruptedException {
133130 @ BeforeEach
134131 void beforeEach () {
135132
133+ String elasticHost = embeddedElastic .getHost ();
134+ int elasticPort = embeddedElastic .getMappedPort (9200 );
135+ TestPropertyValues .of ("spring.elasticsearch.uris=" + elasticHost + ":" + elasticPort ).applyTo (configurableApplicationContext .getEnvironment ());
136136 ourCtx = FhirContext .forR4 ();
137137 ourCtx .getRestfulClientFactory ().setServerValidationMode (ServerValidationModeEnum .NEVER );
138138 ourCtx .getRestfulClientFactory ().setSocketTimeout (1200 * 1000 );
139139 String ourServerBase = "http://localhost:" + port + "/fhir/" ;
140140 ourClient = ourCtx .newRestfulGenericClient (ourServerBase );
141141 ourClient .registerInterceptor (new LoggingInterceptor (true ));
142+
143+ //buildElasticRestClient(configurableApplicationContext.getEnvironment());
142144 }
143145
144146 static class Initializer implements ApplicationContextInitializer <ConfigurableApplicationContext > {
@@ -152,11 +154,10 @@ public void initialize(ConfigurableApplicationContext configurableApplicationCon
152154 int elasticPort = embeddedElastic .getMappedPort (9200 );
153155 // Since the port is dynamically generated, replace the URL with one that has the correct port
154156 TestPropertyValues .of ("spring.elasticsearch.uris=" + elasticHost + ":" + elasticPort ).applyTo (configurableApplicationContext .getEnvironment ());
155- TestPropertyValues .of ("spring.jpa.properties.hibernate.search.backend.hosts=" + elasticHost + ":" + elasticPort ).applyTo (configurableApplicationContext .getEnvironment ());
156157
157- ElasticsearchClient client = buildElasticRestClient (configurableApplicationContext .getEnvironment ());
158+ // ElasticsearchClient client = buildElasticRestClient(configurableApplicationContext.getEnvironment());
158159
159- try {
160+ /* try {
160161 PutClusterSettingsResponse clusterResponse = client.cluster().putSettings(s -> s.persistent("indices.max_ngram_diff", JsonData.of(17)));
161162 if (clusterResponse.acknowledged()) {
162163 return;
@@ -178,26 +179,10 @@ public void initialize(ConfigurableApplicationContext configurableApplicationCon
178179
179180 if (!templateResponse.acknowledged()) {
180181 throw new IllegalStateException("Unable to set index.max_ngram_diff via cluster settings or index template");
181- }
182+ }*/
182183 }
183184 }
184-
185-
186- public static final String ELASTICSEARCH_VERSION = "8.19.7" ;
187- public static final String ELASTICSEARCH_IMAGE = "docker.elastic.co/elasticsearch/elasticsearch:" + ELASTICSEARCH_VERSION ;
188-
189- public static ElasticsearchContainer getEmbeddedElasticSearch () {
190-
191- return new ElasticsearchContainer (ELASTICSEARCH_IMAGE )
192- // the default is 4GB which is too much for our little tests
193- .withEnv ("ES_JAVA_OPTS" , "-Xms512m -Xmx512m" )
194- // turn off security warnings
195- .withEnv ("xpack.security.enabled" , "false" )
196- // turn off machine learning (we don't need it in tests anyways)
197- .withEnv ("xpack.ml.enabled" , "false" )
198- .withStartupTimeout (Duration .of (300 , SECONDS ));
199- }
200-
185+ /*
201186 static ElasticsearchClient buildElasticRestClient(Environment environment) throws IllegalStateException {
202187 String uri = environment.getProperty("spring.elasticsearch.uris");
203188 if (!StringUtils.hasText(uri)) {
@@ -216,5 +201,5 @@ static ElasticsearchClient buildElasticRestClient(Environment environment) throw
216201 }
217202
218203 return new ElasticsearchClient(new RestClientTransport(builder.build(), new JacksonJsonpMapper()));
219- }
204+ }*/
220205}
0 commit comments