From 62d355f346798df4f97671671982a06a3ebffc5e Mon Sep 17 00:00:00 2001 From: Christos Tranoris Date: Tue, 5 Nov 2024 23:54:22 +0200 Subject: [PATCH] fix for #50 --- .../reposervices/ResourceRepoService.java | 6 + .../sim638/api/ServiceApiRouteBuilder.java | 26 ++-- .../sim638/service/ServiceRepoService.java | 4 +- .../api/sim638/ServiceRepoServiceTest.java | 135 +++++++++++++----- 4 files changed, 120 insertions(+), 51 deletions(-) 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 9e79d47..7f52235 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 93afc97..b1e6877 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 58b4c91..436cb6c 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 7b6b52c..d011129 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(); -- GitLab