1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service.impl;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.kuali.rice.core.api.criteria.AndPredicate;
22 import org.kuali.rice.core.api.criteria.LikePredicate;
23 import org.kuali.rice.core.api.criteria.OrPredicate;
24 import org.kuali.rice.core.api.criteria.Predicate;
25 import org.kuali.rice.core.api.criteria.QueryByCriteria;
26 import org.kuali.rice.krad.data.DataObjectService;
27 import org.kuali.rice.krad.data.DataObjectWrapper;
28 import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
29 import org.kuali.rice.krad.data.provider.impl.DataObjectWrapperBase;
30 import org.kuali.rice.krad.datadictionary.DataDictionary;
31 import org.kuali.rice.krad.service.DataDictionaryService;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.invocation.InvocationOnMock;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.mockito.stubbing.Answer;
37
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.Set;
41
42 import static org.junit.Assert.*;
43 import static org.mockito.Matchers.any;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46
47
48
49
50
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class LookupCriteriaGeneratorImplTest {
54
55 @Mock DataDictionary dataDictionary;
56 @Mock DataDictionaryService dataDictionaryService;
57 @Mock DataObjectService dataObjectService;
58
59 @InjectMocks private LookupCriteriaGeneratorImpl generator = new LookupCriteriaGeneratorImpl();
60
61 @Before
62 public void setUp() throws Exception {
63 when(dataDictionaryService.getDataDictionary()).thenReturn(dataDictionary);
64 when(dataObjectService.wrap(any(TestClass.class))).thenAnswer(new Answer<DataObjectWrapper<TestClass>>() {
65 @Override
66 public DataObjectWrapper<TestClass> answer(InvocationOnMock invocation) throws Throwable {
67 return new DataObjectWrapperImpl<TestClass>((TestClass)invocation.getArguments()[0], mock(DataObjectMetadata.class), dataObjectService);
68 }
69 });
70
71 }
72
73 @Test
74 public void testGenerateCriteria_MultipleOr() throws Exception {
75 Map<String, String> mapCriteria = new HashMap<String, String>();
76 mapCriteria.put("prop1", "a|b");
77 mapCriteria.put("prop2", "c");
78 mapCriteria.put("prop3", "d");
79
80 QueryByCriteria.Builder qbcBuilder = generator.generateCriteria(TestClass.class, mapCriteria, false);
81 assertNotNull(qbcBuilder);
82 QueryByCriteria qbc = qbcBuilder.build();
83
84
85
86
87
88
89
90
91
92
93
94 Predicate and = qbc.getPredicate();
95 assertTrue(and instanceof AndPredicate);
96 Set<Predicate> predicates = ((AndPredicate) and).getPredicates();
97
98 assertEquals(3, predicates.size());
99
100 boolean foundProp1 = false;
101 boolean foundProp2 = false;
102 boolean foundProp3 = false;
103 for (Predicate predicate : predicates) {
104 if (predicate instanceof LikePredicate) {
105 LikePredicate like = (LikePredicate)predicate;
106 if (like.getPropertyPath().equals("prop2")) {
107 assertEquals("c", like.getValue().getValue());
108 foundProp2 = true;
109 } else if (like.getPropertyPath().equals("prop3")) {
110 assertEquals("d", like.getValue().getValue());
111 foundProp3 = true;
112 } else {
113 fail("Invalid like predicate encountered.");
114 }
115 } else if (predicate instanceof OrPredicate) {
116 foundProp1 = true;
117
118 OrPredicate orPredicate = (OrPredicate)predicate;
119 assertEquals(2, orPredicate.getPredicates().size());
120 for (Predicate orSubPredicate : orPredicate.getPredicates()) {
121 if (orSubPredicate instanceof LikePredicate) {
122 LikePredicate likeInternal = (LikePredicate)orSubPredicate;
123 if (likeInternal.getPropertyPath().equals("prop1")) {
124 assertTrue("a".equals(likeInternal.getValue().getValue()) ||
125 "b".equals(likeInternal.getValue().getValue()));
126 } else {
127 fail("Invalid predicate, does not have a propertypath of prop1");
128 }
129 } else {
130 fail("Invalid predicate: " + orSubPredicate);
131 }
132 }
133 } else {
134 fail("Invalid predicate: " + predicate);
135 }
136 }
137 assertTrue(foundProp1);
138 assertTrue(foundProp2);
139 assertTrue(foundProp3);
140
141
142 }
143
144 public static final class TestClass {
145
146 private String prop1;
147 private String prop2;
148 private String prop3;
149
150 public String getProp1() {
151 return prop1;
152 }
153
154 public void setProp1(String prop1) {
155 this.prop1 = prop1;
156 }
157
158 public String getProp2() {
159 return prop2;
160 }
161
162 public void setProp2(String prop2) {
163 this.prop2 = prop2;
164 }
165
166 public String getProp3() {
167 return prop3;
168 }
169
170 public void setProp3(String prop3) {
171 this.prop3 = prop3;
172 }
173 }
174
175 private static final class DataObjectWrapperImpl<T> extends DataObjectWrapperBase<T> {
176 private DataObjectWrapperImpl(T dataObject, DataObjectMetadata metadata, DataObjectService dataObjectService) {
177 super(dataObject, metadata, dataObjectService);
178 }
179 }
180
181 }