Search in sources :

Example 1 with Constraint

use of io.lumeer.api.model.Constraint in project engine by Lumeer.

the class ConstraintManagerTest method testDateTimeConstraint.

@Test
public void testDateTimeConstraint() {
    final ConstraintManager cm = new ConstraintManager();
    cm.setLocale(l);
    final Date d = new Date(1234567890);
    final ZonedDateTime dt = ZonedDateTime.from(d.toInstant().atZone(ZoneId.of("Asia/Kolkata")));
    final String valid1 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ", l).format(dt);
    final String valid2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSO", l).format(dt);
    final String valid3 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSx", l).format(dt);
    final String valid4 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX", l).format(dt);
    Object encoded = cm.encode(valid1, new Constraint(ConstraintType.DateTime, null));
    assertThat(encoded).isInstanceOf(Date.class);
    assertThat(((Date) encoded).toInstant()).isEqualTo(d.toInstant());
    encoded = cm.encode(valid2, new Constraint(ConstraintType.DateTime, null));
    assertThat(((Date) encoded).toInstant()).isEqualTo(d.toInstant());
    encoded = cm.encode(valid3, new Constraint(ConstraintType.DateTime, null));
    assertThat(encoded).isEqualTo(d);
    encoded = cm.encode(valid4, new Constraint(ConstraintType.DateTime, null));
    assertThat(encoded).isEqualTo(d);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Constraint(io.lumeer.api.model.Constraint) Date(java.util.Date) Test(org.junit.Test)

Example 2 with Constraint

use of io.lumeer.api.model.Constraint in project engine by Lumeer.

the class ConstraintManagerTest method testNumberConstraint.

@Test
public void testNumberConstraint() {
    final Constraint numberConstraint = new Constraint(ConstraintType.Number, null);
    final ConstraintManager cm = new ConstraintManager();
    cm.setLocale(l);
    final NumberFormat nf = NumberFormat.getNumberInstance(l);
    final String validDouble = nf.format(0.02);
    final String validLong = nf.format(123);
    final BigDecimal bdv = new BigDecimal("123454.434563456345");
    final String validBigDecimal = bdv.toString();
    final BigDecimal bd = new BigDecimal(Double.MAX_VALUE).multiply(new BigDecimal(Double.MAX_VALUE));
    // does not fit into Decimal128
    final String unstorableBigDecimal = bd.toString();
    Object encoded = cm.encode(validDouble, numberConstraint);
    assertThat(encoded).isInstanceOf(BigDecimal.class);
    assertThat(encoded).isEqualTo(new BigDecimal("0.02"));
    encoded = cm.encode(validLong, numberConstraint);
    assertThat(encoded).isInstanceOf(Long.class);
    assertThat(encoded).isEqualTo(123L);
    encoded = cm.encode(validBigDecimal, numberConstraint);
    assertThat(encoded).isInstanceOf(BigDecimal.class);
    assertThat(encoded).isEqualTo(bdv);
    encoded = cm.encode(unstorableBigDecimal, numberConstraint);
    assertThat(encoded).isInstanceOf(String.class);
    assertThat(encoded).isEqualTo(unstorableBigDecimal);
}
Also used : Constraint(io.lumeer.api.model.Constraint) BigDecimal(java.math.BigDecimal) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Example 3 with Constraint

use of io.lumeer.api.model.Constraint in project engine by Lumeer.

the class CollectionFacadeIT method testAttributeConversion.

@Test
public void testAttributeConversion() {
    Collection collection = collectionFacade.createCollection(prepareCollection(CODE3));
    collectionFacade.createCollectionAttributes(collection.getId(), Arrays.asList(new Attribute("a1", "Task", null, null, null, null, 0, null), new Attribute("a2", ATTRIBUTE_STATE, null, null, null, null, 0, null)));
    var values = Arrays.asList("New", "In Progress", "To Do", "Done", "Won't fix");
    var rnd = new Random();
    for (int i = 0; i < 100; i++) {
        documentFacade.createDocument(collection.getId(), new Document(new DataDocument("a1", "Task-" + (i + 1)).append("a2", values.get(rnd.nextInt(values.size())))));
    }
    var attributes = collectionFacade.getCollection(collection.getId()).getAttributes();
    var attr = attributes.stream().filter(attribute -> attribute.getName().equals(ATTRIBUTE_STATE)).findFirst().get();
    attr.setConstraint(new Constraint(ConstraintType.Select, new org.bson.Document("options", List.of(new org.bson.Document("value", "New").append("displayValue", ""), new org.bson.Document("value", "In Progress").append("displayValue", ""), new org.bson.Document("value", "To Do").append("displayValue", ""), new org.bson.Document("value", "Done").append("displayValue", ""), new org.bson.Document("value", "Won't fix").append("displayValue", "")))));
    collectionFacade.updateCollectionAttribute(collection.getId(), attr.getId(), attr);
    var documents = documentDao.getDocumentsByCollection(collection.getId());
    documents.forEach(document -> {
        var doc = documentFacade.getDocument(collection.getId(), document.getId());
        assertThat(doc.getData().getString(attr.getId())).isIn(values);
    });
    // now add display value
    var attr2 = attributes.stream().filter(attribute -> attribute.getName().equals(ATTRIBUTE_STATE)).findFirst().get();
    attr2.setConstraint(new Constraint(ConstraintType.Select, new org.bson.Document("options", List.of(new org.bson.Document("value", 0).append("displayValue", "New"), new org.bson.Document("value", 1).append("displayValue", "In Progress"), new org.bson.Document("value", 2).append("displayValue", "To Do"), new org.bson.Document("value", 3).append("displayValue", "Done"), new org.bson.Document("value", 4).append("displayValue", "Won't fix"))).append("displayValues", List.of("New", "In Progress", "To Do", "Done", "Won't fix"))));
    collectionFacade.updateCollectionAttribute(collection.getId(), attr2.getId(), attr2);
    documents = documentDao.getDocumentsByCollection(collection.getId());
    documents.forEach(document -> {
        var doc = documentFacade.getDocument(collection.getId(), document.getId());
        assertThat(doc.getData().getInteger(attr.getId())).isBetween(0, 4);
    });
    // now add display value
    var attr3 = attributes.stream().filter(attribute -> attribute.getName().equals(ATTRIBUTE_STATE)).findFirst().get();
    attr3.setConstraint(null);
    collectionFacade.updateCollectionAttribute(collection.getId(), attr3.getId(), attr3);
    documents = documentDao.getDocumentsByCollection(collection.getId());
    documents.forEach(document -> {
        var doc = documentFacade.getDocument(collection.getId(), document.getId());
        assertThat(doc.getData().getString(attr.getId())).isIn(values);
    });
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Random(java.util.Random) Attribute(io.lumeer.api.model.Attribute) Constraint(io.lumeer.api.model.Constraint) Collection(io.lumeer.api.model.Collection) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) Constraint(io.lumeer.api.model.Constraint) Test(org.junit.Test)

Example 4 with Constraint

use of io.lumeer.api.model.Constraint in project engine by Lumeer.

the class DelayedActionIT method configureCollection.

@Before
public void configureCollection() {
    User user = new User(USER);
    setUserNotifications(user);
    this.user = userDao.createUser(user);
    User user2 = new User(USER2);
    setUserNotifications(user2);
    this.user2 = userDao.createUser(user2);
    Organization organization = new Organization();
    organization.setCode(ORGANIZATION_CODE);
    organization.setPermissions(new Permissions());
    Organization storedOrganization = organizationDao.createOrganization(organization);
    organizationId = storedOrganization.getId();
    projectDao.setOrganization(storedOrganization);
    Permissions organizationPermissions = new Permissions();
    Permission userPermission = Permission.buildWithRoles(this.user.getId(), Organization.ROLES);
    Permission user2Permission = Permission.buildWithRoles(this.user2.getId(), Organization.ROLES);
    organizationPermissions.addUserPermissions(Set.of(userPermission, user2Permission));
    storedOrganization.setPermissions(organizationPermissions);
    organizationDao.updateOrganization(storedOrganization.getId(), storedOrganization);
    Project project = new Project();
    project.setCode(PROJECT_CODE);
    Permissions projectPermissions = new Permissions();
    projectPermissions.addUserPermissions(Set.of(new Permission(this.user.getId(), Project.ROLES), new Permission(this.user2.getId(), Project.ROLES)));
    project.setPermissions(projectPermissions);
    Project storedProject = projectDao.createProject(project);
    workspaceKeeper.setWorkspaceIds(storedOrganization.getId(), storedProject.getId());
    collectionDao.setProject(storedProject);
    collectionDao.createRepository(storedProject);
    Permissions collectionPermissions = new Permissions();
    collectionPermissions.addUserPermissions(Set.of(new Permission(this.user.getId(), Project.ROLES), new Permission(this.user2.getId(), Project.ROLES)));
    Collection jsonCollection = new Collection(null, COLLECTION_NAME, COLLECTION_ICON, COLLECTION_COLOR, collectionPermissions);
    jsonCollection.setLastAttributeNum(0);
    jsonCollection.setAttributes(List.of(new Attribute("a0", "Summary", null, new Constraint(ConstraintType.Text, null), null, null, 0, null), new Attribute("a1", "Assignee", null, new Constraint(ConstraintType.User, new org.bson.Document("multi", true).append("externalUsers", true)), null, null, 0, null), new Attribute("a2", "Due date", null, new Constraint(ConstraintType.DateTime, new org.bson.Document("format", "DD/MM/YYYY H:mm:ss")), null, null, 0, null), new Attribute("a3", "State", null, new Constraint(ConstraintType.Select, new org.bson.Document("options", List.of(new org.bson.Document("value", "New").append("displayValue", ""), new org.bson.Document("value", "In Progress").append("displayValue", ""), new org.bson.Document("value", "To Do").append("displayValue", ""), new org.bson.Document("value", "Done").append("displayValue", ""), new org.bson.Document("value", "Won't fix").append("displayValue", "")))), null, null, 0, null), new Attribute("a4", "Observers", null, new Constraint(ConstraintType.User, new org.bson.Document("multi", true).append("externalUsers", true)), null, null, 0, null), new Attribute("a5", "Something", null, null, null, null, 0, null)));
    jsonCollection.setDefaultAttributeId("a0");
    jsonCollection.setPurpose(new CollectionPurpose(CollectionPurposeType.Tasks, new DataDocument(CollectionPurpose.META_ASSIGNEE_ATTRIBUTE_ID, "a1").append(CollectionPurpose.META_DUE_DATE_ATTRIBUTE_ID, "a2").append(CollectionPurpose.META_STATE_ATTRIBUTE_ID, "a3").append(CollectionPurpose.META_FINAL_STATES_LIST, List.of("Done", "Won't fix")).append(CollectionPurpose.META_OBSERVERS_ATTRIBUTE_ID, "a4")));
    collection = collectionDao.createCollection(jsonCollection);
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) User(io.lumeer.api.model.User) AuthenticatedUser(io.lumeer.core.auth.AuthenticatedUser) Organization(io.lumeer.api.model.Organization) Attribute(io.lumeer.api.model.Attribute) Constraint(io.lumeer.api.model.Constraint) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) Project(io.lumeer.api.model.Project) Permissions(io.lumeer.api.model.Permissions) Permission(io.lumeer.api.model.Permission) Collection(io.lumeer.api.model.Collection) CollectionPurpose(io.lumeer.api.model.CollectionPurpose) Before(org.junit.Before)

Example 5 with Constraint

use of io.lumeer.api.model.Constraint in project engine by Lumeer.

the class SearchFacadeIT method testSearchMultiSelectConstraint.

@Test
public void testSearchMultiSelectConstraint() {
    var options = Arrays.asList(new DataDocument("option", "a"), new DataDocument("option", "b"), new DataDocument("option", "c"), new DataDocument("option", "d"));
    Constraint constraint = new Constraint(ConstraintType.Select, new DataDocument("multi", true).append("options", options));
    Attribute attribute = new Attribute(DOCUMENT_KEY, DOCUMENT_KEY, null, constraint, null, null, 3, null);
    String collectionId = createCollection("selectCollection", attribute).getId();
    String id1 = createDocument(collectionId, Arrays.asList("a", "b", "c")).getId();
    String id2 = createDocument(collectionId, Arrays.asList("a", "b")).getId();
    String id3 = createDocument(collectionId, Arrays.asList("a", "c")).getId();
    String id4 = createDocument(collectionId, Arrays.asList("c", "x")).getId();
    String id5 = createDocument(collectionId, Collections.singletonList("d")).getId();
    Query query = createSimpleQueryWithAttributeFilter(collectionId, CollectionAttributeFilter.createFromValues(collectionId, DOCUMENT_KEY, ConditionType.IN, Arrays.asList("a", "b")));
    List<Document> documents = searchFacade.searchDocuments(query, true);
    assertThat(documents).extracting(Document::getId).containsOnly(id2);
    query = createSimpleQueryWithAttributeFilter(collectionId, CollectionAttributeFilter.createFromValues(collectionId, DOCUMENT_KEY, ConditionType.HAS_SOME, Arrays.asList("a", "c")));
    documents = searchFacade.searchDocuments(query, true);
    assertThat(documents).extracting(Document::getId).containsOnly(id1, id2, id3, id4);
    query = createSimpleQueryWithAttributeFilter(collectionId, CollectionAttributeFilter.createFromValues(collectionId, DOCUMENT_KEY, ConditionType.HAS_ALL, Arrays.asList("a", "b")));
    documents = searchFacade.searchDocuments(query, true);
    assertThat(documents).extracting(Document::getId).containsOnly(id1, id2);
    query = createSimpleQueryWithAttributeFilter(collectionId, CollectionAttributeFilter.createFromValues(collectionId, DOCUMENT_KEY, ConditionType.HAS_NONE_OF, Arrays.asList("a", "b")));
    documents = searchFacade.searchDocuments(query, true);
    assertThat(documents).extracting(Document::getId).containsOnly(id4, id5);
    query = createSimpleQueryWithAttributeFilter(collectionId, CollectionAttributeFilter.createFromValues(collectionId, DOCUMENT_KEY, ConditionType.NOT_EMPTY));
    documents = searchFacade.searchDocuments(query, true);
    assertThat(documents).extracting(Document::getId).containsOnly(id1, id2, id3, id4, id5);
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Query(io.lumeer.api.model.Query) Constraint(io.lumeer.api.model.Constraint) Attribute(io.lumeer.api.model.Attribute) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) Test(org.junit.Test)

Aggregations

Constraint (io.lumeer.api.model.Constraint)25 Attribute (io.lumeer.api.model.Attribute)17 Test (org.junit.Test)17 DataDocument (io.lumeer.engine.api.data.DataDocument)15 Document (io.lumeer.api.model.Document)13 Query (io.lumeer.api.model.Query)9 Collection (io.lumeer.api.model.Collection)8 HashMap (java.util.HashMap)5 BigDecimal (java.math.BigDecimal)4 ConstraintType (io.lumeer.api.model.ConstraintType)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Point (com.mongodb.client.model.geojson.Point)2 Position (com.mongodb.client.model.geojson.Position)2 CollectionPurpose (io.lumeer.api.model.CollectionPurpose)2 LinkType (io.lumeer.api.model.LinkType)2 NumberFormat (java.text.NumberFormat)2 ZonedDateTime (java.time.ZonedDateTime)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2