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.awaitility.Awaitility.await;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertNotNull;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  import static org.junit.jupiter.api.Assertions.fail;
27  
28  import java.io.IOException;
29  import java.time.OffsetDateTime;
30  import java.util.List;
31  import java.util.Set;
32  import java.util.concurrent.TimeUnit;
33  import java.util.concurrent.atomic.AtomicReference;
34  import javax.ws.rs.core.HttpHeaders;
35  import javax.ws.rs.core.Response;
36  import org.apache.syncope.common.lib.SyncopeClientException;
37  import org.apache.syncope.common.lib.to.ExecTO;
38  import org.apache.syncope.common.lib.to.ReportTO;
39  import org.apache.syncope.common.lib.types.ClientExceptionType;
40  import org.apache.syncope.common.lib.types.IdRepoImplementationType;
41  import org.apache.syncope.common.rest.api.RESTHeaders;
42  import org.apache.syncope.common.rest.api.batch.BatchResponseItem;
43  import org.apache.syncope.common.rest.api.beans.ExecQuery;
44  import org.apache.syncope.common.rest.api.beans.ExecSpecs;
45  import org.apache.syncope.core.provisioning.java.job.report.ReportJob;
46  import org.apache.syncope.fit.AbstractITCase;
47  import org.apache.syncope.fit.core.reference.SampleReportJobDelegate;
48  import org.junit.jupiter.api.Assertions;
49  import org.junit.jupiter.api.Test;
50  import org.springframework.http.MediaType;
51  
52  public class ReportITCase extends AbstractITCase {
53  
54      protected static String execReport(final String reportKey) {
55          AtomicReference<ReportTO> reportTO = new AtomicReference<>(REPORT_SERVICE.read(reportKey));
56          int preExecSize = reportTO.get().getExecutions().size();
57          ExecTO execution = REPORT_SERVICE.execute(new ExecSpecs.Builder().key(reportKey).build());
58          assertNotNull(execution.getExecutor());
59  
60          await().atMost(MAX_WAIT_SECONDS, TimeUnit.SECONDS).pollInterval(1, TimeUnit.SECONDS).until(() -> {
61              try {
62                  reportTO.set(REPORT_SERVICE.read(reportKey));
63                  return preExecSize < reportTO.get().getExecutions().size();
64              } catch (Exception e) {
65                  return false;
66              }
67          });
68          ExecTO exec = reportTO.get().getExecutions().get(reportTO.get().getExecutions().size() - 1);
69          assertEquals(ReportJob.Status.SUCCESS.name(), exec.getStatus());
70          return exec.getKey();
71      }
72  
73      @Test
74      public void getReportDelegates() {
75          Set<String> reportDelegates = ANONYMOUS_CLIENT.platform().
76                  getJavaImplInfo(IdRepoImplementationType.REPORT_DELEGATE).get().getClasses();
77          assertNotNull(reportDelegates);
78          assertFalse(reportDelegates.isEmpty());
79          assertTrue(reportDelegates.contains(SampleReportJobDelegate.class.getName()));
80      }
81  
82      @Test
83      public void list() {
84          List<ReportTO> reports = REPORT_SERVICE.list();
85          assertNotNull(reports);
86          assertFalse(reports.isEmpty());
87          reports.forEach(Assertions::assertNotNull);
88      }
89  
90      @Test
91      public void read() {
92          ReportTO reportTO = REPORT_SERVICE.read("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
93  
94          assertNotNull(reportTO);
95          assertNotNull(reportTO.getExecutions());
96          assertFalse(reportTO.getExecutions().isEmpty());
97      }
98  
99      @Test
100     public void crud() {
101         ReportTO report = new ReportTO();
102         report.setName("testReportForCreate" + getUUIDString());
103         report.setMimeType(MediaType.APPLICATION_PDF_VALUE);
104         report.setFileExt("pdf");
105         report.setJobDelegate(SampleReportJobDelegate.class.getSimpleName());
106         report.setActive(true);
107 
108         report = createReport(report);
109         assertNotNull(report);
110         ReportTO actual = REPORT_SERVICE.read(report.getKey());
111         assertNotNull(actual);
112         assertEquals(actual, report);
113 
114         report = actual;
115         report.setMimeType("text/csv");
116         report.setFileExt("csv");
117 
118         REPORT_SERVICE.update(report);
119         actual = REPORT_SERVICE.read(report.getKey());
120         assertNotNull(actual);
121         assertEquals("csv", actual.getFileExt());
122 
123         REPORT_SERVICE.delete(report.getKey());
124         try {
125             REPORT_SERVICE.read(report.getKey());
126             fail("This should not happen");
127         } catch (SyncopeClientException e) {
128             assertEquals(Response.Status.NOT_FOUND, e.getType().getResponseStatus());
129         }
130     }
131 
132     @Test
133     public void executeAndExport() throws IOException {
134         ReportTO report = REPORT_SERVICE.read("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
135         report.setActive(false);
136         report.getExecutions().clear();
137         REPORT_SERVICE.update(report);
138 
139         try {
140             execReport(report.getKey());
141             fail("This should not happen");
142         } catch (SyncopeClientException e) {
143             assertEquals(ClientExceptionType.Scheduling, e.getType());
144             assertTrue(e.getElements().iterator().next().contains("active"));
145         }
146 
147         report.setActive(true);
148         REPORT_SERVICE.update(report);
149 
150         String execKey = execReport(report.getKey());
151 
152         Response response = REPORT_SERVICE.exportExecutionResult(execKey);
153         assertNotNull(response);
154         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusInfo().getStatusCode());
155         assertNotNull(response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION));
156         assertTrue(response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION).endsWith(".pdf"));
157 
158         assertFalse(response.readEntity(String.class).isEmpty());
159     }
160 
161     @Test
162     public void deleteExecutions() throws IOException {
163         OffsetDateTime start = OffsetDateTime.now();
164         try {
165             Thread.sleep(1000);
166         } catch (InterruptedException e) {
167             // ignore
168         }
169 
170         ReportTO report = REPORT_SERVICE.read("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
171         report.setName("deleteExecutions" + getUUIDString());
172         report.getExecutions().clear();
173         report = createReport(report);
174         assertNotNull(report);
175 
176         String execKey = execReport(report.getKey());
177         assertNotNull(execKey);
178 
179         try {
180             Thread.sleep(1000);
181         } catch (InterruptedException e) {
182         }
183         OffsetDateTime end = OffsetDateTime.now();
184 
185         Response response = REPORT_SERVICE.deleteExecutions(
186                 new ExecQuery.Builder().key(report.getKey()).after(start).before(end).build());
187         List<BatchResponseItem> batchResponseItems = parseBatchResponse(response);
188         assertEquals(1, batchResponseItems.size());
189         assertEquals(execKey, batchResponseItems.get(0).getHeaders().get(RESTHeaders.RESOURCE_KEY).get(0));
190         assertEquals(Response.Status.OK.getStatusCode(), batchResponseItems.get(0).getStatus());
191     }
192 
193     @Test
194     public void issueSYNCOPE102() throws IOException {
195         // Create
196         ReportTO reportTO = REPORT_SERVICE.read("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
197         reportTO.setName("issueSYNCOPE102" + getUUIDString());
198         reportTO = createReport(reportTO);
199         assertNotNull(reportTO.getKey());
200         String reportKey = reportTO.getKey();
201 
202         // Execute (multiple requests)
203         for (int i = 0; i < 10; i++) {
204             assertNotNull(REPORT_SERVICE.execute(new ExecSpecs.Builder().key(reportKey).build()));
205         }
206 
207         // Wait for one execution
208         await().atMost(MAX_WAIT_SECONDS, TimeUnit.SECONDS).pollInterval(1, TimeUnit.SECONDS).until(() -> {
209             try {
210                 return !REPORT_SERVICE.read(reportKey).getExecutions().isEmpty();
211             } catch (Exception e) {
212                 return false;
213             }
214         });
215     }
216 }