Commit e619f22e authored by Christos Tranoris's avatar Christos Tranoris
Browse files

Merge remote-tracking branch 'origin/develop' into...

Merge remote-tracking branch 'origin/develop' into 109-integrate-flyway-in-tmf-api-for-db-migration-actions
parents 92b4185a bebcf967
Loading
Loading
Loading
Loading
+14
Changes for src/main/java/org/etsi/osl/tmf/rcm634/reposervices/ResourceSpecificationRepoService.java: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -422,6 +422,20 @@ public class ResourceSpecificationRepoService {

        List<ResourceSpecification> optionalCat = this.resourceSpecificationRepo.findByCategory( acategory );

        // Initialize lazy collections while the transaction is still open: the result is marshalled
        // to JSON by the Camel route after this method returns, when there is no session anymore.
        for (ResourceSpecification dd : optionalCat) {
            Hibernate.initialize(dd.getAttachment());
            Hibernate.initialize(dd.getRelatedParty());
            Hibernate.initialize(dd.getFeatureSpecification());
            Hibernate.initialize(dd.getResourceSpecCharacteristic());
            for (ResourceSpecificationCharacteristic schar : dd.getResourceSpecCharacteristic()) {
                Hibernate.initialize(schar.getResourceSpecCharacteristicValue());
                Hibernate.initialize(schar.getResourceSpecCharRelationship());
            }
            Hibernate.initialize(dd.getResourceSpecRelationship());
        }

        return optionalCat;
    }
    
+31
Changes for src/test/java/org/etsi/osl/services/api/rcm634/ResourceSpecificationRepoServiceTest.java: 31 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -12,6 +12,10 @@ import org.etsi.osl.tmf.rcm634.reposervices.ResourceSpecificationRepoService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@@ -250,4 +254,31 @@ public class ResourceSpecificationRepoServiceTest extends BaseIT {
        assertThat(updated).isInstanceOf(LogicalResourceSpecification.class);
        assertThat(updated).isNotInstanceOf(PhysicalResourceSpecification.class);
    }

    /**
     * CATALOG.GET.RESOURCESPEC_BY_CATEGORY marshals the result to JSON after findByCategory returns,
     * outside any transaction. Lazy collections (e.g. attachment) used to throw
     * LazyInitializationException there. Runs without the BaseIT transaction to reproduce that.
     */
    @Test
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void findByCategory_resultSerializesOutsideTransaction() throws Exception {
        ResourceSpecification created = specRepoService.addResourceSpecificationFromJson(json("""
                {
                  "name": "ByCategorySpec",
                  "version": "1.0.0",
                  "category": "bycategory.lazy.test",
                  "@type": "PhysicalResourceSpecification",
                  "model": "M-1"
                }"""));
        try {
            List<ResourceSpecification> specs = specRepoService.findByCategory("bycategory.lazy.test");
            assertThat(specs).hasSize(1);

            String asJson = objectMapper.writeValueAsString(specs);
            assertThat(asJson).contains("ByCategorySpec");
        } finally {
            specRepoService.deleteByUuid(created.getUuid());
        }
    }
}