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.core.persistence.jpa.inner;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  
25  import java.net.URI;
26  import java.util.List;
27  import java.util.UUID;
28  import javax.ws.rs.HttpMethod;
29  import org.apache.syncope.common.lib.types.SRARouteFilter;
30  import org.apache.syncope.common.lib.types.SRARouteFilterFactory;
31  import org.apache.syncope.common.lib.types.SRARoutePredicate;
32  import org.apache.syncope.common.lib.types.SRARoutePredicateFactory;
33  import org.apache.syncope.common.lib.types.SRARouteType;
34  import org.apache.syncope.core.persistence.api.dao.SRARouteDAO;
35  import org.apache.syncope.core.persistence.api.entity.SRARoute;
36  import org.apache.syncope.core.persistence.jpa.AbstractTest;
37  import org.junit.jupiter.api.Test;
38  import org.springframework.beans.factory.annotation.Autowired;
39  import org.springframework.transaction.annotation.Transactional;
40  
41  @Transactional("Master")
42  public class SRARouteTest extends AbstractTest {
43  
44      @Autowired
45      private SRARouteDAO routeDAO;
46  
47      @Test
48      public void find() {
49          SRARoute route = routeDAO.find("ec7bada2-3dd6-460c-8441-65521d005ffa");
50          assertNotNull(route);
51          assertEquals(1, route.getPredicates().size());
52  
53          route = routeDAO.find(UUID.randomUUID().toString());
54          assertNull(route);
55      }
56  
57      @Test
58      public void findAll() {
59          List<SRARoute> routes = routeDAO.findAll();
60          assertNotNull(routes);
61          assertEquals(1, routes.size());
62      }
63  
64      @Test
65      public void save() {
66          SRARoute route = entityFactory.newEntity(SRARoute.class);
67          route.setName("just for test");
68          route.setTarget(URI.create("http://localhost:80"));
69          route.setType(SRARouteType.PUBLIC);
70          route.setPredicates(List.of(new SRARoutePredicate.Builder().
71                  factory(SRARoutePredicateFactory.METHOD).args(HttpMethod.GET).build()));
72          route.setFilters(List.of(new SRARouteFilter.Builder().
73                  factory(SRARouteFilterFactory.ADD_REQUEST_HEADER).args("X-Request-Foo, Bar").build()));
74  
75          int beforeCount = routeDAO.findAll().size();
76  
77          route = routeDAO.save(route);
78          assertNotNull(route);
79          assertNotNull(route.getKey());
80  
81          int afterCount = routeDAO.findAll().size();
82          assertEquals(afterCount, beforeCount + 1);
83      }
84  
85      @Test
86      public void delete() {
87          SRARoute route = routeDAO.find("ec7bada2-3dd6-460c-8441-65521d005ffa");
88          assertNotNull(route);
89  
90          routeDAO.delete("ec7bada2-3dd6-460c-8441-65521d005ffa");
91  
92          route = routeDAO.find("ec7bada2-3dd6-460c-8441-65521d005ffa");
93          assertNull(route);
94      }
95  }