Commit 62d355f3 authored by Christos Tranoris's avatar Christos Tranoris
Browse files

fix for #50

parent ce1392e7
Loading
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -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;
@@ -92,6 +93,9 @@ public class ResourceRepoService {
  @Autowired
  ResourceApiRouteBuilderEvents resourceApiRouteBuilder;
  
  @Autowired
  ServiceRepoService serviceRepoService;

  @Autowired
  public ResourceRepoService(EntityManagerFactory factory) {
    if (factory.unwrap(SessionFactory.class) == null) {
@@ -445,6 +449,8 @@ public class ResourceRepoService {


    resource = this.resourceRepo.save(resource);
    this.serviceRepoService.updateServicesHavingThisSupportingResource(resource);
    
    if (resourceCharacteristicChanged) {
      raiseResourceAttributeValueChangeEventNotification(resource);
    } else if (resourceStateChanged) {
+13 −13
Original line number Diff line number Diff line
@@ -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})");
		
        
        
+1 −3
Original line number Diff line number Diff line
@@ -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() );
+100 −35
Original line number Diff line number Diff line
@@ -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;
@@ -70,6 +70,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<ResourceRef> resourceRefSet = responsesService.getSupportingResource();
//        List<ResourceRef> 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<Note> noteSet = updatedService.getNote();
//        List<Note> 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<ResourceRef> resourceRefSet = responsesService.getSupportingResource();
        List<ResourceRef> 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<Note> noteSet = updatedService.getNote();
        List<Note> 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<ResourceRef> resourceRefSet = responsesService.getSupportingResource();
        List<ResourceRef> 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<Note> noteSet = updatedService.getNote();
        List<Note> 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();