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.persistence.jpa.outer;
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.assertNull;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  
27  import java.time.OffsetDateTime;
28  import javax.persistence.EntityExistsException;
29  import javax.ws.rs.core.MediaType;
30  import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
31  import org.apache.syncope.core.persistence.api.dao.ReportDAO;
32  import org.apache.syncope.core.persistence.api.dao.ReportExecDAO;
33  import org.apache.syncope.core.persistence.api.entity.Report;
34  import org.apache.syncope.core.persistence.api.entity.ReportExec;
35  import org.apache.syncope.core.persistence.jpa.AbstractTest;
36  import org.junit.jupiter.api.Test;
37  import org.springframework.beans.factory.annotation.Autowired;
38  import org.springframework.transaction.annotation.Transactional;
39  
40  @Transactional("Master")
41  public class ReportTest extends AbstractTest {
42  
43      @Autowired
44      private ReportDAO reportDAO;
45  
46      @Autowired
47      private ReportExecDAO reportExecDAO;
48  
49      @Autowired
50      private ImplementationDAO implementationDAO;
51  
52      @Test
53      public void find() {
54          Report report = reportDAO.find("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
55          assertNotNull(report);
56  
57          assertNotNull(report.getExecs());
58          assertFalse(report.getExecs().isEmpty());
59          assertEquals(1, report.getExecs().size());
60      }
61  
62      @Test
63      public void saveWithExistingName() {
64          assertThrows(EntityExistsException.class, () -> {
65              Report report = reportDAO.find("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
66              assertNotNull(report);
67  
68              String name = report.getName();
69  
70              report = entityFactory.newEntity(Report.class);
71              report.setName(name);
72              report.setJobDelegate(implementationDAO.find("SampleReportJobDelegate"));
73              report.setMimeType(MediaType.TEXT_PLAIN);
74              report.setFileExt("txt");
75              report.setActive(true);
76  
77              reportDAO.save(report);
78              entityManager().flush();
79          });
80      }
81  
82      @Test
83      public void save() {
84          Report report = reportDAO.find("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
85          assertNotNull(report);
86          assertEquals(1, report.getExecs().size());
87  
88          ReportExec reportExec = entityFactory.newEntity(ReportExec.class);
89          reportExec.setReport(report);
90          reportExec.setStart(OffsetDateTime.now());
91          reportExec.setEnd(OffsetDateTime.now());
92          reportExec.setStatus("SUCCESS");
93          reportExec.setExecutor("admin");
94  
95          report.add(reportExec);
96          reportDAO.save(report);
97  
98          entityManager().flush();
99  
100         report = reportDAO.find("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
101         assertNotNull(report);
102         assertEquals(2, report.getExecs().size());
103     }
104 
105     @Test
106     public void deleteReport() {
107         reportDAO.delete("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
108 
109         entityManager().flush();
110 
111         assertNull(reportDAO.find("0062ea9c-924d-4ecf-9961-4492a8cc6d1b"));
112         assertNull(reportExecDAO.find("0062ea9c-924d-4ecf-9961-4492a8cc6d1b"));
113     }
114 
115     @Test
116     public void deleteReportExecution() {
117         ReportExec execution = reportExecDAO.find("c13f39c5-0d35-4bff-ba79-3cd5de940369");
118         int executionNumber = execution.getReport().getExecs().size();
119 
120         reportExecDAO.delete("c13f39c5-0d35-4bff-ba79-3cd5de940369");
121 
122         entityManager().flush();
123 
124         assertNull(reportExecDAO.find("0062ea9c-924d-4ecf-9961-4492a8cc6d1b"));
125 
126         Report report = reportDAO.find("0062ea9c-924d-4ecf-9961-4492a8cc6d1b");
127         assertEquals(report.getExecs().size(), executionNumber - 1);
128     }
129 }