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.sra;
20  
21  import com.fasterxml.jackson.databind.json.JsonMapper;
22  import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
23  import java.util.Comparator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.ConcurrentHashMap;
27  import java.util.stream.Collectors;
28  import javax.ws.rs.NotFoundException;
29  import javax.ws.rs.core.Response;
30  import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
31  import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
32  import org.apache.syncope.common.keymaster.client.api.ServiceOps;
33  import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
34  import org.apache.syncope.common.lib.to.SRARouteTO;
35  import org.apache.syncope.common.rest.api.service.SRARouteService;
36  import org.springframework.beans.factory.annotation.Autowired;
37  import org.springframework.context.ApplicationListener;
38  import org.springframework.context.event.ContextRefreshedEvent;
39  import org.springframework.stereotype.Component;
40  
41  @Component
42  public class SyncopeCoreTestingServer implements ApplicationListener<ContextRefreshedEvent> {
43  
44      private static final int PORT = 9999;
45  
46      public static final String ADDRESS = "http://localhost:" + PORT + "/syncope/rest";
47  
48      public static final Map<String, SRARouteTO> ROUTES = new ConcurrentHashMap<>();
49  
50      @Autowired
51      private RouteRefresher routeRefresher;
52  
53      @Autowired
54      private ServiceOps serviceOps;
55  
56      @Override
57      public void onApplicationEvent(final ContextRefreshedEvent event) {
58          if (AbstractTest.available(PORT)) {
59              // 1. start (mocked) Core as embedded CXF
60              JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
61              sf.setAddress(ADDRESS);
62              sf.setResourceClasses(SRARouteService.class);
63              sf.setResourceProvider(SRARouteService.class,
64                      new SingletonResourceProvider(new StubSRARouteService(), true));
65              sf.setProviders(List.of(new JacksonJsonProvider(JsonMapper.builder().findAndAddModules().build())));
66              sf.create();
67  
68              // 2. register Core in Keymaster
69              NetworkService core = new NetworkService();
70              core.setType(NetworkService.Type.CORE);
71              core.setAddress(ADDRESS);
72              serviceOps.register(core);
73          }
74      }
75  
76      public class StubSRARouteService implements SRARouteService {
77  
78          @Override
79          public List<SRARouteTO> list() {
80              return ROUTES.values().stream().
81                      sorted(Comparator.comparing(SRARouteTO::getKey)).
82                      collect(Collectors.toList());
83          }
84  
85          @Override
86          public Response create(final SRARouteTO routeTO) {
87              ROUTES.putIfAbsent(routeTO.getKey(), routeTO);
88              return Response.noContent().build();
89          }
90  
91          @Override
92          public SRARouteTO read(final String key) {
93              SRARouteTO route = ROUTES.get(key);
94              if (route == null) {
95                  throw new NotFoundException();
96              }
97              return route;
98          }
99  
100         @Override
101         public void update(final SRARouteTO routeTO) {
102             read(routeTO.getKey());
103             ROUTES.put(routeTO.getKey(), routeTO);
104         }
105 
106         @Override
107         public void delete(final String key) {
108             ROUTES.remove(key);
109         }
110 
111         @Override
112         public void pushToSRA() {
113             routeRefresher.refresh();
114         }
115     }
116 }