2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.enrichment;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.JsonParser;
33 import java.io.FileNotFoundException;
34 import java.io.FileOutputStream;
35 import java.io.PrintStream;
36 import java.util.ArrayList;
37 import java.util.Collection;
39 import org.json.JSONObject;
40 import org.junit.jupiter.api.AfterEach;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.oransc.enrichment.clients.AsyncRestClient;
45 import org.oransc.enrichment.clients.AsyncRestClientFactory;
46 import org.oransc.enrichment.configuration.ApplicationConfig;
47 import org.oransc.enrichment.configuration.ImmutableWebClientConfig;
48 import org.oransc.enrichment.configuration.WebClientConfig;
49 import org.oransc.enrichment.controller.ConsumerSimulatorController;
50 import org.oransc.enrichment.controller.ProducerSimulatorController;
51 import org.oransc.enrichment.controllers.consumer.ConsumerConsts;
52 import org.oransc.enrichment.controllers.consumer.ConsumerEiJobInfo;
53 import org.oransc.enrichment.controllers.consumer.ConsumerEiJobStatus;
54 import org.oransc.enrichment.controllers.consumer.ConsumerEiTypeInfo;
55 import org.oransc.enrichment.controllers.producer.ProducerConsts;
56 import org.oransc.enrichment.controllers.producer.ProducerJobInfo;
57 import org.oransc.enrichment.controllers.producer.ProducerRegistrationInfo;
58 import org.oransc.enrichment.controllers.producer.ProducerRegistrationInfo.ProducerEiTypeRegistrationInfo;
59 import org.oransc.enrichment.controllers.producer.ProducerStatusInfo;
60 import org.oransc.enrichment.exceptions.ServiceException;
61 import org.oransc.enrichment.repository.EiJob;
62 import org.oransc.enrichment.repository.EiJobs;
63 import org.oransc.enrichment.repository.EiProducers;
64 import org.oransc.enrichment.repository.EiType;
65 import org.oransc.enrichment.repository.EiTypes;
66 import org.oransc.enrichment.tasks.ProducerSupervision;
67 import org.springframework.beans.factory.annotation.Autowired;
68 import org.springframework.boot.test.context.SpringBootTest;
69 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
70 import org.springframework.boot.test.context.TestConfiguration;
71 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
72 import org.springframework.boot.web.server.LocalServerPort;
73 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
74 import org.springframework.context.ApplicationContext;
75 import org.springframework.context.annotation.Bean;
76 import org.springframework.http.HttpStatus;
77 import org.springframework.http.MediaType;
78 import org.springframework.http.ResponseEntity;
79 import org.springframework.test.context.TestPropertySource;
80 import org.springframework.test.context.junit.jupiter.SpringExtension;
81 import org.springframework.web.reactive.function.client.WebClientResponseException;
83 import reactor.core.publisher.Mono;
84 import reactor.test.StepVerifier;
86 @ExtendWith(SpringExtension.class)
87 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
90 "server.ssl.key-store=./config/keystore.jks", //
91 "app.webclient.trust-store=./config/truststore.jks"})
92 class ApplicationTest {
93 private final String EI_TYPE_ID = "typeId";
94 private final String EI_PRODUCER_ID = "producerId";
95 private final String EI_JOB_PROPERTY = "\"property1\"";
96 private final String EI_JOB_ID = "jobId";
99 ApplicationContext context;
108 EiProducers eiProducers;
111 ApplicationConfig applicationConfig;
114 ProducerSimulatorController producerSimulator;
117 ConsumerSimulatorController consumerSimulator;
120 ProducerSupervision producerSupervision;
122 private static Gson gson = new GsonBuilder().create();
125 * Overrides the BeanFactory.
128 static class TestBeanFactory {
130 public ServletWebServerFactory servletContainer() {
131 return new TomcatServletWebServerFactory();
141 this.eiTypes.clear();
142 this.eiProducers.clear();
143 this.producerSimulator.getTestResults().reset();
144 this.consumerSimulator.getTestResults().reset();
149 assertThat(this.producerSimulator.getTestResults().errorFound).isFalse();
153 void createApiDoc() throws FileNotFoundException {
154 String url = "/v2/api-docs";
155 ResponseEntity<String> resp = restClient().getForEntity(url).block();
156 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
158 String indented = (new JSONObject(resp.getBody())).toString(4);
159 try (PrintStream out = new PrintStream(new FileOutputStream("docs/api.json"))) {
165 void testGetEiTypes() throws Exception {
166 putEiProducerWithOneType(EI_PRODUCER_ID, "test");
167 String url = ConsumerConsts.API_ROOT + "/eitypes";
168 String rsp = restClient().get(url).block();
169 assertThat(rsp).isEqualTo("[\"test\"]");
173 void testGetEiTypesEmpty() throws Exception {
174 String url = ConsumerConsts.API_ROOT + "/eitypes";
175 String rsp = restClient().get(url).block();
176 assertThat(rsp).isEqualTo("[]");
180 void testGetEiType() throws Exception {
181 putEiProducerWithOneType(EI_PRODUCER_ID, "test");
182 String url = ConsumerConsts.API_ROOT + "/eitypes/test";
183 String rsp = restClient().get(url).block();
184 ConsumerEiTypeInfo info = gson.fromJson(rsp, ConsumerEiTypeInfo.class);
185 assertThat(info).isNotNull();
189 void testGetEiTypeNotFound() throws Exception {
190 String url = ConsumerConsts.API_ROOT + "/eitypes/junk";
191 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND, "Could not find EI type: junk");
195 void testGetEiJobsIds() throws Exception {
196 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
197 putEiJob(EI_TYPE_ID, "jobId");
198 final String JOB_ID_JSON = "[\"jobId\"]";
199 String url = ConsumerConsts.API_ROOT + "/eijobs?eiTypeId=typeId";
200 String rsp = restClient().get(url).block();
201 assertThat(rsp).isEqualTo(JOB_ID_JSON);
203 url = ConsumerConsts.API_ROOT + "/eijobs?owner=owner";
204 rsp = restClient().get(url).block();
205 assertThat(rsp).isEqualTo(JOB_ID_JSON);
207 url = ConsumerConsts.API_ROOT + "/eijobs?owner=JUNK";
208 rsp = restClient().get(url).block();
209 assertThat(rsp).isEqualTo("[]");
211 url = ConsumerConsts.API_ROOT + "/eijobs";
212 rsp = restClient().get(url).block();
213 assertThat(rsp).isEqualTo(JOB_ID_JSON);
215 url = ConsumerConsts.API_ROOT + "/eijobs?eiTypeId=typeId&&owner=owner";
216 rsp = restClient().get(url).block();
217 assertThat(rsp).isEqualTo(JOB_ID_JSON);
219 url = ConsumerConsts.API_ROOT + "/eijobs?eiTypeId=JUNK";
220 rsp = restClient().get(url).block();
221 assertThat(rsp).isEqualTo("[]");
225 void testGetEiJob() throws Exception {
226 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
227 putEiJob(EI_TYPE_ID, "jobId");
228 String url = ConsumerConsts.API_ROOT + "/eijobs/jobId";
229 String rsp = restClient().get(url).block();
230 ConsumerEiJobInfo info = gson.fromJson(rsp, ConsumerEiJobInfo.class);
231 assertThat(info.owner).isEqualTo("owner");
232 assertThat(info.eiTypeId).isEqualTo(EI_TYPE_ID);
236 void testGetEiJobNotFound() throws Exception {
237 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
238 String url = ConsumerConsts.API_ROOT + "/eijobs/junk";
239 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND, "Could not find EI job: junk");
243 void testGetEiJobStatus() throws Exception {
244 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
245 putEiJob(EI_TYPE_ID, "jobId");
247 verifyJobStatus("jobId", "ENABLED");
251 void testDeleteEiJob() throws Exception {
252 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
253 putEiJob(EI_TYPE_ID, "jobId");
254 assertThat(this.eiJobs.size()).isEqualTo(1);
255 String url = ConsumerConsts.API_ROOT + "/eijobs/jobId";
256 restClient().delete(url).block();
257 assertThat(this.eiJobs.size()).isZero();
259 ProducerSimulatorController.TestResults simulatorResults = this.producerSimulator.getTestResults();
260 await().untilAsserted(() -> assertThat(simulatorResults.jobsStopped.size()).isEqualTo(1));
261 assertThat(simulatorResults.jobsStopped.get(0)).isEqualTo("jobId");
265 void testDeleteEiJobNotFound() throws Exception {
266 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
267 String url = ConsumerConsts.API_ROOT + "/eijobs/junk";
268 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND, "Could not find EI job: junk");
272 void testPutEiJob() throws Exception {
273 // Test that one producer accepting a job is enough
274 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
275 putEiProducerWithOneTypeRejecting("simulateProducerError", EI_TYPE_ID);
277 String url = ConsumerConsts.API_ROOT + "/eijobs/jobId";
278 String body = gson.toJson(eiJobInfo());
279 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
280 assertThat(this.eiJobs.size()).isEqualTo(1);
281 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.CREATED);
283 ProducerSimulatorController.TestResults simulatorResults = this.producerSimulator.getTestResults();
284 await().untilAsserted(() -> assertThat(simulatorResults.jobsStarted.size()).isEqualTo(1));
285 ProducerJobInfo request = simulatorResults.jobsStarted.get(0);
286 assertThat(request.id).isEqualTo("jobId");
288 assertThat(simulatorResults.noOfRejectedCreate).isEqualTo(1);
290 resp = restClient().putForEntity(url, body).block();
291 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
292 EiJob job = this.eiJobs.getJob("jobId");
293 assertThat(job.owner()).isEqualTo("owner");
297 void putEiProducerWithOneType_rejecting() throws JsonMappingException, JsonProcessingException, ServiceException {
298 putEiProducerWithOneTypeRejecting("simulateProducerError", EI_TYPE_ID);
299 String url = ConsumerConsts.API_ROOT + "/eijobs/jobId";
300 String body = gson.toJson(eiJobInfo());
301 testErrorCode(restClient().put(url, body), HttpStatus.CONFLICT, "Job not accepted by any producers");
303 ProducerSimulatorController.TestResults simulatorResults = this.producerSimulator.getTestResults();
304 assertThat(simulatorResults.noOfRejectedCreate).isEqualTo(1);
308 void testPutEiJob_jsonSchemavalidationError() throws Exception {
309 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
311 String url = ConsumerConsts.API_ROOT + "/eijobs/jobId";
312 // The element with name "property1" is mandatory in the schema
313 ConsumerEiJobInfo jobInfo = new ConsumerEiJobInfo("typeId", jsonObject("{ \"XXstring\" : \"value\" }"), "owner",
314 "targetUri", "jobStatusUrl");
315 String body = gson.toJson(jobInfo);
317 testErrorCode(restClient().put(url, body), HttpStatus.CONFLICT, "Json validation failure");
321 void testGetEiProducerTypes() throws Exception {
322 final String EI_TYPE_ID_2 = EI_TYPE_ID + "_2";
323 putEiProducerWithOneType("producer1", EI_TYPE_ID);
324 putEiJob(EI_TYPE_ID, "jobId");
325 putEiProducerWithOneType("producer2", EI_TYPE_ID_2);
326 putEiJob(EI_TYPE_ID_2, "jobId2");
327 String url = ProducerConsts.API_ROOT + "/eitypes";
329 ResponseEntity<String> resp = restClient().getForEntity(url).block();
330 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
331 assertThat(resp.getBody()).contains(EI_TYPE_ID);
332 assertThat(resp.getBody()).contains(EI_TYPE_ID_2);
336 void testReplacingEiProducerTypes() throws Exception {
337 final String REPLACED_TYPE_ID = "replaced";
338 putEiProducerWithOneType(EI_PRODUCER_ID, REPLACED_TYPE_ID);
339 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
341 String url = ProducerConsts.API_ROOT + "/eitypes";
343 ResponseEntity<String> resp = restClient().getForEntity(url).block();
344 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
345 assertThat(resp.getBody()).contains(EI_TYPE_ID);
346 assertThat(resp.getBody()).doesNotContain(REPLACED_TYPE_ID);
350 void testChangingEiTypeGetRejected() throws Exception {
351 putEiProducerWithOneType("producer1", "typeId1");
352 putEiProducerWithOneType("producer2", "typeId2");
353 putEiJob("typeId1", "jobId");
355 String url = ConsumerConsts.API_ROOT + "/eijobs/jobId";
356 String body = gson.toJson(eiJobInfo("typeId2", "jobId"));
357 testErrorCode(restClient().put(url, body), HttpStatus.CONFLICT,
358 "Not allowed to change type for existing EI job");
362 void testPutEiProducer() throws Exception {
363 String url = ProducerConsts.API_ROOT + "/eiproducers/eiProducerId";
364 String body = gson.toJson(producerEiRegistratioInfo(EI_TYPE_ID));
366 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
367 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.CREATED);
369 assertThat(this.eiTypes.size()).isEqualTo(1);
370 EiType type = this.eiTypes.getType(EI_TYPE_ID);
371 assertThat(type.getProducerIds()).contains("eiProducerId");
372 assertThat(this.eiProducers.size()).isEqualTo(1);
373 assertThat(this.eiProducers.get("eiProducerId").getEiTypes().iterator().next().getId()).isEqualTo(EI_TYPE_ID);
375 resp = restClient().putForEntity(url, body).block();
376 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
378 resp = restClient().getForEntity(url).block();
379 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
380 assertThat(resp.getBody()).isEqualTo(body);
384 void testPutEiProducerExistingJob() throws Exception {
385 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
386 putEiJob(EI_TYPE_ID, "jobId");
387 String url = ProducerConsts.API_ROOT + "/eiproducers/eiProducerId";
388 String body = gson.toJson(producerEiRegistratioInfo(EI_TYPE_ID));
389 restClient().putForEntity(url, body).block();
391 ProducerSimulatorController.TestResults simulatorResults = this.producerSimulator.getTestResults();
392 await().untilAsserted(() -> assertThat(simulatorResults.jobsStarted.size()).isEqualTo(2));
393 ProducerJobInfo request = simulatorResults.jobsStarted.get(0);
394 assertThat(request.id).isEqualTo("jobId");
398 void testPutProducerAndEiJob() throws Exception {
399 String url = ProducerConsts.API_ROOT + "/eiproducers/eiProducerId";
400 String body = gson.toJson(producerEiRegistratioInfo(EI_TYPE_ID));
401 restClient().putForEntity(url, body).block();
402 assertThat(this.eiTypes.size()).isEqualTo(1);
403 this.eiTypes.getType(EI_TYPE_ID);
405 url = ConsumerConsts.API_ROOT + "/eijobs/jobId";
406 body = gson.toJson(eiJobInfo());
407 restClient().putForEntity(url, body).block();
409 ProducerSimulatorController.TestResults simulatorResults = this.producerSimulator.getTestResults();
410 await().untilAsserted(() -> assertThat(simulatorResults.jobsStarted.size()).isEqualTo(1));
411 ProducerJobInfo request = simulatorResults.jobsStarted.get(0);
412 assertThat(request.id).isEqualTo("jobId");
416 void testGetEiJobsForProducer() throws JsonMappingException, JsonProcessingException, ServiceException {
417 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
418 putEiJob(EI_TYPE_ID, "jobId1");
419 putEiJob(EI_TYPE_ID, "jobId2");
422 String url = ProducerConsts.API_ROOT + "/eiproducers/eiProducerId";
423 String body = gson.toJson(producerEiRegistratioInfo(EI_TYPE_ID));
424 restClient().putForEntity(url, body).block();
426 url = ProducerConsts.API_ROOT + "/eiproducers/eiProducerId/eijobs";
427 ResponseEntity<String> resp = restClient().getForEntity(url).block();
428 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
430 ProducerJobInfo[] parsedResp = gson.fromJson(resp.getBody(), ProducerJobInfo[].class);
431 assertThat(parsedResp[0].typeId).isEqualTo(EI_TYPE_ID);
432 assertThat(parsedResp[1].typeId).isEqualTo(EI_TYPE_ID);
436 void testDeleteEiProducer() throws Exception {
437 putEiProducerWithOneType("eiProducerId", EI_TYPE_ID);
438 putEiProducerWithOneType("eiProducerId2", EI_TYPE_ID);
440 assertThat(this.eiProducers.size()).isEqualTo(2);
441 EiType type = this.eiTypes.getType(EI_TYPE_ID);
442 assertThat(type.getProducerIds()).contains("eiProducerId");
443 assertThat(type.getProducerIds()).contains("eiProducerId2");
444 putEiJob(EI_TYPE_ID, "jobId");
445 assertThat(this.eiJobs.size()).isEqualTo(1);
447 deleteEiProducer("eiProducerId");
448 assertThat(this.eiProducers.size()).isEqualTo(1);
449 assertThat(this.eiTypes.getType(EI_TYPE_ID).getProducerIds()).doesNotContain("eiProducerId");
450 verifyJobStatus("jobId", "ENABLED");
452 deleteEiProducer("eiProducerId2");
453 assertThat(this.eiProducers.size()).isZero();
454 assertThat(this.eiTypes.size()).isZero();
455 verifyJobStatus("jobId", "DISABLED");
459 void testJobStatusNotifications() throws JsonMappingException, JsonProcessingException, ServiceException {
460 putEiProducerWithOneType("eiProducerId", EI_TYPE_ID);
461 putEiJob(EI_TYPE_ID, "jobId");
463 deleteEiProducer("eiProducerId");
464 assertThat(this.eiTypes.size()).isZero(); // The type is gone
465 assertThat(this.eiJobs.size()).isEqualTo(1); // The job remains
466 ConsumerSimulatorController.TestResults consumerResults = this.consumerSimulator.getTestResults();
467 await().untilAsserted(() -> assertThat(consumerResults.status.size()).isEqualTo(1));
468 assertThat(consumerResults.status.get(0).state).isEqualTo(ConsumerEiJobStatus.EiJobStatusValues.DISABLED);
470 putEiProducerWithOneType("eiProducerId", EI_TYPE_ID);
471 await().untilAsserted(() -> assertThat(consumerResults.status.size()).isEqualTo(2));
472 assertThat(consumerResults.status.get(1).state).isEqualTo(ConsumerEiJobStatus.EiJobStatusValues.ENABLED);
476 void testGetProducerEiType() throws JsonMappingException, JsonProcessingException, ServiceException {
477 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
478 String url = ProducerConsts.API_ROOT + "/eitypes/" + EI_TYPE_ID;
479 ResponseEntity<String> resp = restClient().getForEntity(url).block();
480 assertThat(resp.getBody()).contains(EI_PRODUCER_ID);
484 void testGetProducerIdentifiers() throws JsonMappingException, JsonProcessingException, ServiceException {
485 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
486 String url = ProducerConsts.API_ROOT + "/eiproducers";
487 ResponseEntity<String> resp = restClient().getForEntity(url).block();
488 assertThat(resp.getBody()).contains(EI_PRODUCER_ID);
492 void testProducerSupervision() throws JsonMappingException, JsonProcessingException, ServiceException {
493 putEiProducerWithOneTypeRejecting("simulateProducerError", EI_TYPE_ID);
497 putEiProducerWithOneType(EI_PRODUCER_ID, EI_TYPE_ID);
498 putEiJob(EI_TYPE_ID, "jobId");
499 deleteEiProducer(EI_PRODUCER_ID);
502 assertThat(this.eiProducers.size()).isEqualTo(1);
503 assertThat(this.eiTypes.size()).isEqualTo(1);
504 assertProducerOpState("simulateProducerError", ProducerStatusInfo.OperationalState.ENABLED);
506 this.producerSupervision.createTask().blockLast();
507 this.producerSupervision.createTask().blockLast();
508 assertThat(this.eiProducers.size()).isEqualTo(1);
509 assertProducerOpState("simulateProducerError", ProducerStatusInfo.OperationalState.DISABLED);
511 // After 3 failed checks, the producer and the type shall be deregisterred
512 this.producerSupervision.createTask().blockLast();
513 assertThat(this.eiProducers.size()).isEqualTo(0);
514 assertThat(this.eiTypes.size()).isEqualTo(0);
516 // Job disabled status notification shall be received
517 ConsumerSimulatorController.TestResults consumerResults = this.consumerSimulator.getTestResults();
518 await().untilAsserted(() -> assertThat(consumerResults.status.size()).isEqualTo(1));
519 assertThat(consumerResults.status.get(0).state).isEqualTo(ConsumerEiJobStatus.EiJobStatusValues.DISABLED);
523 void testGetStatus() throws JsonMappingException, JsonProcessingException, ServiceException {
524 putEiProducerWithOneTypeRejecting("simulateProducerError", EI_TYPE_ID);
525 putEiProducerWithOneTypeRejecting("simulateProducerError2", EI_TYPE_ID);
527 String url = "/status";
528 ResponseEntity<String> resp = restClient().getForEntity(url).block();
529 assertThat(resp.getBody()).contains("hunky dory");
532 private void deleteEiProducer(String eiProducerId) {
533 String url = ProducerConsts.API_ROOT + "/eiproducers/" + eiProducerId;
534 restClient().deleteForEntity(url).block();
537 private void verifyJobStatus(String jobId, String expStatus) {
538 String url = ConsumerConsts.API_ROOT + "/eijobs/" + jobId + "/status";
539 String rsp = restClient().get(url).block();
540 assertThat(rsp).contains(expStatus);
543 private void assertProducerOpState(String producerId,
544 ProducerStatusInfo.OperationalState expectedOperationalState) {
545 String statusUrl = ProducerConsts.API_ROOT + "/eiproducers/" + producerId + "/status";
546 ResponseEntity<String> resp = restClient().getForEntity(statusUrl).block();
547 ProducerStatusInfo statusInfo = gson.fromJson(resp.getBody(), ProducerStatusInfo.class);
548 assertThat(statusInfo.opState).isEqualTo(expectedOperationalState);
551 ProducerEiTypeRegistrationInfo producerEiTypeRegistrationInfo(String typeId)
552 throws JsonMappingException, JsonProcessingException {
553 return new ProducerEiTypeRegistrationInfo(jsonSchemaObject(), typeId);
556 ProducerRegistrationInfo producerEiRegistratioInfoRejecting(String typeId)
557 throws JsonMappingException, JsonProcessingException {
558 Collection<ProducerEiTypeRegistrationInfo> types = new ArrayList<>();
559 types.add(producerEiTypeRegistrationInfo(typeId));
560 return new ProducerRegistrationInfo(types, //
561 baseUrl() + ProducerSimulatorController.JOB_ERROR_URL,
562 baseUrl() + ProducerSimulatorController.SUPERVISION_ERROR_URL);
565 ProducerRegistrationInfo producerEiRegistratioInfo(String typeId)
566 throws JsonMappingException, JsonProcessingException {
567 Collection<ProducerEiTypeRegistrationInfo> types = new ArrayList<>();
568 types.add(producerEiTypeRegistrationInfo(typeId));
569 return new ProducerRegistrationInfo(types, //
570 baseUrl() + ProducerSimulatorController.JOB_URL, baseUrl() + ProducerSimulatorController.SUPERVISION_URL);
573 private ConsumerEiJobInfo eiJobInfo() throws JsonMappingException, JsonProcessingException {
574 return eiJobInfo(EI_TYPE_ID, EI_JOB_ID);
577 ConsumerEiJobInfo eiJobInfo(String typeId, String eiJobId) throws JsonMappingException, JsonProcessingException {
578 return new ConsumerEiJobInfo(typeId, jsonObject(), "owner", "targetUri",
579 baseUrl() + ConsumerSimulatorController.getJobStatusUrl(eiJobId));
582 private Object jsonObject(String json) {
584 return JsonParser.parseString(json).getAsJsonObject();
585 } catch (Exception e) {
586 throw new NullPointerException(e.toString());
590 private Object jsonSchemaObject() {
591 // a json schema with one mandatory property named "string"
592 String schemaStr = "{" //
593 + "\"$schema\": \"http://json-schema.org/draft-04/schema#\"," //
594 + "\"type\": \"object\"," //
595 + "\"properties\": {" //
596 + EI_JOB_PROPERTY + " : {" //
597 + " \"type\": \"string\"" //
600 + "\"required\": [" //
604 return jsonObject(schemaStr);
607 private Object jsonObject() {
608 return jsonObject("{ " + EI_JOB_PROPERTY + " : \"value\" }");
611 private EiJob putEiJob(String eiTypeId, String jobId)
612 throws JsonMappingException, JsonProcessingException, ServiceException {
614 String url = ConsumerConsts.API_ROOT + "/eijobs/" + jobId;
615 String body = gson.toJson(eiJobInfo(eiTypeId, jobId));
616 restClient().putForEntity(url, body).block();
618 return this.eiJobs.getJob(jobId);
621 private EiType putEiProducerWithOneTypeRejecting(String producerId, String eiTypeId)
622 throws JsonMappingException, JsonProcessingException, ServiceException {
623 String url = ProducerConsts.API_ROOT + "/eiproducers/" + producerId;
624 String body = gson.toJson(producerEiRegistratioInfoRejecting(eiTypeId));
626 restClient().putForEntity(url, body).block();
627 return this.eiTypes.getType(eiTypeId);
630 private EiType putEiProducerWithOneType(String producerId, String eiTypeId)
631 throws JsonMappingException, JsonProcessingException, ServiceException {
632 String url = ProducerConsts.API_ROOT + "/eiproducers/" + producerId;
633 String body = gson.toJson(producerEiRegistratioInfo(eiTypeId));
635 restClient().putForEntity(url, body).block();
636 return this.eiTypes.getType(eiTypeId);
639 private String baseUrl() {
640 return "https://localhost:" + this.port;
643 private AsyncRestClient restClient(boolean useTrustValidation) {
644 WebClientConfig config = this.applicationConfig.getWebClientConfig();
645 config = ImmutableWebClientConfig.builder() //
646 .keyStoreType(config.keyStoreType()) //
647 .keyStorePassword(config.keyStorePassword()) //
648 .keyStore(config.keyStore()) //
649 .keyPassword(config.keyPassword()) //
650 .isTrustStoreUsed(useTrustValidation) //
651 .trustStore(config.trustStore()) //
652 .trustStorePassword(config.trustStorePassword()) //
655 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(config);
656 return restClientFactory.createRestClient(baseUrl());
659 private AsyncRestClient restClient() {
660 return restClient(false);
663 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
664 testErrorCode(request, expStatus, responseContains, true);
667 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
668 boolean expectApplicationProblemJsonMediaType) {
669 StepVerifier.create(request) //
670 .expectSubscription() //
672 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
676 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
677 boolean expectApplicationProblemJsonMediaType) {
678 assertTrue(throwable instanceof WebClientResponseException);
679 WebClientResponseException responseException = (WebClientResponseException) throwable;
680 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
681 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
682 if (expectApplicationProblemJsonMediaType) {
683 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);