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.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.assertTrue;
25  import static org.junit.jupiter.api.Assertions.fail;
26  
27  import java.util.List;
28  import javax.ws.rs.core.Response;
29  import org.apache.syncope.client.lib.SyncopeClient;
30  import org.apache.syncope.common.lib.SyncopeClientException;
31  import org.apache.syncope.common.lib.to.NotificationTO;
32  import org.apache.syncope.common.lib.types.AnyTypeKind;
33  import org.apache.syncope.common.lib.types.ClientExceptionType;
34  import org.apache.syncope.common.lib.types.TraceLevel;
35  import org.apache.syncope.common.rest.api.service.NotificationService;
36  import org.apache.syncope.fit.AbstractITCase;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.Test;
39  
40  public class NotificationITCase extends AbstractITCase {
41  
42      private static NotificationTO buildNotificationTO() {
43          NotificationTO notificationTO = new NotificationTO();
44          notificationTO.setTraceLevel(TraceLevel.SUMMARY);
45          notificationTO.getEvents().add("create");
46  
47          notificationTO.getAbouts().put(AnyTypeKind.USER.name(),
48                  SyncopeClient.getUserSearchConditionBuilder().
49                          is("fullname").equalTo("*o*").and("fullname").equalTo("*i*").query());
50  
51          notificationTO.setRecipientAttrName("email");
52  
53          notificationTO.setSender("syncope@syncope.apache.org");
54          notificationTO.setSubject("Test notification");
55          notificationTO.setTemplate("test");
56          return notificationTO;
57      }
58  
59      @Test
60      public void read() {
61          NotificationTO notificationTO = NOTIFICATION_SERVICE.read("9e2b911c-25de-4c77-bcea-b86ed9451050");
62          assertNotNull(notificationTO);
63      }
64  
65      @Test
66      public void list() {
67          List<NotificationTO> notificationTOs = NOTIFICATION_SERVICE.list();
68          assertNotNull(notificationTOs);
69          assertFalse(notificationTOs.isEmpty());
70          notificationTOs.forEach(Assertions::assertNotNull);
71      }
72  
73      @Test
74      public void create() {
75          NotificationTO notificationTO = buildNotificationTO();
76          notificationTO.setRecipientsFIQL(SyncopeClient.getUserSearchConditionBuilder().
77                  inGroups("bf825fe1-7320-4a54-bd64-143b5c18ab97").query());
78  
79          Response response = NOTIFICATION_SERVICE.create(notificationTO);
80          NotificationTO actual = getObject(response.getLocation(), NotificationService.class, NotificationTO.class);
81  
82          assertNotNull(actual);
83          assertNotNull(actual.getKey());
84          notificationTO.setKey(actual.getKey());
85          assertEquals(actual, notificationTO);
86      }
87  
88      @Test
89      public void update() {
90          NotificationTO notificationTO = NOTIFICATION_SERVICE.read("9e2b911c-25de-4c77-bcea-b86ed9451050");
91          notificationTO.setRecipientsFIQL(SyncopeClient.getUserSearchConditionBuilder().
92                  inGroups("bf825fe1-7320-4a54-bd64-143b5c18ab97").query());
93  
94          NOTIFICATION_SERVICE.update(notificationTO);
95          NotificationTO actual = NOTIFICATION_SERVICE.read(notificationTO.getKey());
96          assertNotNull(actual);
97          assertEquals(actual, notificationTO);
98      }
99  
100     @Test
101     public void delete() {
102         NotificationTO notification = buildNotificationTO();
103         notification.setSelfAsRecipient(true);
104         Response response = NOTIFICATION_SERVICE.create(notification);
105         notification = getObject(response.getLocation(), NotificationService.class, NotificationTO.class);
106 
107         NOTIFICATION_SERVICE.delete(notification.getKey());
108 
109         try {
110             NOTIFICATION_SERVICE.read(notification.getKey());
111             fail("This should not happen");
112         } catch (SyncopeClientException e) {
113             assertEquals(ClientExceptionType.NotFound, e.getType());
114         }
115     }
116 
117     @Test
118     public void issueSYNCOPE83() {
119         NotificationTO notificationTO = buildNotificationTO();
120         notificationTO.setSelfAsRecipient(true);
121 
122         NotificationTO actual = null;
123         try {
124             Response response = NOTIFICATION_SERVICE.create(notificationTO);
125             actual = getObject(response.getLocation(), NotificationService.class, NotificationTO.class);
126         } catch (SyncopeClientException e) {
127             assertNotNull(e);
128         }
129         assertNotNull(actual);
130         assertNotNull(actual.getKey());
131         notificationTO.setKey(actual.getKey());
132         assertEquals(actual, notificationTO);
133     }
134 
135     @Test
136     public void issueSYNCOPE445() {
137         NotificationTO notificationTO = buildNotificationTO();
138         notificationTO.getStaticRecipients().add("syncope445@syncope.apache.org");
139 
140         NotificationTO actual = null;
141         try {
142             Response response = NOTIFICATION_SERVICE.create(notificationTO);
143             actual = getObject(response.getLocation(), NotificationService.class, NotificationTO.class);
144         } catch (SyncopeClientException e) {
145             assertNotNull(e);
146         }
147         assertNotNull(actual);
148         assertNotNull(actual.getKey());
149         notificationTO.setKey(actual.getKey());
150         assertEquals(actual, notificationTO);
151     }
152 
153     @Test
154     public void issueSYNCOPE446() {
155         NotificationTO notificationTO = buildNotificationTO();
156         notificationTO.getStaticRecipients().add("syncope446@syncope.apache.org");
157         notificationTO.getAbouts().put(AnyTypeKind.GROUP.name(),
158                 SyncopeClient.getGroupSearchConditionBuilder().is("name").equalTo("citizen").query());
159 
160         NotificationTO actual = null;
161         try {
162             Response response = NOTIFICATION_SERVICE.create(notificationTO);
163             actual = getObject(response.getLocation(), NotificationService.class, NotificationTO.class);
164         } catch (SyncopeClientException e) {
165             assertNotNull(e);
166         }
167         assertNotNull(actual);
168         assertNotNull(actual.getKey());
169         notificationTO.setKey(actual.getKey());
170         assertEquals(actual, notificationTO);
171     }
172 
173     @Test
174     public void issueSYNCOPE974() {
175         NotificationTO notificationTO = new NotificationTO();
176         notificationTO.setRecipientAttrName("email");
177         notificationTO.setSelfAsRecipient(false);
178         notificationTO.setSender("sender@ukr.net");
179         notificationTO.setSubject("subject 21");
180         notificationTO.setTemplate("requestPasswordReset");
181         notificationTO.setTraceLevel(TraceLevel.ALL);
182         notificationTO.setActive(true);
183 
184         try {
185             NOTIFICATION_SERVICE.create(notificationTO);
186             fail("This should not happen");
187         } catch (SyncopeClientException e) {
188             assertEquals(ClientExceptionType.RequiredValuesMissing, e.getType());
189             assertTrue(e.getMessage().contains("events"));
190         }
191     }
192 }