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.reference;
20  
21  import java.util.Optional;
22  import org.apache.syncope.common.lib.Attr;
23  import org.apache.syncope.common.lib.request.AnyObjectCR;
24  import org.apache.syncope.common.lib.to.AnyObjectTO;
25  import org.apache.syncope.common.lib.to.RealmTO;
26  import org.apache.syncope.core.logic.AnyObjectLogic;
27  import org.apache.syncope.core.logic.RealmLogic;
28  import org.apache.syncope.core.provisioning.api.macro.Command;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.transaction.annotation.Propagation;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  public class TestCommand implements Command<TestCommandArgs> {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(TestCommand.class);
38  
39      @Autowired
40      private RealmLogic realmLogic;
41  
42      @Autowired
43      private AnyObjectLogic anyObjectLogic;
44  
45      private Optional<RealmTO> getRealm(final String fullPath) {
46          return realmLogic.search(null, fullPath, -1, -1).getRight().stream().
47                  filter(realm -> fullPath.equals(realm.getFullPath())).findFirst();
48      }
49  
50      @Transactional(propagation = Propagation.NOT_SUPPORTED)
51      @Override
52      public String run(final TestCommandArgs args) {
53          // 1. create new Realm
54          RealmTO realm = new RealmTO();
55          realm.setName(args.getRealmName());
56          realm.setParent(getRealm(args.getParentRealm()).map(RealmTO::getKey).orElse(null));
57          realm = realmLogic.create(args.getParentRealm(), realm).getEntity();
58          LOG.info("Realm created: {}", realm.getFullPath());
59  
60          // 2. create new PRINTER
61          AnyObjectTO anyObject = anyObjectLogic.create(
62                  new AnyObjectCR.Builder(realm.getFullPath(), "PRINTER", args.getPrinterName()).
63                          plainAttr(new Attr.Builder("location").value("location").build()).
64                          build(),
65                  false).getEntity();
66          LOG.info("PRINTER created: {}", anyObject.getName());
67  
68          return "Realm created: " + realm.getFullPath() + "; PRINTER created: " + anyObject.getName();
69      }
70  }