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.core.logic;
20  
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.when;
26  
27  import java.io.ByteArrayOutputStream;
28  import java.io.IOException;
29  import java.io.OutputStream;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.stream.Collectors;
33  import org.apache.syncope.common.lib.SyncopeConstants;
34  import org.apache.syncope.common.lib.to.ReportTO;
35  import org.apache.syncope.common.lib.types.IdMEntitlement;
36  import org.apache.syncope.core.provisioning.api.job.JobManager;
37  import org.apache.syncope.core.provisioning.api.job.report.ReportJobDelegate;
38  import org.apache.syncope.core.provisioning.java.job.report.AbstractReportJobDelegate;
39  import org.apache.syncope.core.spring.ApplicationContextProvider;
40  import org.apache.syncope.core.spring.security.SyncopeAuthenticationDetails;
41  import org.apache.syncope.core.spring.security.SyncopeGrantedAuthority;
42  import org.junit.jupiter.api.AfterAll;
43  import org.junit.jupiter.api.BeforeAll;
44  import org.junit.jupiter.api.Test;
45  import org.quartz.JobDataMap;
46  import org.quartz.JobExecutionContext;
47  import org.quartz.JobExecutionException;
48  import org.springframework.beans.factory.annotation.Autowired;
49  import org.springframework.beans.factory.support.AbstractBeanDefinition;
50  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
51  import org.springframework.security.core.GrantedAuthority;
52  import org.springframework.security.core.context.SecurityContextHolder;
53  import org.springframework.transaction.annotation.Transactional;
54  
55  @Transactional("Master")
56  public class ReportLogicTest extends AbstractTest {
57  
58      public static class TestReportJobDelegate extends AbstractReportJobDelegate {
59  
60          @Override
61          protected String doExecute(
62                  final boolean dryRun,
63                  final OutputStream os,
64                  final String executor,
65                  final JobExecutionContext context) throws JobExecutionException {
66  
67              try {
68                  os.write("test".getBytes());
69              } catch (IOException e) {
70                  throw new JobExecutionException(e);
71              }
72  
73              return "";
74          }
75      }
76  
77      @BeforeAll
78      public static void setAuthContext() {
79          List<GrantedAuthority> authorities = IdMEntitlement.values().stream().
80                  map(entitlement -> new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM)).
81                  collect(Collectors.toList());
82  
83          UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
84                  new org.springframework.security.core.userdetails.User(
85                          "admin", "FAKE_PASSWORD", authorities), "FAKE_PASSWORD", authorities);
86          auth.setDetails(new SyncopeAuthenticationDetails(SyncopeConstants.MASTER_DOMAIN, null));
87          SecurityContextHolder.getContext().setAuthentication(auth);
88      }
89  
90      @AfterAll
91      public static void unsetAuthContext() {
92          SecurityContextHolder.getContext().setAuthentication(null);
93      }
94  
95      @Autowired
96      private ReportLogic logic;
97  
98      private void checkExport(final String execKey) throws IOException {
99          ByteArrayOutputStream os = new ByteArrayOutputStream();
100 
101         logic.exportExecutionResult(os, execKey);
102 
103         os.close();
104         byte[] entity = os.toByteArray();
105         assertTrue(entity.length > 0);
106     }
107 
108     @Test
109     public void executeAndExport() throws Exception {
110         ReportTO report = logic.read("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
111         assertNotNull(report);
112         assertTrue(report.isActive());
113 
114         report.getExecutions().forEach(exec -> logic.deleteExecution(exec.getKey()));
115 
116         report = logic.read(report.getKey());
117         assertTrue(report.getExecutions().isEmpty());
118 
119         JobDataMap jobDataMap = new JobDataMap(Map.of(JobManager.EXECUTOR_KEY, "test"));
120         JobExecutionContext ctx = mock(JobExecutionContext.class);
121         when(ctx.getMergedJobDataMap()).thenReturn(jobDataMap);
122 
123         ReportJobDelegate delegate = (ReportJobDelegate) ApplicationContextProvider.getBeanFactory().
124                 createBean(TestReportJobDelegate.class, AbstractBeanDefinition.AUTOWIRE_BY_NAME, false);
125         delegate.execute(report.getKey(), false, ctx);
126 
127         report = logic.read(report.getKey());
128         assertFalse(report.getExecutions().isEmpty());
129 
130         String execKey = report.getExecutions().get(0).getKey();
131 
132         checkExport(execKey);
133     }
134 }