99import ca .uhn .fhir .rest .client .api .IGenericClient ;
1010import ca .uhn .fhir .rest .client .api .ServerValidationModeEnum ;
1111import ca .uhn .fhir .rest .client .interceptor .LoggingInterceptor ;
12- import co .elastic .clients .elasticsearch .ElasticsearchClient ;
13- import co .elastic .clients .elasticsearch ._types .ElasticsearchException ;
14- import co .elastic .clients .elasticsearch .cluster .PutClusterSettingsResponse ;
15- import co .elastic .clients .elasticsearch .indices .PutIndexTemplateResponse ;
16- import co .elastic .clients .json .JsonData ;
17- import co .elastic .clients .json .jackson .JacksonJsonpMapper ;
18- import co .elastic .clients .transport .rest_client .RestClientTransport ;
1912import jakarta .annotation .PreDestroy ;
2013
2114import java .io .IOException ;
2215import java .util .Date ;
2316import java .util .GregorianCalendar ;
2417
25- import org .apache .http .HttpHost ;
26- import org .apache .http .auth .AuthScope ;
27- import org .apache .http .auth .UsernamePasswordCredentials ;
28- import org .apache .http .client .CredentialsProvider ;
29- import org .apache .http .impl .client .BasicCredentialsProvider ;
30- import org .elasticsearch .client .RestClient ;
31- import org .elasticsearch .client .RestClientBuilder ;
3218import org .hl7 .fhir .instance .model .api .IIdType ;
3319import org .hl7 .fhir .r4 .model .Bundle ;
3420import org .hl7 .fhir .r4 .model .DateTimeType ;
4228import org .junit .jupiter .api .extension .ExtendWith ;
4329import org .springframework .beans .factory .annotation .Autowired ;
4430import org .springframework .boot .test .context .SpringBootTest ;
45- import org .springframework .boot .test .util .TestPropertyValues ;
4631import org .springframework .boot .test .web .server .LocalServerPort ;
47- import org .springframework .context .ApplicationContextInitializer ;
48- import org .springframework .context .ConfigurableApplicationContext ;
49- import org .springframework .core .env .Environment ;
5032import org .springframework .test .context .ActiveProfiles ;
51- import org .springframework .test .context .ContextConfiguration ;
5233import org .springframework .test .context .DynamicPropertyRegistry ;
5334import org .springframework .test .context .DynamicPropertySource ;
5435import org .springframework .test .context .junit .jupiter .SpringExtension ;
55- import org .springframework .util .StringUtils ;
5636import org .testcontainers .elasticsearch .ElasticsearchContainer ;
5737import org .testcontainers .junit .jupiter .Container ;
5838import org .testcontainers .junit .jupiter .Testcontainers ;
5939
6040@ ExtendWith (SpringExtension .class )
6141@ Testcontainers
6242@ ActiveProfiles ("test,elastic" )
63- @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT , classes = {Application .class }, properties = {
64- "spring.datasource.url=jdbc:h2:mem:dbr4" ,
65-
43+ @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT , classes = {Application .class }, properties = {"spring.datasource.url=jdbc:h2:mem:dbr4" ,
6644 // Override the default exclude configuration for the Elasticsearch client.
67- "spring.autoconfigure.exclude=" ,
68-
69- "hapi.fhir.fhir_version=r4" ,
70-
71- // Because the port is set randomly, we will set the rest_url using the Initializer.
72- // "elasticsearch.rest_url='http://localhost:9200'",
45+ "spring.autoconfigure.exclude=" , "hapi.fhir.fhir_version=r4"
7346})
74- @ ContextConfiguration (initializers = ElasticsearchLastNR4IT .Initializer .class )
7547class ElasticsearchLastNR4IT {
7648 private IGenericClient ourClient ;
77- private FhirContext ourCtx ;
7849
7950 @ Container
8051 public static ElasticsearchContainer embeddedElastic = TestElasticsearchContainerHelper .getEmbeddedElasticSearch ();
@@ -92,13 +63,9 @@ public void stop() {
9263
9364 @ DynamicPropertySource
9465 static void configureProperties (DynamicPropertyRegistry registry ) {
95- registry .add ("spring.datasource.password " , embeddedElastic ::getHttpHostAddress );
66+ registry .add ("spring.elasticsearch.uris " , embeddedElastic ::getHttpHostAddress );
9667 }
9768
98-
99- @ Autowired
100- ConfigurableApplicationContext configurableApplicationContext ;
101-
10269 @ Test
10370 void testLastN () throws IOException , InterruptedException {
10471 Patient pt = new Patient ();
@@ -130,76 +97,11 @@ void testLastN() throws IOException, InterruptedException {
13097 @ BeforeEach
13198 void beforeEach () {
13299
133- String elasticHost = embeddedElastic .getHost ();
134- int elasticPort = embeddedElastic .getMappedPort (9200 );
135- TestPropertyValues .of ("spring.elasticsearch.uris=" + elasticHost + ":" + elasticPort ).applyTo (configurableApplicationContext .getEnvironment ());
136- ourCtx = FhirContext .forR4 ();
100+ FhirContext ourCtx = FhirContext .forR4 ();
137101 ourCtx .getRestfulClientFactory ().setServerValidationMode (ServerValidationModeEnum .NEVER );
138102 ourCtx .getRestfulClientFactory ().setSocketTimeout (1200 * 1000 );
139103 String ourServerBase = "http://localhost:" + port + "/fhir/" ;
140104 ourClient = ourCtx .newRestfulGenericClient (ourServerBase );
141105 ourClient .registerInterceptor (new LoggingInterceptor (true ));
142-
143- //buildElasticRestClient(configurableApplicationContext.getEnvironment());
144- }
145-
146- static class Initializer implements ApplicationContextInitializer <ConfigurableApplicationContext > {
147-
148- @ Override
149- public void initialize (ConfigurableApplicationContext configurableApplicationContext ) {
150- if (!embeddedElastic .isRunning ()) {
151- embeddedElastic .start ();
152- }
153- String elasticHost = embeddedElastic .getHost ();
154- int elasticPort = embeddedElastic .getMappedPort (9200 );
155- // Since the port is dynamically generated, replace the URL with one that has the correct port
156- TestPropertyValues .of ("spring.elasticsearch.uris=" + elasticHost + ":" + elasticPort ).applyTo (configurableApplicationContext .getEnvironment ());
157-
158- //ElasticsearchClient client = buildElasticRestClient(configurableApplicationContext.getEnvironment());
159-
160- /*try {
161- PutClusterSettingsResponse clusterResponse = client.cluster().putSettings(s -> s.persistent("indices.max_ngram_diff", JsonData.of(17)));
162- if (clusterResponse.acknowledged()) {
163- return;
164- }
165- } catch (ElasticsearchException | IOException e) {
166- // Fall through to index template approach when cluster setting is rejected
167- }
168-
169- PutIndexTemplateResponse templateResponse;
170- try {
171- templateResponse = client.indices().putIndexTemplate(b -> b
172- .name("hapi-max-ngram-diff")
173- .indexPatterns("*")
174- .priority(500L)
175- .template(t -> t.settings(s -> s.maxNgramDiff(17))));
176- } catch (IOException e) {
177- throw new RuntimeException(e);
178- }
179-
180- if (!templateResponse.acknowledged()) {
181- throw new IllegalStateException("Unable to set index.max_ngram_diff via cluster settings or index template");
182- }*/
183- }
184106 }
185- /*
186- static ElasticsearchClient buildElasticRestClient(Environment environment) throws IllegalStateException {
187- String uri = environment.getProperty("spring.elasticsearch.uris");
188- if (!StringUtils.hasText(uri)) {
189- throw new IllegalStateException("spring.elasticsearch.uris must be set for tests");
190- }
191-
192- HttpHost host = HttpHost.create(uri);
193- RestClientBuilder builder = RestClient.builder(host);
194-
195- String username = environment.getProperty("spring.elasticsearch.username");
196- String password = environment.getProperty("spring.elasticsearch.password");
197- if (StringUtils.hasText(username)) {
198- CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
199- credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
200- builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
201- }
202-
203- return new ElasticsearchClient(new RestClientTransport(builder.build(), new JacksonJsonpMapper()));
204- }*/
205107}
0 commit comments