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.fail;
25  
26  import java.net.URI;
27  import java.util.List;
28  import java.util.UUID;
29  import javax.ws.rs.HttpMethod;
30  import javax.ws.rs.core.Response;
31  import org.apache.syncope.common.lib.SyncopeClientException;
32  import org.apache.syncope.common.lib.to.SRARouteTO;
33  import org.apache.syncope.common.lib.types.ClientExceptionType;
34  import org.apache.syncope.common.lib.types.SRARouteFilter;
35  import org.apache.syncope.common.lib.types.SRARouteFilterFactory;
36  import org.apache.syncope.common.lib.types.SRARoutePredicate;
37  import org.apache.syncope.common.lib.types.SRARoutePredicateFactory;
38  import org.apache.syncope.common.rest.api.service.SRARouteService;
39  import org.apache.syncope.fit.AbstractITCase;
40  import org.junit.jupiter.api.Test;
41  
42  public class SRARouteITCase extends AbstractITCase {
43  
44      @Test
45      public void read() {
46          SRARouteTO route = SRA_ROUTE_SERVICE.read("ec7bada2-3dd6-460c-8441-65521d005ffa");
47          assertNotNull(route);
48          assertEquals(1, route.getPredicates().size());
49  
50          try {
51              SRA_ROUTE_SERVICE.read(UUID.randomUUID().toString());
52              fail();
53          } catch (SyncopeClientException e) {
54              assertEquals(ClientExceptionType.NotFound, e.getType());
55          }
56      }
57  
58      @Test
59      public void findAll() {
60          List<SRARouteTO> routes = SRA_ROUTE_SERVICE.list();
61          assertNotNull(routes);
62          assertFalse(routes.isEmpty());
63      }
64  
65      @Test
66      public void createUpdateDelete() {
67          SRARouteTO route = new SRARouteTO();
68          route.setName("just for test");
69          route.setTarget(URI.create("http://localhost:80"));
70          route.getPredicates().add(new SRARoutePredicate.Builder().
71                  factory(SRARoutePredicateFactory.METHOD).args(HttpMethod.GET).build());
72          route.getFilters().add(new SRARouteFilter.Builder().
73                  factory(SRARouteFilterFactory.ADD_REQUEST_HEADER).args("X-Request-Foo, Bar").build());
74  
75          int beforeCount = SRA_ROUTE_SERVICE.list().size();
76  
77          Response response = SRA_ROUTE_SERVICE.create(route);
78          assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
79          route = getObject(response.getLocation(), SRARouteService.class, SRARouteTO.class);
80          assertNotNull(route);
81          assertNotNull(route.getKey());
82  
83          int afterCount = SRA_ROUTE_SERVICE.list().size();
84          assertEquals(afterCount, beforeCount + 1);
85  
86          SRA_ROUTE_SERVICE.delete(route.getKey());
87  
88          try {
89              SRA_ROUTE_SERVICE.read(route.getKey());
90              fail();
91          } catch (SyncopeClientException e) {
92              assertEquals(ClientExceptionType.NotFound, e.getType());
93          }
94  
95          int endCount = SRA_ROUTE_SERVICE.list().size();
96          assertEquals(endCount, beforeCount);
97      }
98  
99      @Test
100     public void exceptions() {
101         SRARouteTO route = new SRARouteTO();
102         try {
103             SRA_ROUTE_SERVICE.create(route);
104             fail();
105         } catch (SyncopeClientException e) {
106             assertEquals(ClientExceptionType.RequiredValuesMissing, e.getType());
107         }
108 
109         route.setName("createException");
110         try {
111             SRA_ROUTE_SERVICE.create(route);
112             fail();
113         } catch (SyncopeClientException e) {
114             assertEquals(ClientExceptionType.RequiredValuesMissing, e.getType());
115         }
116 
117         route.setTarget(URI.create("http://localhost:80"));
118         Response response = SRA_ROUTE_SERVICE.create(route);
119         assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatusInfo().getStatusCode());
120 
121         try {
122             SRA_ROUTE_SERVICE.create(route);
123             fail();
124         } catch (SyncopeClientException e) {
125             assertEquals(ClientExceptionType.EntityExists, e.getType());
126         }
127 
128         route.setKey(UUID.randomUUID().toString());
129         try {
130             SRA_ROUTE_SERVICE.update(route);
131             fail();
132         } catch (SyncopeClientException e) {
133             assertEquals(ClientExceptionType.NotFound, e.getType());
134         }
135         try {
136             SRA_ROUTE_SERVICE.delete(route.getKey());
137             fail();
138         } catch (SyncopeClientException e) {
139             assertEquals(ClientExceptionType.NotFound, e.getType());
140         }
141     }
142 }