+ void producerDeleteInfoProducer() throws Exception {
+ putInfoProducerWithOneType("infoProducerId", TYPE_ID);
+ putInfoProducerWithOneType("infoProducerId2", TYPE_ID);
+
+ assertThat(this.infoProducers.size()).isEqualTo(2);
+ InfoType type = this.infoTypes.getType(TYPE_ID);
+ assertThat(this.infoProducers.getProducerIdsForType(type.getId())).contains("infoProducerId");
+ assertThat(this.infoProducers.getProducerIdsForType(type.getId())).contains("infoProducerId2");
+ putInfoJob(TYPE_ID, "jobId");
+ assertThat(this.infoJobs.size()).isEqualTo(1);
+
+ deleteInfoProducer("infoProducerId");
+ assertThat(this.infoProducers.size()).isEqualTo(1);
+ assertThat(this.infoProducers.getProducerIdsForType(TYPE_ID)).doesNotContain("infoProducerId");
+ verifyJobStatus("jobId", "ENABLED");
+
+ deleteInfoProducer("infoProducerId2");
+ assertThat(this.infoProducers.size()).isZero();
+ assertThat(this.infoTypes.size()).isEqualTo(1);
+ verifyJobStatus("jobId", "DISABLED");
+
+ String url = ProducerConsts.API_ROOT + "/info-producers/" + "junk";
+ testErrorCode(restClient().delete(url), HttpStatus.NOT_FOUND, "Could not find Information Producer");
+ }
+
+ @Test
+ void a1eJobStatusNotifications() throws JsonMappingException, JsonProcessingException, ServiceException {
+ ConsumerSimulatorController.TestResults consumerCalls = this.consumerSimulator.getTestResults();
+ ProducerSimulatorController.TestResults producerCalls = this.producerSimulator.getTestResults();
+
+ putInfoProducerWithOneType("infoProducerId", TYPE_ID);
+ putInfoJob(TYPE_ID, "jobId");
+ putInfoProducerWithOneType("infoProducerId2", TYPE_ID);
+ await().untilAsserted(() -> assertThat(producerCalls.jobsStarted.size()).isEqualTo(2));
+
+ deleteInfoProducer("infoProducerId2");
+ assertThat(this.infoTypes.size()).isEqualTo(1); // The type remains, one producer left
+ deleteInfoProducer("infoProducerId");
+ assertThat(this.infoTypes.size()).isEqualTo(1); // The type remains
+ assertThat(this.infoJobs.size()).isEqualTo(1); // The job remains
+ await().untilAsserted(() -> assertThat(consumerCalls.eiJobStatusCallbacks.size()).isEqualTo(1));
+ assertThat(consumerCalls.eiJobStatusCallbacks.get(0).state)
+ .isEqualTo(A1eEiJobStatus.EiJobStatusValues.DISABLED);
+
+ putInfoProducerWithOneType("infoProducerId", TYPE_ID);
+ await().untilAsserted(() -> assertThat(consumerCalls.eiJobStatusCallbacks.size()).isEqualTo(2));
+ assertThat(consumerCalls.eiJobStatusCallbacks.get(1).state).isEqualTo(A1eEiJobStatus.EiJobStatusValues.ENABLED);
+ }
+
+ @Test
+ void a1eJobStatusNotifications2() throws JsonMappingException, JsonProcessingException, ServiceException {
+ // Test replacing a producer with new and removed types
+
+ // Create a job
+ putInfoProducerWithOneType(PRODUCER_ID, TYPE_ID);
+ putInfoJob(TYPE_ID, EI_JOB_ID);
+
+ // change the type for the producer, the job shall be disabled
+ putInfoProducerWithOneType(PRODUCER_ID, "junk");
+ verifyJobStatus(EI_JOB_ID, "DISABLED");
+ ConsumerSimulatorController.TestResults consumerCalls = this.consumerSimulator.getTestResults();
+ await().untilAsserted(() -> assertThat(consumerCalls.eiJobStatusCallbacks.size()).isEqualTo(1));
+ assertThat(consumerCalls.eiJobStatusCallbacks.get(0).state)
+ .isEqualTo(A1eEiJobStatus.EiJobStatusValues.DISABLED);
+
+ putInfoProducerWithOneType(PRODUCER_ID, TYPE_ID);
+ verifyJobStatus(EI_JOB_ID, "ENABLED");
+ await().untilAsserted(() -> assertThat(consumerCalls.eiJobStatusCallbacks.size()).isEqualTo(2));
+ assertThat(consumerCalls.eiJobStatusCallbacks.get(1).state).isEqualTo(A1eEiJobStatus.EiJobStatusValues.ENABLED);
+ }
+
+ @Test
+ void producerGetProducerInfoType() throws JsonMappingException, JsonProcessingException, ServiceException {
+ putInfoProducerWithOneType(PRODUCER_ID, TYPE_ID);
+ String url = ProducerConsts.API_ROOT + "/info-types/" + TYPE_ID;
+ ResponseEntity<String> resp = restClient().getForEntity(url).block();
+ ProducerInfoTypeInfo info = gson.fromJson(resp.getBody(), ProducerInfoTypeInfo.class);
+ assertThat(info.jobDataSchema).isNotNull();
+ assertThat(info.typeSpecificInformation).isNotNull();
+
+ testErrorCode(restClient().get(url + "junk"), HttpStatus.NOT_FOUND, "Information type not found");
+ }
+
+ @Test
+ void producerGetProducerIdentifiers() throws JsonMappingException, JsonProcessingException, ServiceException {
+ putInfoProducerWithOneType(PRODUCER_ID, TYPE_ID);
+ String url = ProducerConsts.API_ROOT + "/info-producers";
+ ResponseEntity<String> resp = restClient().getForEntity(url).block();
+ assertThat(resp.getBody()).contains(PRODUCER_ID);
+
+ url = ProducerConsts.API_ROOT + "/info-producers?info_type_id=" + TYPE_ID;
+ resp = restClient().getForEntity(url).block();
+ assertThat(resp.getBody()).contains(PRODUCER_ID);
+
+ url = ProducerConsts.API_ROOT + "/info-producers?info_type_id=junk";
+ resp = restClient().getForEntity(url).block();
+ assertThat(resp.getBody()).isEqualTo("[]");
+ }
+
+ @Test
+ void producerSupervision() throws JsonMappingException, JsonProcessingException, ServiceException {
+
+ ConsumerSimulatorController.TestResults consumerResults = this.consumerSimulator.getTestResults();
+ putInfoProducerWithOneTypeRejecting("simulateProducerError", TYPE_ID);
+
+ {
+ // Create a job
+ putInfoProducerWithOneType(PRODUCER_ID, TYPE_ID);
+ putInfoJob(TYPE_ID, EI_JOB_ID);
+ verifyJobStatus(EI_JOB_ID, "ENABLED");
+ deleteInfoProducer(PRODUCER_ID);
+ // A Job disabled status notification shall now be received
+ await().untilAsserted(() -> assertThat(consumerResults.eiJobStatusCallbacks.size()).isEqualTo(1));
+ assertThat(consumerResults.eiJobStatusCallbacks.get(0).state)
+ .isEqualTo(A1eEiJobStatus.EiJobStatusValues.DISABLED);
+ verifyJobStatus(EI_JOB_ID, "DISABLED");
+ }
+
+ assertThat(this.infoProducers.size()).isEqualTo(1);
+ assertThat(this.infoTypes.size()).isEqualTo(1);
+ assertProducerOpState("simulateProducerError", ProducerStatusInfo.OperationalState.ENABLED);
+
+ this.producerSupervision.createTask().blockLast();
+ this.producerSupervision.createTask().blockLast();
+
+ // Now we have one producer that is disabled
+ assertThat(this.infoProducers.size()).isEqualTo(1);
+ assertProducerOpState("simulateProducerError", ProducerStatusInfo.OperationalState.DISABLED);
+
+ // After 3 failed checks, the producer shall be deregistered
+ this.producerSupervision.createTask().blockLast();
+ assertThat(this.infoProducers.size()).isZero(); // The producer is removed
+ assertThat(this.infoTypes.size()).isEqualTo(1); // The type remains
+
+ // Now we have one disabled job, and no producer.
+ // PUT a producer, then a Job ENABLED status notification shall be received
+ putInfoProducerWithOneType(PRODUCER_ID, TYPE_ID);
+ await().untilAsserted(() -> assertThat(consumerResults.eiJobStatusCallbacks.size()).isEqualTo(2));
+ assertThat(consumerResults.eiJobStatusCallbacks.get(1).state)
+ .isEqualTo(A1eEiJobStatus.EiJobStatusValues.ENABLED);
+ verifyJobStatus(EI_JOB_ID, "ENABLED");
+ }
+
+ @Test
+ void producerSupervision2() throws JsonMappingException, JsonProcessingException, ServiceException {
+ // Test that supervision enables not enabled jobs and sends a notification when
+ // suceeded
+
+ putInfoProducerWithOneType(PRODUCER_ID, TYPE_ID);
+ putInfoJob(TYPE_ID, EI_JOB_ID);
+
+ InfoProducer producer = this.infoProducers.getProducer(PRODUCER_ID);
+ InfoJob job = this.infoJobs.getJob(EI_JOB_ID);
+ // Pretend that the producer did reject the job and the a DISABLED notification
+ // is sent for the job
+ producer.setJobDisabled(job);
+ job.setLastReportedStatus(false);
+ verifyJobStatus(EI_JOB_ID, "DISABLED");
+
+ // Run the supervision and wait for the job to get started in the producer
+ this.producerSupervision.createTask().blockLast();
+ ConsumerSimulatorController.TestResults consumerResults = this.consumerSimulator.getTestResults();
+ await().untilAsserted(() -> assertThat(consumerResults.eiJobStatusCallbacks.size()).isEqualTo(1));
+ assertThat(consumerResults.eiJobStatusCallbacks.get(0).state)
+ .isEqualTo(A1eEiJobStatus.EiJobStatusValues.ENABLED);
+ verifyJobStatus(EI_JOB_ID, "ENABLED");
+ }
+
+ @Test
+ void testGetStatus() throws JsonMappingException, JsonProcessingException, ServiceException {
+ putInfoProducerWithOneTypeRejecting("simulateProducerError", TYPE_ID);
+ putInfoProducerWithOneTypeRejecting("simulateProducerError2", TYPE_ID);
+
+ String url = "/status";
+ ResponseEntity<String> resp = restClient().getForEntity(url).block();
+ assertThat(resp.getBody()).contains("hunky dory");
+ }
+
+ @Test
+ void testEiJobDatabase() throws Exception {
+ putInfoProducerWithOneType(PRODUCER_ID, TYPE_ID);
+ putInfoJob(TYPE_ID, "jobId1");
+ putInfoJob(TYPE_ID, "jobId2");
+
+ assertThat(this.infoJobs.size()).isEqualTo(2);
+
+ {
+ InfoJob savedJob = this.infoJobs.getJob("jobId1");
+ // Restore the jobs
+ InfoJobs jobs = new InfoJobs(this.applicationConfig, this.producerCallbacks);
+ jobs.restoreJobsFromDatabase();
+ assertThat(jobs.size()).isEqualTo(2);
+ InfoJob restoredJob = jobs.getJob("jobId1");
+ assertThat(restoredJob.getId()).isEqualTo("jobId1");
+ assertThat(restoredJob.getLastUpdated()).isEqualTo(savedJob.getLastUpdated());
+
+ jobs.remove("jobId1", this.infoProducers);
+ jobs.remove("jobId2", this.infoProducers);
+ }
+ {
+ // Restore the jobs, no jobs in database
+ InfoJobs jobs = new InfoJobs(this.applicationConfig, this.producerCallbacks);
+ jobs.restoreJobsFromDatabase();
+ assertThat(jobs.size()).isZero();
+ }
+ logger.warn("Test removing a job when the db file is gone");
+ this.infoJobs.remove("jobId1", this.infoProducers);
+ assertThat(this.infoJobs.size()).isEqualTo(1);
+
+ ProducerSimulatorController.TestResults simulatorResults = this.producerSimulator.getTestResults();
+ await().untilAsserted(() -> assertThat(simulatorResults.jobsStopped.size()).isEqualTo(3));
+ }
+
+ @Test
+ void testEiTypesDatabase() throws Exception {
+ putInfoProducerWithOneType(PRODUCER_ID, TYPE_ID);
+
+ assertThat(this.infoTypes.size()).isEqualTo(1);
+
+ {
+ // Restore the types
+ InfoTypes types = new InfoTypes(this.applicationConfig);
+ types.restoreTypesFromDatabase();
+ assertThat(types.size()).isEqualTo(1);
+ }
+ {
+ // Restore the jobs, no jobs in database
+ InfoTypes types = new InfoTypes(this.applicationConfig);
+ types.clear();
+ types.restoreTypesFromDatabase();
+ assertThat(types.size()).isZero();
+ }
+ logger.warn("Test removing a job when the db file is gone");
+ this.infoTypes.remove(this.infoTypes.getType(TYPE_ID));
+ assertThat(this.infoJobs.size()).isZero();
+ }