View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.model;
12  
13  import com.codebox.bean.JavaBeanTester;
14  
15  import java.util.List;
16  
17  import org.junit.jupiter.api.Assertions;
18  import org.junit.jupiter.api.Test;
19  
20  /**
21   * The Class SessionSearchInfoTest.
22   */
23  class SessionSearchInfoTest {
24  
25    /**
26     * Javabean tester.
27     */
28    @Test
29    void javabeanTester() {
30      JavaBeanTester.builder(SessionSearchInfo.class).loadData().skipStrictSerializable().test();
31    }
32  
33    /**
34     * Session search flags and defaults test.
35     */
36    @Test
37    void sessionSearchDefaultsAndFlagsTest() {
38      SessionSearchInfo info = new SessionSearchInfo();
39  
40      Assertions.assertEquals(SessionSearchInfo.ACTION_NONE, info.getSearchAction());
41      Assertions.assertTrue(info.isEmpty());
42      Assertions.assertTrue(info.isValid());
43      Assertions.assertFalse(info.isApply());
44      Assertions.assertFalse(info.isClear());
45      Assertions.assertFalse(info.isUseSearch());
46      Assertions.assertFalse(info.isUseAttr());
47      Assertions.assertFalse(info.isUseSessionId());
48      Assertions.assertFalse(info.isUseAttrName());
49      Assertions.assertFalse(info.isUseAgeFrom());
50      Assertions.assertFalse(info.isUseAgeTo());
51      Assertions.assertFalse(info.isUseIdleTimeFrom());
52      Assertions.assertFalse(info.isUseIdleTimeTo());
53      Assertions.assertFalse(info.isUseLastIp());
54  
55      info.setSearchAction(SessionSearchInfo.ACTION_APPLY);
56      info.setSessionId("abc");
57      Assertions.assertTrue(info.isApply());
58      Assertions.assertFalse(info.isClear());
59      Assertions.assertTrue(info.isUseSearch());
60      Assertions.assertTrue(info.isUseSessionId());
61  
62      info.setSearchAction(SessionSearchInfo.ACTION_CLEAR);
63      Assertions.assertFalse(info.isApply());
64      Assertions.assertTrue(info.isClear());
65      Assertions.assertFalse(info.isUseSearch());
66  
67      info.setSearchAction(null);
68      Assertions.assertFalse(info.isApply());
69      Assertions.assertFalse(info.isClear());
70    }
71  
72    /**
73     * Session id and attribute pattern validation test.
74     */
75    @Test
76    void patternValidationTest() {
77      SessionSearchInfo info = new SessionSearchInfo();
78  
79      info.setSessionId("test.*");
80      Assertions.assertNull(info.getSessionIdMsg());
81      Assertions.assertNotNull(info.getSessionIdPattern());
82      Assertions.assertTrue(info.isSessionIdValid());
83  
84      info.setSessionId("(");
85      Assertions.assertNull(info.getSessionIdPattern());
86      Assertions.assertNotNull(info.getSessionIdMsg());
87      Assertions.assertFalse(info.isSessionIdValid());
88      Assertions.assertFalse(info.isUseSessionId());
89  
90      info.setAttrName("user.*,token");
91      Assertions.assertTrue(info.isAttrNameValid());
92      Assertions.assertTrue(info.isUseAttrName());
93      Assertions.assertEquals(2, info.getAttrNamePatterns().size());
94      Assertions.assertTrue(info.getAttrNameMsgs().isEmpty());
95  
96      info.setAttrName("(");
97      Assertions.assertFalse(info.isAttrNameValid());
98      Assertions.assertFalse(info.isUseAttrName());
99      Assertions.assertEquals(1, info.getAttrNameMsgs().size());
100   }
101 
102   /**
103    * Numeric and list behavior test.
104    */
105   @Test
106   void numericAndDefensiveListBehaviorTest() {
107     SessionSearchInfo info = new SessionSearchInfo();
108 
109     info.setAgeFrom("10");
110     info.setAgeTo("20");
111     info.setIdleTimeFrom("30");
112     info.setIdleTimeTo("40");
113     info.setLastIp("127.0.0.1");
114     info.setInfoMessage("ok");
115 
116     Assertions.assertTrue(info.isAgeFromValid());
117     Assertions.assertTrue(info.isAgeToValid());
118     Assertions.assertTrue(info.isIdleTimeFromValid());
119     Assertions.assertTrue(info.isIdleTimeToValid());
120     Assertions.assertTrue(info.isUseAgeFrom());
121     Assertions.assertTrue(info.isUseAgeTo());
122     Assertions.assertTrue(info.isUseIdleTimeFrom());
123     Assertions.assertTrue(info.isUseIdleTimeTo());
124     Assertions.assertTrue(info.isUseLastIp());
125     Assertions.assertEquals(Integer.valueOf(10), info.getAgeFromSec());
126     Assertions.assertEquals(Integer.valueOf(20), info.getAgeToSec());
127     Assertions.assertEquals(Integer.valueOf(30), info.getIdleTimeFromSec());
128     Assertions.assertEquals(Integer.valueOf(40), info.getIdleTimeToSec());
129     Assertions.assertEquals("ok", info.getInfoMessage());
130 
131     info.setAgeFrom("x");
132     info.setAgeTo("x");
133     info.setIdleTimeFrom("x");
134     info.setIdleTimeTo("x");
135     Assertions.assertFalse(info.isAgeFromValid());
136     Assertions.assertFalse(info.isAgeToValid());
137     Assertions.assertFalse(info.isIdleTimeFromValid());
138     Assertions.assertFalse(info.isIdleTimeToValid());
139     Assertions.assertFalse(info.isValid());
140 
141     info.addErrorMessage("err");
142     List<String> messages = info.getErrorMessages();
143     Assertions.assertEquals(1, messages.size());
144     messages.clear();
145     Assertions.assertEquals(1, info.getErrorMessages().size());
146   }
147 
148   /**
149    * Empty and valid branch coverage test.
150    */
151   @Test
152   void emptyAndValidBranchCoverageTest() {
153     SessionSearchInfo withSessionId = new SessionSearchInfo();
154     withSessionId.setSessionId("x");
155     Assertions.assertFalse(withSessionId.isEmpty());
156 
157     SessionSearchInfo withAttrName = new SessionSearchInfo();
158     withAttrName.setAttrName("x");
159     Assertions.assertFalse(withAttrName.isEmpty());
160 
161     SessionSearchInfo withAgeFrom = new SessionSearchInfo();
162     withAgeFrom.setAgeFrom("1");
163     Assertions.assertFalse(withAgeFrom.isEmpty());
164 
165     SessionSearchInfo withAgeTo = new SessionSearchInfo();
166     withAgeTo.setAgeTo("1");
167     Assertions.assertFalse(withAgeTo.isEmpty());
168 
169     SessionSearchInfo withIdleFrom = new SessionSearchInfo();
170     withIdleFrom.setIdleTimeFrom("1");
171     Assertions.assertFalse(withIdleFrom.isEmpty());
172 
173     SessionSearchInfo withIdleTo = new SessionSearchInfo();
174     withIdleTo.setIdleTimeTo("1");
175     Assertions.assertFalse(withIdleTo.isEmpty());
176 
177     SessionSearchInfo withLastIp = new SessionSearchInfo();
178     withLastIp.setLastIp("127.0.0.1");
179     Assertions.assertFalse(withLastIp.isEmpty());
180 
181     SessionSearchInfo invalidSession = new SessionSearchInfo();
182     invalidSession.setSessionId("(");
183     Assertions.assertFalse(invalidSession.isValid());
184 
185     SessionSearchInfo invalidAttr = new SessionSearchInfo();
186     invalidAttr.setAttrName("(");
187     Assertions.assertFalse(invalidAttr.isValid());
188 
189     SessionSearchInfo invalidAgeFrom = new SessionSearchInfo();
190     invalidAgeFrom.setAgeFrom("x");
191     Assertions.assertFalse(invalidAgeFrom.isValid());
192 
193     SessionSearchInfo invalidAgeTo = new SessionSearchInfo();
194     invalidAgeTo.setAgeTo("x");
195     Assertions.assertFalse(invalidAgeTo.isValid());
196 
197     SessionSearchInfo invalidIdleFrom = new SessionSearchInfo();
198     invalidIdleFrom.setIdleTimeFrom("x");
199     Assertions.assertFalse(invalidIdleFrom.isValid());
200 
201     SessionSearchInfo invalidIdleTo = new SessionSearchInfo();
202     invalidIdleTo.setIdleTimeTo("x");
203     Assertions.assertFalse(invalidIdleTo.isValid());
204 
205     SessionSearchInfo info = new SessionSearchInfo();
206     info.setSearchAction(SessionSearchInfo.ACTION_APPLY);
207     info.setAttrName("user");
208     Assertions.assertTrue(info.isUseAttr());
209     info.setAttrName(null);
210     info.setSessionId(null);
211     Assertions.assertFalse(info.isUseAttr());
212   }
213 
214 }