diff --git a/src/main/java/org/etsi/osl/tmf/ri639/reposervices/ResourceRepoService.java b/src/main/java/org/etsi/osl/tmf/ri639/reposervices/ResourceRepoService.java index 9e79d472e5903d40fea50714d745f4762057ae2b..7f522352b12b86bfa2404f994abd7d780dc2341c 100644 --- a/src/main/java/org/etsi/osl/tmf/ri639/reposervices/ResourceRepoService.java +++ b/src/main/java/org/etsi/osl/tmf/ri639/reposervices/ResourceRepoService.java @@ -60,6 +60,7 @@ import org.etsi.osl.tmf.ri639.model.ResourceStateChangeNotification; import org.etsi.osl.tmf.ri639.model.ResourceUpdate; import org.etsi.osl.tmf.ri639.repo.ResourceRepository; import org.etsi.osl.tmf.sim638.model.Service; +import org.etsi.osl.tmf.sim638.service.ServiceRepoService; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; @@ -91,6 +92,9 @@ public class ResourceRepoService { @Autowired ResourceApiRouteBuilderEvents resourceApiRouteBuilder; + + @Autowired + ServiceRepoService serviceRepoService; @Autowired public ResourceRepoService(EntityManagerFactory factory) { @@ -445,6 +449,8 @@ public class ResourceRepoService { resource = this.resourceRepo.save(resource); + this.serviceRepoService.updateServicesHavingThisSupportingResource(resource); + if (resourceCharacteristicChanged) { raiseResourceAttributeValueChangeEventNotification(resource); } else if (resourceStateChanged) { diff --git a/src/main/java/org/etsi/osl/tmf/sim638/api/ServiceApiRouteBuilder.java b/src/main/java/org/etsi/osl/tmf/sim638/api/ServiceApiRouteBuilder.java index 93afc976d554ffe86976320a3e0d56fd7b591191..b1e68771a517dcf2964a114df51ce35337396cfc 100644 --- a/src/main/java/org/etsi/osl/tmf/sim638/api/ServiceApiRouteBuilder.java +++ b/src/main/java/org/etsi/osl/tmf/sim638/api/ServiceApiRouteBuilder.java @@ -224,19 +224,19 @@ public class ServiceApiRouteBuilder extends RouteBuilder { - from( EVENT_RESOURCE_ATTRIBUTE_VALUE_CHANGED ) - .errorHandler(deadLetterChannel("direct:retriesDeadLetters") - .maximumRedeliveries(5) - .redeliveryDelay(1000).useOriginalMessage() - .logExhausted(true) - .logHandled(true) - .retriesExhaustedLogLevel(LoggingLevel.ERROR) - .retryAttemptedLogLevel(LoggingLevel.ERROR)) - - .log(LoggingLevel.INFO, log, EVENT_RESOURCE_ATTRIBUTE_VALUE_CHANGED + " message received and will be processed for service inventory!") - .to("log:DEBUG?showBody=true&showHeaders=true") - .unmarshal().json( JsonLibrary.Jackson, ResourceAttributeValueChangeNotification.class, true) - .bean( serviceRepoService, "resourceAttrChangedEvent(${body})"); +// from( EVENT_RESOURCE_ATTRIBUTE_VALUE_CHANGED ) +// .errorHandler(deadLetterChannel("direct:retriesDeadLetters") +// .maximumRedeliveries(5) +// .redeliveryDelay(1000).useOriginalMessage() +// .logExhausted(true) +// .logHandled(true) +// .retriesExhaustedLogLevel(LoggingLevel.ERROR) +// .retryAttemptedLogLevel(LoggingLevel.ERROR)) +// +// .log(LoggingLevel.INFO, log, EVENT_RESOURCE_ATTRIBUTE_VALUE_CHANGED + " message received and will be processed for service inventory!") +// .to("log:DEBUG?showBody=true&showHeaders=true") +// .unmarshal().json( JsonLibrary.Jackson, ResourceAttributeValueChangeNotification.class, true) +// .bean( serviceRepoService, "resourceAttrChangedEvent(${body})"); diff --git a/src/main/java/org/etsi/osl/tmf/sim638/service/ServiceRepoService.java b/src/main/java/org/etsi/osl/tmf/sim638/service/ServiceRepoService.java index 58b4c91cba358764d8120c62ce22af56f1cbe6f9..436cb6ce6a9690f44c63f2a89761123955b71e05 100644 --- a/src/main/java/org/etsi/osl/tmf/sim638/service/ServiceRepoService.java +++ b/src/main/java/org/etsi/osl/tmf/sim638/service/ServiceRepoService.java @@ -1111,11 +1111,9 @@ public class ServiceRepoService { @Transactional - public void resourceAttrChangedEvent(@Valid ResourceAttributeValueChangeNotification resNotif) { + public void updateServicesHavingThisSupportingResource(@Valid Resource res) { try { - logger.debug("ResourceAttributeValueChangeNotification"); - Resource res = resNotif.getEvent().getEvent().getResource(); logger.info("Will update services related to this resource with id = " + res.getId() ); var aservices = findServicesHavingThisSupportingResourceID( res.getId() ); diff --git a/src/test/java/org/etsi/osl/services/api/sim638/ServiceRepoServiceTest.java b/src/test/java/org/etsi/osl/services/api/sim638/ServiceRepoServiceTest.java index 7b6b52c8390833edec7a1462a0e3551444f03327..d0111296649e424f47fb9a2ab81ddbf896a7d888 100644 --- a/src/test/java/org/etsi/osl/services/api/sim638/ServiceRepoServiceTest.java +++ b/src/test/java/org/etsi/osl/services/api/sim638/ServiceRepoServiceTest.java @@ -40,7 +40,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.WebApplicationContext; - +import jakarta.validation.Valid; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; @@ -69,6 +69,7 @@ public class ServiceRepoServiceTest { @Autowired ServiceRepoService serviceRepoService; + @Autowired ResourceRepoService resourceRepoService; @@ -230,42 +231,106 @@ public class ServiceRepoServiceTest { // // org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection [HikariPool-1 - Connection is not available, request timed out after 30000ms.] -// @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) -// @Test -// public void testResourceStateChangedEvent() throws Exception { -// String response = createService(); -// Service responsesService = JsonUtils.toJsonObj(response, Service.class); -// String id = responsesService.getId(); -// Set resourceRefSet = responsesService.getSupportingResource(); -// List resourceRefList = new ArrayList<>(resourceRefSet); -// -// assertThat(resourceRefList.size()).isEqualTo(1); -// ResourceRef firstResourceRef = resourceRefList.get(0); -// -// Resource resource = resourceRepoService.findByUuid(firstResourceRef.getId()); -// -// ResourceStateChangeNotification resourceCreateNotification = new ResourceStateChangeNotification(); -// ResourceStateChangeEvent event = new ResourceStateChangeEvent(); -// event.getEvent().setResource(resource); -// resourceCreateNotification.setEvent(event); -// -// serviceRepoService.resourceStateChangedEvent(resourceCreateNotification); -// Service updatedService = serviceRepoService.findByUuid(id); -// -// Set noteSet = updatedService.getNote(); -// List noteList = new ArrayList<>(noteSet); -// -// boolean expectedNoteExists = false; -// for (Note n : noteList) { -// if ( n.getText().contains("State Changed with status:") && n.getAuthor().equals("SIM638-API")) { -// expectedNoteExists= true; -// break; -// } -// } -// assertThat( expectedNoteExists ).isTrue(); -// } + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testResourceStateChangedEvent() throws Exception { + String response = createService(); + Service responsesService = JsonUtils.toJsonObj(response, Service.class); + String id = responsesService.getId(); + Set resourceRefSet = responsesService.getSupportingResource(); + List resourceRefList = new ArrayList<>(resourceRefSet); + + assertThat(resourceRefList.size()).isEqualTo(1); + ResourceRef firstResourceRef = resourceRefList.get(0); + + Resource resource = resourceRepoService.findByUuid(firstResourceRef.getId()); + + ResourceStateChangeNotification resourceCreateNotification = new ResourceStateChangeNotification(); + ResourceStateChangeEvent event = new ResourceStateChangeEvent(); + event.getEvent().setResource(resource); + resourceCreateNotification.setEvent(event); + + serviceRepoService.resourceStateChangedEvent(resourceCreateNotification); + Service updatedService = serviceRepoService.findByUuid(id); + + Set noteSet = updatedService.getNote(); + List noteList = new ArrayList<>(noteSet); + + boolean expectedNoteExists = false; + for (Note n : noteList) { + if ( n.getText().contains("State Changed with status:") && n.getAuthor().equals("SIM638-API")) { + expectedNoteExists= true; + break; + } + } + assertThat( expectedNoteExists ).isTrue(); + } + @WithMockUser(username="osadmin", roles = {"ADMIN","USER"}) + @Test + public void testResourceAttrChangedEvent() throws Exception { + String response = createService(); + Service responsesService = JsonUtils.toJsonObj(response, Service.class); + String id = responsesService.getId(); + Set resourceRefSet = responsesService.getSupportingResource(); + List resourceRefList = new ArrayList<>(resourceRefSet); + + assertThat(resourceRefList.size()).isEqualTo(1); + ResourceRef firstResourceRef = resourceRefList.get(0); + + Resource resource = resourceRepoService.findByUuid(firstResourceRef.getId()); + assertThat( resource.getResourceCharacteristic().size() ).isEqualTo( 0 ); + + + + @Valid ResourceAttributeValueChangeNotification resNotid = new ResourceAttributeValueChangeNotification(); + ResourceAttributeValueChangeEvent event = new ResourceAttributeValueChangeEvent(); + event.getEvent().setResource(resource); + + resNotid.setEvent( event ); + + Service updatedService = serviceRepoService.findByUuid(id); + + assertThat( updatedService.getServiceCharacteristic().size() ).isEqualTo( 6 ); + assertThat( updatedService.getSupportingResource().size() ).isEqualTo( 1); + + ResourceUpdate resourceUpdate = new ResourceUpdate(); + + org.etsi.osl.tmf.ri639.model.Characteristic resCharacteristicItem = new org.etsi.osl.tmf.ri639.model.Characteristic(); + + resCharacteristicItem.setName( "newChar" ); + resCharacteristicItem.setValue( new Any("myval0")); + resourceUpdate.addResourceCharacteristicItem(resCharacteristicItem); + + + System.out.println("STEP 1 - =========================================== " +serviceRepoService.toString() ); + Resource nullResource = resourceRepoService.updateResource( resource.getId(), resourceUpdate, false); + resource = resourceRepoService.findByUuid(firstResourceRef.getId()); + assertThat( resource.getResourceCharacteristic().size() ).isEqualTo( 1 ); + + Thread.sleep(1000); + + System.out.println("STEP 3 - =========================================== " ); + updatedService = serviceRepoService.findByUuid(id); + assertThat( updatedService.getSupportingResource().size() ).isEqualTo( 1); + assertThat( updatedService.getServiceCharacteristic().size() ).isEqualTo( 7 ); + + Set noteSet = updatedService.getNote(); + List noteList = new ArrayList<>(noteSet); + + boolean expectedNoteExists = false; + for (Note n : noteList) { + if ( n.getText().contains("Service Characteristic changed") && n.getAuthor().equals("API")) { + expectedNoteExists= true; + break; + } + } + assertThat( expectedNoteExists ).isTrue(); + } + + + @Transactional private String createService() throws Exception { int servicesCount = serviceRepoService.findAll().size();