/*
 * Copyright (C) by Courtanet, All Rights Reserved.
 */
package org.modelmap.example;

import static org.assertj.core.api.Assertions.assertThat;
import static org.modelmap.core.dsl.lang.EValidity.INVALID;
import static org.modelmap.core.dsl.lang.EValidity.VALID;
import static org.modelmap.sample.field.SampleFieldIdInfo.accountId;
import static org.modelmap.sample.field.SampleFieldIdInfo.birthdate;
import static org.modelmap.sample.field.SampleFieldIdInfo.timezone;
import static org.modelmap.sample.model.Timezone.ETC_GMT;

import java.time.LocalDate;

import org.junit.Test;
import org.modelmap.core.FieldModel;
import org.modelmap.core.dsl.DSL;
import org.modelmap.core.dsl.lang.StepValidate;
import org.modelmap.sample.model.SampleModels;

public class DSLSandboxTest {

    private FieldModel model = SampleModels.wrapper();

    @Test
    public void sample1() {
        StepValidate step = DSL.when(accountId().eq(1L))
                        .validate()
                        .withMessage("incorrect account id");
        System.out.println(step.readable());
        assertThat(step.executeOn(model).validity()).isEqualTo(INVALID);
        assertThat(step.executeOn(model).message()).isEqualTo("incorrect account id");
    }

    @Test
    public void sample2() {
        StepValidate step = DSL.when(accountId().eq(1L))
                        .validate();
        System.out.println(step.readable());
        assertThat(step.executeOn(model).validity()).isEqualTo(INVALID);
        assertThat(step.executeOn(model).message()).isEqualTo("account id equals 1");
    }

    @Test
    public void sample3() {
        StepValidate step = DSL.when(accountId().eq(1L).not())
                        .validate()
                        .withMessage("incorrect account id");
        System.out.println(step.readable());
        assertThat(step.executeOn(model).validity()).isEqualTo(VALID);
        assertThat(step.executeOn(model).message()).isNull();
    }

    @Test
    public void sample4() {
        StepValidate step = DSL.when(birthdate().eq(LocalDate.of(1980, 8, 1)))
                        .validate()
                        .withMessage("you can't be born the August 1, 1980");
        System.out.println(step.readable());
        assertThat(step.executeOn(model).validity()).isEqualTo(VALID);
        assertThat(step.executeOn(model).message()).isNull();
    }

    @Test
    public void sample5() {
        StepValidate step = DSL.when(birthdate().between(LocalDate.of(1980, 1, 1), LocalDate.of(1980, 12, 31)))
                        .validate()
                        .withMessage("you can't be born in 1980");
        System.out.println(step.readable());
        assertThat(step.executeOn(model).validity()).isEqualTo(VALID);
        assertThat(step.executeOn(model).message()).isNull();
    }

    @Test
    public void sample6() {
        StepValidate step = DSL.when(birthdate().between(LocalDate.of(1980, 1, 1), LocalDate.of(1980, 12, 31))
                        .and(accountId().notEq(9L))
                        .or(timezone().eq(ETC_GMT)))
                        .validate()
                        .withMessage("you can't be born in 1980 and have an ID different of 9 or having timezone " +
                                        "equals to ETC_GMT");
        System.out.println(step.readable());
        assertThat(step.executeOn(model).validity()).isEqualTo(VALID);
        assertThat(step.executeOn(model).message()).isNull();
    }

}

@@ -79,24 +79,63 @@
 p.example;%0A%0A
+import static java.util.Arrays.asList;%0A
 import stati
@@ -494,24 +494,223 @@
 dIdInfo.
-timezone
+preferencesMail;%0Aimport static org.modelmap.sample.field.SampleFieldIdInfo.timezone;%0Aimport static org.modelmap.sample.model.EmailType.ADMINISTRATOR;%0Aimport static org.modelmap.sample.model.EmailType.PRIVATE
 ;%0Aimport
@@ -789,16 +789,67 @@
 calDate;
+%0Aimport java.util.Arrays;%0Aimport java.util.EnumSet;
 %0A%0Aimport
@@ -980,24 +980,68 @@
 epValidate;%0A
+import org.modelmap.sample.model.EmailType;%0A
 import org.m
@@ -1261,69 +1261,19 @@
 1L))
-%0A                        .validate()%0A                        
+.validate()
 .wit
@@ -1616,33 +1616,8 @@
 1L))
-%0A                        
 .val
@@ -1940,69 +1940,19 @@
 t())
-%0A                        .validate()%0A                        
+.validate()
 .wit
@@ -2290,33 +2290,8 @@
 1)))
-%0A                        
 .val
@@ -2750,33 +2750,8 @@
 te()
-%0A                        
 .wit
@@ -3035,32 +3035,57 @@
 idate step = DSL
+%0A                        
 .when(birthdate(
@@ -3172,16 +3172,32 @@
         
+                
 .and(acc
@@ -3219,33 +3219,8 @@
 9L))
-%0A                        
 .or(
@@ -3398,11 +3398,10 @@
 ne %22
- +
 %0A
+ 
     
@@ -3435,16 +3435,17 @@
         
++
  %22equals
@@ -3644,10 +3644,372 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void sample7() %7B%0A        StepValidate step = DSL.when(preferencesMail().eq(EnumSet.of(ADMINISTRATOR, PRIVATE)))%0A                        .validate()%0A                        .withMessage(%22les pr%C3%A9f%C3%A9rence d'email sont incorrects%22);%0A        System.out.println(step.readable());%0A        assertThat(step.executeOn(model).message()).isNull();%0A    %7D%0A
 %7D%0A

Use SampleFieldInfo new fluent API in sandbox tests