Search in sources :

Example 1 with SchemaDescriptor

use of org.kie.kogito.persistence.api.schema.SchemaDescriptor in project kogito-apps by kiegroup.

the class ProtoSchemaManagerTest method onSchemaRegisteredEvent.

@Test
void onSchemaRegisteredEvent() {
    String processId = "testProcessId";
    String processType = "testProcessType";
    ProcessDescriptor processDescriptor = new ProcessDescriptor(processId, processType);
    String name = "testName";
    String content = "testContent";
    SchemaDescriptor schemaDescriptor = new SchemaDescriptor(name, content, emptyMap(), processDescriptor);
    SchemaType schemaType = new SchemaType(ProtoSchemaAcceptor.PROTO_SCHEMA_TYPE);
    SchemaRegisteredEvent event = new SchemaRegisteredEvent(schemaDescriptor, schemaType);
    protoSchemaManager.onSchemaRegisteredEvent(event);
    verify(protoSchemaAcceptor).accept(eq(schemaType));
    verify(protobufCache).put(eq(name), eq(content));
    verify(processIdModelCache).put(eq(processId), eq(processType));
    verify(remoteCacheManagerAdmin).getOrCreateCache(eq(processId + "_domain"), any(BasicConfiguration.class));
    verify(cacheManager).getDomainModelCacheName(processId);
    verify(cacheTemplate).data("cache_name", processId + "_domain");
    verify(templateInstance).data("indexed", emptySet());
}
Also used : SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) SchemaType(org.kie.kogito.persistence.api.schema.SchemaType) BasicConfiguration(org.infinispan.commons.configuration.BasicConfiguration) Test(org.junit.jupiter.api.Test)

Example 2 with SchemaDescriptor

use of org.kie.kogito.persistence.api.schema.SchemaDescriptor in project kogito-apps by kiegroup.

the class ProtoSchemaManagerTest method onSchemaRegisteredEventWithError.

@Test
void onSchemaRegisteredEventWithError() {
    String processId = "testProcessId";
    String processType = "testProcessType";
    ProcessDescriptor processDescriptor = new ProcessDescriptor(processId, processType);
    String name = "testName";
    String content = "testContent";
    SchemaDescriptor schemaDescriptor = new SchemaDescriptor(name, content, emptyMap(), processDescriptor);
    SchemaType schemaType = new SchemaType(ProtoSchemaAcceptor.PROTO_SCHEMA_TYPE);
    SchemaRegisteredEvent event = new SchemaRegisteredEvent(schemaDescriptor, schemaType);
    when(protobufCache.containsKey(eq(ProtobufMetadataManagerConstants.ERRORS_KEY_SUFFIX))).thenReturn(true);
    when(protobufCache.get(eq(ProtobufMetadataManagerConstants.ERRORS_KEY_SUFFIX))).thenReturn("testError");
    assertThrows(SchemaRegistrationException.class, () -> protoSchemaManager.onSchemaRegisteredEvent(event));
    verify(protoSchemaAcceptor).accept(eq(schemaType));
    verify(protobufCache).put(eq(name), eq(content));
    verify(processIdModelCache).put(eq(processId), eq(processType));
}
Also used : SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) SchemaType(org.kie.kogito.persistence.api.schema.SchemaType) Test(org.junit.jupiter.api.Test)

Example 3 with SchemaDescriptor

use of org.kie.kogito.persistence.api.schema.SchemaDescriptor in project kogito-apps by kiegroup.

the class ProtoSchemaManager method onSchemaRegisteredEvent.

public void onSchemaRegisteredEvent(@Observes SchemaRegisteredEvent event) {
    if (schemaAcceptor.accept(event.getSchemaType())) {
        SchemaDescriptor schemaDescriptor = event.getSchemaDescriptor();
        Storage<String, String> cache = protobufCacheService.getProtobufCache();
        cache.put(schemaDescriptor.getName(), schemaDescriptor.getSchemaContent());
        schemaDescriptor.getProcessDescriptor().ifPresent(processDescriptor -> {
            cacheManager.getProcessIdModelCache().put(processDescriptor.getProcessId(), processDescriptor.getProcessType());
            // Initialize domain cache
            String cacheName = cacheManager.getDomainModelCacheName(processDescriptor.getProcessId());
            String cacheTemplateRendered = getTemplateRendered(schemaDescriptor, cacheName);
            LOGGER.debug("Cache template: \n{}", cacheTemplateRendered);
            manager.administration().getOrCreateCache(cacheName, new XMLStringConfiguration(cacheTemplateRendered));
        });
        List<String> errors = checkSchemaErrors(cache);
        if (!errors.isEmpty()) {
            String message = "Proto Schema contain errors:\n" + String.join("\n", errors);
            throw new SchemaRegistrationException(message);
        }
        if (LOGGER.isDebugEnabled()) {
            logProtoCacheKeys();
        }
    }
}
Also used : SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) XMLStringConfiguration(org.infinispan.commons.configuration.XMLStringConfiguration) SchemaRegistrationException(org.kie.kogito.persistence.api.schema.SchemaRegistrationException)

Example 4 with SchemaDescriptor

use of org.kie.kogito.persistence.api.schema.SchemaDescriptor in project kogito-apps by kiegroup.

the class ProtobufServiceTest method registerProtoBufferTypeSchemaRegistrationFailed.

@Test
void registerProtoBufferTypeSchemaRegistrationFailed() {
    String testExceptionMessage = "test schema registration fail";
    doThrow(new RuntimeException(testExceptionMessage)).when(schemaEvent).fire(any(SchemaRegisteredEvent.class));
    String content = TestUtils.getTestFileContent();
    String exceptionMessage = "";
    try {
        protobufService.registerProtoBufferType(content);
    } catch (ProtobufValidationException e) {
        exceptionMessage = e.getMessage();
    }
    assertEquals(testExceptionMessage, exceptionMessage);
    verify(schemaEvent, times(1)).fire(new SchemaRegisteredEvent(new SchemaDescriptor(TestUtils.PROCESS_ID + ".proto", content, getValidEntityIndexDescriptors(true), new ProcessDescriptor(TestUtils.PROCESS_ID, TestUtils.PROCESS_TYPE)), SCHEMA_TYPE));
    verify(domainModelEvent, never()).fire(any(FileDescriptorRegisteredEvent.class));
}
Also used : SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) Test(org.junit.jupiter.api.Test)

Example 5 with SchemaDescriptor

use of org.kie.kogito.persistence.api.schema.SchemaDescriptor in project kogito-apps by kiegroup.

the class IndexManagerIT method testOnSchemaRegisteredEvent.

@Test
void testOnSchemaRegisteredEvent() {
    Map<String, EntityIndexDescriptor> indexes = new HashMap<>();
    indexes.put("test", travelEntityIndexDescriptor);
    indexManager.getCollectionIndexMapping().put("test", travelEntityIndexDescriptor.getName());
    indexManager.onSchemaRegisteredEvent(new SchemaRegisteredEvent(new SchemaDescriptor("test", "test", indexes, new ProcessDescriptor("test", travelEntityIndexDescriptor.getName())), new SchemaType("test")));
    MongoCollection<Document> testCollection = indexManager.getCollection("test");
    collections.add(testCollection);
    Set<String> testIndexes = StreamSupport.stream(testCollection.listIndexes().spliterator(), false).map(document -> document.getString(INDEX_NAME_FIELD)).filter(name -> !DEFAULT_INDEX.equals(name)).collect(toSet());
    assertEquals(getTestIndexNames(), testIndexes);
    mockProcessIndexEventListener.assertFire("test", travelEntityIndexDescriptor.getName());
}
Also used : IntStream(java.util.stream.IntStream) Document(org.bson.Document) Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) MongoDBQuarkusTestResource(org.kie.kogito.testcontainers.quarkus.MongoDBQuarkusTestResource) ArgumentMatchers(org.mockito.ArgumentMatchers) IndexModel(com.mongodb.client.model.IndexModel) SchemaRegistrationException(org.kie.kogito.persistence.api.schema.SchemaRegistrationException) MongoCollection(com.mongodb.client.MongoCollection) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) HashMap(java.util.HashMap) QuarkusTest(io.quarkus.test.junit.QuarkusTest) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Bson(org.bson.conversions.Bson) QuarkusMock.installMockForType(io.quarkus.test.junit.QuarkusMock.installMockForType) SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) INDEX_NAME_FIELD(org.kie.kogito.persistence.mongodb.index.IndexManager.INDEX_NAME_FIELD) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LinkedList(java.util.LinkedList) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) MockProcessIndexEventListener(org.kie.kogito.persistence.mongodb.mock.MockProcessIndexEventListener) Collectors.toSet(java.util.stream.Collectors.toSet) QuarkusTestResource(io.quarkus.test.common.QuarkusTestResource) SchemaType(org.kie.kogito.persistence.api.schema.SchemaType) Set(java.util.Set) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) IndexOptions(com.mongodb.client.model.IndexOptions) Test(org.junit.jupiter.api.Test) Indexes(com.mongodb.client.model.Indexes) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Collectors.toList(java.util.stream.Collectors.toList) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) DEFAULT_INDEX(org.kie.kogito.persistence.mongodb.index.IndexManager.DEFAULT_INDEX) Mockito.mock(org.mockito.Mockito.mock) SchemaDescriptor(org.kie.kogito.persistence.api.schema.SchemaDescriptor) HashMap(java.util.HashMap) SchemaRegisteredEvent(org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent) ProcessDescriptor(org.kie.kogito.persistence.api.schema.ProcessDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) Document(org.bson.Document) SchemaType(org.kie.kogito.persistence.api.schema.SchemaType) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Aggregations

SchemaDescriptor (org.kie.kogito.persistence.api.schema.SchemaDescriptor)9 SchemaRegisteredEvent (org.kie.kogito.persistence.api.schema.SchemaRegisteredEvent)8 Test (org.junit.jupiter.api.Test)6 ProcessDescriptor (org.kie.kogito.persistence.api.schema.ProcessDescriptor)6 EntityIndexDescriptor (org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)3 SchemaType (org.kie.kogito.persistence.api.schema.SchemaType)3 SerializationContext (org.infinispan.protostream.SerializationContext)2 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)2 SchemaRegistrationException (org.kie.kogito.persistence.api.schema.SchemaRegistrationException)2 MongoCollection (com.mongodb.client.MongoCollection)1 IndexModel (com.mongodb.client.model.IndexModel)1 IndexOptions (com.mongodb.client.model.IndexOptions)1 Indexes (com.mongodb.client.model.Indexes)1 StartupEvent (io.quarkus.runtime.StartupEvent)1 QuarkusTestResource (io.quarkus.test.common.QuarkusTestResource)1 QuarkusMock.installMockForType (io.quarkus.test.junit.QuarkusMock.installMockForType)1 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1