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);
}
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);
}
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);
});
}
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);
}
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);
}
Aggregations