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.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.fail;
24  
25  import java.util.UUID;
26  import javax.ws.rs.core.Response;
27  import org.apache.syncope.common.lib.SyncopeClientException;
28  import org.apache.syncope.common.lib.to.ImplementationTO;
29  import org.apache.syncope.common.lib.to.PullTaskTO;
30  import org.apache.syncope.common.lib.types.ClientExceptionType;
31  import org.apache.syncope.common.lib.types.IdMImplementationType;
32  import org.apache.syncope.common.lib.types.ImplementationEngine;
33  import org.apache.syncope.common.lib.types.TaskType;
34  import org.apache.syncope.common.rest.api.RESTHeaders;
35  import org.apache.syncope.fit.AbstractITCase;
36  import org.apache.syncope.fit.core.reference.TestPullActions;
37  import org.junit.jupiter.api.Test;
38  
39  public class ImplementationITCase extends AbstractITCase {
40  
41      @Test
42      public void create() {
43          ImplementationTO implementationTO = new ImplementationTO();
44          implementationTO.setKey(UUID.randomUUID().toString());
45          implementationTO.setEngine(ImplementationEngine.JAVA);
46          implementationTO.setType(IdMImplementationType.PUSH_ACTIONS);
47          implementationTO.setBody(TestPullActions.class.getName());
48  
49          // fail because type is wrong
50          try {
51              IMPLEMENTATION_SERVICE.create(implementationTO);
52              fail("This should not happen");
53          } catch (SyncopeClientException e) {
54              assertEquals(ClientExceptionType.InvalidImplementation, e.getType());
55          }
56          implementationTO.setType(IdMImplementationType.PULL_ACTIONS);
57  
58          Response response = IMPLEMENTATION_SERVICE.create(implementationTO);
59          if (response.getStatusInfo().getStatusCode() != Response.Status.CREATED.getStatusCode()) {
60              Exception ex = CLIENT_FACTORY.getExceptionMapper().fromResponse(response);
61              if (ex != null) {
62                  throw (RuntimeException) ex;
63              }
64          }
65  
66          ImplementationTO actual = IMPLEMENTATION_SERVICE.read(
67                  implementationTO.getType(), response.getHeaderString(RESTHeaders.RESOURCE_KEY));
68          assertNotNull(actual);
69          assertEquals(actual, implementationTO);
70      }
71  
72      @Test
73      public void delete() {
74          ImplementationTO implementationTO = new ImplementationTO();
75          implementationTO.setKey(UUID.randomUUID().toString());
76          implementationTO.setEngine(ImplementationEngine.JAVA);
77          implementationTO.setType(IdMImplementationType.PULL_ACTIONS);
78          implementationTO.setBody(TestPullActions.class.getName());
79  
80          IMPLEMENTATION_SERVICE.create(implementationTO);
81  
82          PullTaskTO pullTask = TASK_SERVICE.read(TaskType.PULL, AbstractTaskITCase.PULL_TASK_KEY, false);
83          assertNotNull(pullTask);
84  
85          int before = pullTask.getActions().size();
86  
87          pullTask.getActions().add(implementationTO.getKey());
88          TASK_SERVICE.update(TaskType.PULL, pullTask);
89  
90          pullTask = TASK_SERVICE.read(TaskType.PULL, AbstractTaskITCase.PULL_TASK_KEY, false);
91          assertNotNull(pullTask);
92  
93          int after = pullTask.getActions().size();
94          assertEquals(before + 1, after);
95  
96          // fails because the implementation is used
97          try {
98              IMPLEMENTATION_SERVICE.delete(implementationTO.getType(), implementationTO.getKey());
99              fail("Unexpected");
100         } catch (SyncopeClientException e) {
101             assertEquals(e.getType(), ClientExceptionType.InUse);
102         }
103 
104         pullTask.getActions().remove(implementationTO.getKey());
105         TASK_SERVICE.update(TaskType.PULL, pullTask);
106 
107         IMPLEMENTATION_SERVICE.delete(implementationTO.getType(), implementationTO.getKey());
108     }
109 
110 }