Search in sources :

Example 1 with IEditingContextPersistenceService

use of org.eclipse.sirius.components.core.api.IEditingContextPersistenceService in project sirius-web by eclipse-sirius.

the class EditingContextPersistenceServiceTests method testDocumentPersistence.

@Test
public void testDocumentPersistence() {
    UUID projectId = UUID.randomUUID();
    // $NON-NLS-1$
    String name = "New Document";
    UUID id = UUID.randomUUID();
    JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI(id.toString()));
    resource.eAdapters().add(new DocumentMetadataAdapter(name));
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    // $NON-NLS-1$
    eClass.setName("Concept");
    resource.getContents().add(eClass);
    AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
    editingDomain.getResourceSet().getResources().add(resource);
    ProjectEntity projectEntity = new ProjectEntity();
    projectEntity.setId(projectId);
    // $NON-NLS-1$
    projectEntity.setName("");
    AccountEntity owner = new AccountEntity();
    owner.setId(UUID.randomUUID());
    // $NON-NLS-1$
    owner.setUsername("jdoe");
    projectEntity.setOwner(owner);
    DocumentEntity existingEntity = new DocumentEntity();
    existingEntity.setId(id);
    existingEntity.setProject(projectEntity);
    existingEntity.setName(name);
    // $NON-NLS-1$
    existingEntity.setContent("");
    List<DocumentEntity> entities = new ArrayList<>();
    IDocumentRepository documentRepository = new NoOpDocumentRepository() {

        @Override
        public <S extends DocumentEntity> S save(S entity) {
            entities.add(entity);
            return entity;
        }

        @Override
        public Optional<DocumentEntity> findById(UUID id) {
            return Optional.of(existingEntity);
        }
    };
    IEditingContextPersistenceService editingContextPersistenceService = new EditingContextPersistenceService(documentRepository, new NoOpApplicationEventPublisher(), new SimpleMeterRegistry());
    assertThat(entities).hasSize(0);
    IEditingContext editingContext = new EditingContext(UUID.randomUUID().toString(), editingDomain);
    editingContextPersistenceService.persist(editingContext);
    assertThat(entities).hasSize(1);
    DocumentEntity documentEntity = entities.get(0);
    assertThat(documentEntity.getId()).isEqualTo(id);
    assertThat(documentEntity.getName()).isEqualTo(name);
    assertThat(documentEntity.getProject().getId()).isEqualTo(projectId);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) DocumentMetadataAdapter(org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) ArrayList(java.util.ArrayList) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) IEditingContextPersistenceService(org.eclipse.sirius.components.core.api.IEditingContextPersistenceService) AccountEntity(org.eclipse.sirius.web.persistence.entities.AccountEntity) EClass(org.eclipse.emf.ecore.EClass) IDocumentRepository(org.eclipse.sirius.web.persistence.repositories.IDocumentRepository) DocumentEntity(org.eclipse.sirius.web.persistence.entities.DocumentEntity) IEditingContextPersistenceService(org.eclipse.sirius.components.core.api.IEditingContextPersistenceService) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ProjectEntity(org.eclipse.sirius.web.persistence.entities.ProjectEntity) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) UUID(java.util.UUID) EditingDomainFactory(org.eclipse.sirius.web.services.documents.EditingDomainFactory) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 EClass (org.eclipse.emf.ecore.EClass)1 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)1 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)1 IEditingContextPersistenceService (org.eclipse.sirius.components.core.api.IEditingContextPersistenceService)1 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)1 SiriusWebJSONResourceFactoryImpl (org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)1 JsonResource (org.eclipse.sirius.emfjson.resource.JsonResource)1 AccountEntity (org.eclipse.sirius.web.persistence.entities.AccountEntity)1 DocumentEntity (org.eclipse.sirius.web.persistence.entities.DocumentEntity)1 ProjectEntity (org.eclipse.sirius.web.persistence.entities.ProjectEntity)1 IDocumentRepository (org.eclipse.sirius.web.persistence.repositories.IDocumentRepository)1 DocumentMetadataAdapter (org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter)1 EditingDomainFactory (org.eclipse.sirius.web.services.documents.EditingDomainFactory)1 Test (org.junit.jupiter.api.Test)1