Skip to content

Commit efe8d45

Browse files
committed
works from here
1 parent bf21bdd commit efe8d45

File tree

1 file changed

+25
-40
lines changed

1 file changed

+25
-40
lines changed

src/test/java/ca/uhn/fhir/jpa/starter/ElasticsearchLastNR4IT.java

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package ca.uhn.fhir.jpa.starter;
22

3-
import static java.time.temporal.ChronoUnit.SECONDS;
43
import static org.junit.jupiter.api.Assertions.assertEquals;
54

65
import ca.uhn.fhir.context.FhirContext;
76
import ca.uhn.fhir.jpa.search.lastn.ElasticsearchSvcImpl;
87
import ca.uhn.fhir.jpa.starter.elastic.ElasticsearchBootSvcImpl;
8+
import ca.uhn.fhir.jpa.test.config.TestElasticsearchContainerHelper;
99
import ca.uhn.fhir.rest.client.api.IGenericClient;
1010
import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum;
1111
import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor;
@@ -19,7 +19,6 @@
1919
import jakarta.annotation.PreDestroy;
2020

2121
import java.io.IOException;
22-
import java.time.Duration;
2322
import java.util.Date;
2423
import java.util.GregorianCalendar;
2524

@@ -50,6 +49,8 @@
5049
import org.springframework.core.env.Environment;
5150
import org.springframework.test.context.ActiveProfiles;
5251
import org.springframework.test.context.ContextConfiguration;
52+
import org.springframework.test.context.DynamicPropertyRegistry;
53+
import org.springframework.test.context.DynamicPropertySource;
5354
import org.springframework.test.context.junit.jupiter.SpringExtension;
5455
import org.springframework.util.StringUtils;
5556
import org.testcontainers.elasticsearch.ElasticsearchContainer;
@@ -58,38 +59,25 @@
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)
8775
class 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

Comments
 (0)