View Javadoc
1   /*
2    * #%L
3    * Nuiton Validator
4    * %%
5    * Copyright (C) 2013 - 2014 Code Lutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package org.nuiton.validator;
23  
24  import org.junit.Assert;
25  import org.nuiton.validator.model.Person;
26  import org.nuiton.validator.model.Pet;
27  
28  import java.io.File;
29  import java.util.List;
30  import java.util.Set;
31  
32  /**
33   * Helper methods to test the validator api.
34   *
35   * @author Tony Chemit - chemit@codelutin.com
36   * @since 2.0
37   */
38  public class ValidatorTestHelper {
39  
40      public static File getBasedir() {
41  
42          // Search basedir from maven environment
43          String basedirPath = System.getenv("basedir");
44          if (basedirPath == null) {
45  
46              // hope the tests are running from the root of the module :)
47              basedirPath = new File("").getAbsolutePath();
48          }
49          return new File(basedirPath);
50      }
51  
52      public static void assertValidatorModel(NuitonValidator<?> validator,
53                                              String expectedContext,
54                                              Class<?> expectedType,
55                                              NuitonValidatorScope... expectedScopes) {
56          Assert.assertNotNull(validator);
57          NuitonValidatorModel<?> model = validator.getModel();
58          Assert.assertNotNull(model);
59          Assert.assertEquals(expectedContext, model.getContext());
60          Assert.assertEquals(expectedType, model.getType());
61          Set<NuitonValidatorScope> scopes = model.getScopes();
62          for (NuitonValidatorScope expectedScope : expectedScopes) {
63              Assert.assertTrue(scopes.contains(expectedScope));
64          }
65      }
66  
67      public static void assertValidatorEffectiveScopes(NuitonValidator<?> validator,
68                                                        NuitonValidatorScope... expectedScopes) {
69          Assert.assertNotNull(validator);
70          Set<NuitonValidatorScope> effectiveScopes = validator.getEffectiveScopes();
71          Assert.assertEquals(expectedScopes.length, effectiveScopes.size());
72          for (NuitonValidatorScope expectedScope : expectedScopes) {
73              Assert.assertTrue(effectiveScopes.contains(expectedScope));
74          }
75      }
76  
77      public static void assertValidatorEffectiveFields(NuitonValidator<?> validator,
78                                                        String... expectedFields) {
79          Assert.assertNotNull(validator);
80          Set<String> effectiveFields = validator.getEffectiveFields();
81          Assert.assertEquals(expectedFields.length, effectiveFields.size());
82          for (String expectedField : expectedFields) {
83              Assert.assertTrue(effectiveFields.contains(expectedField));
84          }
85      }
86  
87      public static void assertValidatorEffectiveFields(NuitonValidator<?> validator,
88                                                        NuitonValidatorScope scope,
89                                                        String... expectedFields) {
90          Assert.assertNotNull(validator);
91          Set<String> effectiveFields = validator.getEffectiveFields(scope);
92          Assert.assertEquals(expectedFields.length, effectiveFields.size());
93          for (String expectedField : expectedFields) {
94              Assert.assertTrue(effectiveFields.contains(expectedField));
95          }
96      }
97  
98      public static void testPerson(NuitonValidator<Person> validator) {
99          Assert.assertNotNull(validator);
100 
101         Person person = new Person();
102 
103         NuitonValidatorResult result;
104 
105         result = validator.validate(person);
106 
107         // two errors : no name, no firstname
108         // one warning : no pet
109         Assert.assertFalse(result.isValid());
110         assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_FIRSTNAME, "person.firstname.required");
111         assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_NAME, "person.name.required");
112         assertFieldMessages(result, NuitonValidatorScope.WARNING, Person.PROPERTY_PET, "person.with.no.pet");
113 
114         person.setFirstname("Joe");
115         result = validator.validate(person);
116 
117         // one error   : no name
118         // one warning : no pet
119         Assert.assertFalse(result.isValid());
120         assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_FIRSTNAME);
121         assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_NAME, "person.name.required");
122         assertFieldMessages(result, NuitonValidatorScope.WARNING, Person.PROPERTY_PET, "person.with.no.pet");
123 
124         person.setName("Black");
125         result = validator.validate(person);
126 
127         // no error
128         // one warning : no pet
129         Assert.assertTrue(result.isValid());
130         assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_FIRSTNAME);
131         assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_NAME);
132         assertFieldMessages(result, NuitonValidatorScope.WARNING, Person.PROPERTY_PET, "person.with.no.pet");
133 
134         person.addPet(new Pet());
135         result = validator.validate(person);
136 
137         // no error
138         // no warning
139         Assert.assertTrue(result.isValid());
140         assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_FIRSTNAME);
141         assertFieldMessages(result, NuitonValidatorScope.ERROR, Person.PROPERTY_NAME);
142         assertFieldMessages(result, NuitonValidatorScope.WARNING, Person.PROPERTY_PET);
143 
144     }
145 
146     public static void assertFieldMessages(NuitonValidatorResult result,
147                                            NuitonValidatorScope scope,
148                                            String field,
149                                            String... expectedMessages) {
150 
151         if (expectedMessages.length == 0) {
152 
153             // no messages
154             boolean hasMessages = result.hasMessagesForScope(field, scope);
155             Assert.assertFalse(hasMessages);
156 
157         } else {
158 
159             // with messages
160             boolean hasMessages = result.hasMessagesForScope(field, scope);
161             Assert.assertTrue(hasMessages);
162 
163             List<String> messages = result.getMessagesForScope(field, scope);
164             Assert.assertNotNull(hasMessages);
165             Assert.assertEquals(expectedMessages.length, messages.size());
166             for (String expectedMessage : expectedMessages) {
167                 Assert.assertTrue(messages.contains(expectedMessage));
168             }
169         }
170     }
171 }