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.inner;
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  
26  import java.util.List;
27  import java.util.UUID;
28  import org.apache.syncope.core.persistence.api.dao.ApplicationDAO;
29  import org.apache.syncope.core.persistence.api.entity.Application;
30  import org.apache.syncope.core.persistence.api.entity.Privilege;
31  import org.apache.syncope.core.persistence.jpa.AbstractTest;
32  import org.junit.jupiter.api.Test;
33  import org.springframework.beans.factory.annotation.Autowired;
34  import org.springframework.transaction.annotation.Transactional;
35  
36  @Transactional("Master")
37  public class ApplicationTest extends AbstractTest {
38  
39      @Autowired
40      private ApplicationDAO applicationDAO;
41  
42      @Test
43      public void findAll() {
44          List<Application> applications = applicationDAO.findAll();
45          assertFalse(applications.isEmpty());
46          assertEquals(1, applications.size());
47      }
48  
49      @Test
50      public void find() {
51          Application mightyApp = applicationDAO.find("mightyApp");
52          assertNotNull(mightyApp);
53          assertEquals(2, mightyApp.getPrivileges().size());
54  
55          Privilege getMighty = applicationDAO.findPrivilege("getMighty");
56          assertNotNull(getMighty);
57          assertEquals(getMighty, mightyApp.getPrivilege("getMighty").get());
58  
59      }
60  
61      @Test
62      public void crud() {
63          // 1. create application
64          Application application = entityFactory.newEntity(Application.class);
65          application.setKey(UUID.randomUUID().toString());
66  
67          String privilege1Key = UUID.randomUUID().toString();
68          Privilege privilege = entityFactory.newEntity(Privilege.class);
69          privilege.setKey(privilege1Key);
70          privilege.setSpec("{ \"one\": true }");
71          application.add(privilege);
72  
73          String privilege2Key = UUID.randomUUID().toString();
74          privilege = entityFactory.newEntity(Privilege.class);
75          privilege.setKey(privilege2Key);
76          privilege.setSpec("{ \"two\": true }");
77          application.add(privilege);
78  
79          String privilege3Key = UUID.randomUUID().toString();
80          privilege = entityFactory.newEntity(Privilege.class);
81          privilege.setKey(privilege3Key);
82          privilege.setSpec("{ \"three\": true }");
83          application.add(privilege);
84  
85          application = applicationDAO.save(application);
86          assertNotNull(application);
87          assertNull(application.getDescription());
88          assertEquals(3, application.getPrivileges().size());
89  
90          // 2. update application
91          application.setDescription("A description");
92  
93          Privilege priv3 = applicationDAO.findPrivilege(privilege3Key);
94          priv3.setApplication(null);
95          application.getPrivileges().remove(priv3);
96          assertEquals(2, application.getPrivileges().size());
97  
98          applicationDAO.save(application);
99  
100         entityManager().flush();
101 
102         application = applicationDAO.find(application.getKey());
103         assertNotNull(application);
104         assertNotNull(application.getDescription());
105         assertEquals(2, application.getPrivileges().size());
106 
107         // 3. delete application
108         applicationDAO.delete(application);
109 
110         entityManager().flush();
111 
112         assertNull(applicationDAO.find(application.getKey()));
113         assertNull(applicationDAO.findPrivilege(privilege1Key));
114         assertNull(applicationDAO.findPrivilege(privilege2Key));
115     }
116 }