View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.core;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  import static org.junit.jupiter.api.Assertions.fail;
26  
27  import java.util.List;
28  import javax.ws.rs.core.Response;
29  import org.apache.syncope.common.lib.SyncopeClientException;
30  import org.apache.syncope.common.lib.to.FIQLQueryTO;
31  import org.apache.syncope.common.lib.types.AnyTypeKind;
32  import org.apache.syncope.common.lib.types.ClientExceptionType;
33  import org.apache.syncope.common.rest.api.RESTHeaders;
34  import org.apache.syncope.common.rest.api.service.FIQLQueryService;
35  import org.apache.syncope.fit.AbstractITCase;
36  import org.junit.jupiter.api.Test;
37  
38  public class FIQLQueryITCase extends AbstractITCase {
39  
40      @Test
41      public void crud() {
42          FIQLQueryService fiqlQueryService =
43                  CLIENT_FACTORY.create("bellini", "password").getService(FIQLQueryService.class);
44  
45          int before = fiqlQueryService.list(AnyTypeKind.USER.name()).size();
46  
47          FIQLQueryTO query = new FIQLQueryTO();
48          query.setFiql("INVALID");
49          query.setName("fancy name");
50          query.setTarget(AnyTypeKind.USER.name());
51  
52          try {
53              fiqlQueryService.create(query);
54              fail();
55          } catch (SyncopeClientException e) {
56              assertEquals(ClientExceptionType.InvalidSearchParameters, e.getType());
57          }
58  
59          query.setFiql("username=~*one*");
60          Response response = fiqlQueryService.create(query);
61          String key = response.getHeaderString(RESTHeaders.RESOURCE_KEY);
62          assertNotNull(key);
63  
64          query = fiqlQueryService.read(key);
65          assertNotNull(query.getKey());
66          assertEquals("username=~*one*", query.getFiql());
67          assertEquals("fancy name", query.getName());
68  
69          List<FIQLQueryTO> queries = fiqlQueryService.list(AnyTypeKind.USER.name());
70          assertEquals(before + 1, queries.size());
71          assertTrue(queries.stream().anyMatch(q -> key.equals(q.getKey())));
72  
73          query.setFiql("email==ciao@bao.it");
74          query.setName("not so fancy");
75          fiqlQueryService.update(query);
76  
77          query = fiqlQueryService.read(key);
78          assertEquals("email==ciao@bao.it", query.getFiql());
79          assertEquals("not so fancy", query.getName());
80  
81          fiqlQueryService.delete(key);
82  
83          queries = fiqlQueryService.list(AnyTypeKind.USER.name());
84          assertEquals(before, queries.size());
85          assertFalse(queries.contains(query));
86      }
87  }