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 javax.ws.rs.core.Response;
28  import org.apache.syncope.common.lib.SyncopeClientException;
29  import org.apache.syncope.common.lib.command.CommandOutput;
30  import org.apache.syncope.common.lib.command.CommandTO;
31  import org.apache.syncope.common.lib.to.AnyObjectTO;
32  import org.apache.syncope.common.lib.to.ImplementationTO;
33  import org.apache.syncope.common.lib.to.PagedResult;
34  import org.apache.syncope.common.lib.types.ClientExceptionType;
35  import org.apache.syncope.common.lib.types.IdRepoImplementationType;
36  import org.apache.syncope.common.lib.types.ImplementationEngine;
37  import org.apache.syncope.common.rest.api.RESTHeaders;
38  import org.apache.syncope.common.rest.api.beans.CommandQuery;
39  import org.apache.syncope.common.rest.api.beans.RealmQuery;
40  import org.apache.syncope.fit.AbstractITCase;
41  import org.apache.syncope.fit.core.reference.TestCommand;
42  import org.apache.syncope.fit.core.reference.TestCommandArgs;
43  import org.junit.jupiter.api.BeforeAll;
44  import org.junit.jupiter.api.Test;
45  
46  public class CommandITCase extends AbstractITCase {
47  
48      @BeforeAll
49      public static void testCommandSetup() {
50          ImplementationTO command = null;
51          try {
52              command = IMPLEMENTATION_SERVICE.read(
53                      IdRepoImplementationType.COMMAND, TestCommand.class.getSimpleName());
54          } catch (SyncopeClientException e) {
55              if (e.getType().getResponseStatus() == Response.Status.NOT_FOUND) {
56                  command = new ImplementationTO();
57                  command.setKey(TestCommand.class.getSimpleName());
58                  command.setEngine(ImplementationEngine.JAVA);
59                  command.setType(IdRepoImplementationType.COMMAND);
60                  command.setBody(TestCommand.class.getName());
61                  Response response = IMPLEMENTATION_SERVICE.create(command);
62                  command = IMPLEMENTATION_SERVICE.read(
63                          command.getType(), response.getHeaderString(RESTHeaders.RESOURCE_KEY));
64                  assertNotNull(command);
65              }
66          }
67          assertNotNull(command);
68      }
69  
70      @Test
71      public void listCommands() {
72          PagedResult<CommandTO> commands = COMMAND_SERVICE.search(new CommandQuery.Builder().page(1).size(100).build());
73          assertEquals(1, commands.getTotalCount());
74          assertEquals(1, commands.getResult().size());
75  
76          CommandTO command = commands.getResult().get(0);
77          assertNotNull(command);
78          assertEquals(TestCommand.class.getSimpleName(), command.getKey());
79          assertTrue(command.getArgs() instanceof TestCommandArgs);
80      }
81  
82      @Test
83      public void argsValidationFailure() {
84          CommandTO command = COMMAND_SERVICE.search(new CommandQuery.Builder().
85                  keyword(TestCommand.class.getSimpleName()).page(1).size(1).build()).getResult().get(0);
86  
87          try {
88              COMMAND_SERVICE.run(command);
89              fail();
90          } catch (SyncopeClientException e) {
91              assertEquals(ClientExceptionType.InvalidValues, e.getType());
92          }
93      }
94  
95      @Test
96      public void runCommand() {
97          CommandTO command = COMMAND_SERVICE.search(
98                  new CommandQuery.Builder().page(1).size(1).build()).getResult().get(0);
99          TestCommandArgs args = ((TestCommandArgs) command.getArgs());
100         args.setParentRealm("/even/two");
101         args.setRealmName("realm123");
102         args.setPrinterName("printer124");
103 
104         CommandOutput output = COMMAND_SERVICE.run(command);
105         assertNotNull(output);
106 
107         AnyObjectTO printer = null;
108         try {
109             printer = ANY_OBJECT_SERVICE.read(PRINTER, args.getPrinterName());
110             assertNotNull(printer);
111             assertEquals(args.getParentRealm() + "/" + args.getRealmName(), printer.getRealm());
112             assertFalse(REALM_SERVICE.search(
113                     new RealmQuery.Builder().base(printer.getRealm()).build()).getResult().isEmpty());
114         } finally {
115             if (printer != null) {
116                 ANY_OBJECT_SERVICE.delete(printer.getKey());
117             }
118             REALM_SERVICE.delete(args.getParentRealm() + "/" + args.getRealmName());
119         }
120     }
121 }