View Javadoc
1   package fr.ifremer.dali.dao.system.rule;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * %%
7    * Copyright (C) 2017 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import fr.ifremer.dali.service.DaliServiceLocator;
25  import fr.ifremer.quadrige3.core.test.AbstractDaoTest;
26  import fr.ifremer.dali.dao.DaliDatabaseResource;
27  import fr.ifremer.dali.dto.FunctionDTO;
28  import fr.ifremer.dali.dto.configuration.control.ControlRuleDTO;
29  import fr.ifremer.dali.dto.enums.ControlFunctionValues;
30  import org.apache.commons.lang3.builder.ToStringBuilder;
31  import org.apache.commons.lang3.builder.ToStringStyle;
32  import org.apache.commons.lang3.mutable.MutableBoolean;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.junit.Assert;
36  import org.junit.Before;
37  import org.junit.ClassRule;
38  import org.junit.Test;
39  
40  import java.util.List;
41  
42  
43  /**
44   * @author peck7 on 26/10/2017.
45   */
46  public class RuleDaoReadTest extends AbstractDaoTest {
47  
48      private static final Log log = LogFactory.getLog(RuleDaoReadTest.class);
49  
50      @ClassRule
51      public static final DaliDatabaseResource dbResource = DaliDatabaseResource.readDb();
52  
53      private DaliRuleDao ruleDao;
54  
55      @Before
56      @Override
57      public void setUp() throws Exception {
58          super.setUp();
59          ruleDao = DaliServiceLocator.instance().getService("daliRuleDao", DaliRuleDao.class);
60      }
61  
62      @Test
63      public void getAllFunction() {
64  
65          List<FunctionDTO> functions = ruleDao.getAllFunction();
66          Assert.assertNotNull(functions);
67          Assert.assertEquals(5, functions.size());
68          if (log.isDebugEnabled()) {
69              for (FunctionDTO dto : functions) {
70                  log.debug(ToStringBuilder.reflectionToString(dto, ToStringStyle.SHORT_PREFIX_STYLE));
71              }
72          }
73      }
74  
75      @Test
76      public void getRulesByRuleListCode() {
77  
78          // RULELIST1
79          MutableBoolean incompatibleRule = new MutableBoolean();
80          List<ControlRuleDTO> rules = ruleDao.getRulesByRuleListCode(dbResource.getFixtures().getRuleListCode(0), false, incompatibleRule);
81          Assert.assertNotNull(rules);
82          // this rule list is not compatible at all (some attributes comes from old Dali referential)
83          Assert.assertTrue(incompatibleRule.booleanValue());
84          Assert.assertEquals(1, rules.size());
85  
86          // RULELIST2
87          incompatibleRule.setFalse();
88          rules = ruleDao.getRulesByRuleListCode(dbResource.getFixtures().getRuleListCode(1), false, incompatibleRule);
89          Assert.assertNotNull(rules);
90          Assert.assertFalse(incompatibleRule.booleanValue());
91          Assert.assertEquals(0, rules.size());
92          rules = ruleDao.getRulesByRuleListCode(dbResource.getFixtures().getRuleListCode(1), true, incompatibleRule);
93          Assert.assertNotNull(rules);
94          Assert.assertFalse(incompatibleRule.booleanValue());
95          Assert.assertEquals(0, rules.size());
96      }
97  
98      @Test
99      public void getPreconditionedRulesByRuleListCode() {
100 
101         MutableBoolean incompatibleRule = new MutableBoolean();
102         List<ControlRuleDTO> rules = ruleDao.getPreconditionedRulesByRuleListCode(dbResource.getFixtures().getRuleListCode(1), false, incompatibleRule);
103         Assert.assertNotNull(rules);
104         Assert.assertFalse(incompatibleRule.booleanValue());
105         Assert.assertEquals(1, rules.size());
106         ControlRuleDTO preconditionedRule = rules.get(0);
107         Assert.assertEquals("PRECOND1", preconditionedRule.getCode());
108         Assert.assertTrue(ControlFunctionValues.PRECONDITION.equals(preconditionedRule.getFunction()));
109         Assert.assertEquals(2, preconditionedRule.sizePreconditions());
110         Assert.assertEquals(2, preconditionedRule.sizeRulePmfms());
111     }
112 }