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.io.IOException;
28  import java.nio.charset.StandardCharsets;
29  import java.util.List;
30  import javax.ws.rs.core.MediaType;
31  import javax.ws.rs.core.Response;
32  import org.apache.commons.io.IOUtils;
33  import org.apache.syncope.common.lib.SyncopeClientException;
34  import org.apache.syncope.common.lib.to.MailTemplateTO;
35  import org.apache.syncope.common.lib.types.ClientExceptionType;
36  import org.apache.syncope.common.lib.types.MailTemplateFormat;
37  import org.apache.syncope.fit.AbstractITCase;
38  import org.junit.jupiter.api.Test;
39  
40  public class MailTemplateITCase extends AbstractITCase {
41  
42      @Test
43      public void read() {
44          MailTemplateTO mailTemplateTO = MAIL_TEMPLATE_SERVICE.read("optin");
45          assertNotNull(mailTemplateTO);
46      }
47  
48      @Test
49      public void list() {
50          List<MailTemplateTO> mailTemplateTOs = MAIL_TEMPLATE_SERVICE.list();
51          assertNotNull(mailTemplateTOs);
52          assertFalse(mailTemplateTOs.isEmpty());
53          for (MailTemplateTO instance : mailTemplateTOs) {
54              assertNotNull(instance);
55          }
56      }
57  
58      @Test
59      public void crud() throws IOException {
60          final String key = getUUIDString();
61  
62          // 1. create (empty) mail template
63          MailTemplateTO mailTemplateTO = new MailTemplateTO();
64          mailTemplateTO.setKey(key);
65  
66          Response response = MAIL_TEMPLATE_SERVICE.create(mailTemplateTO);
67          assertEquals(201, response.getStatus());
68  
69          // 2. attempt to read HTML and TEXT -> fail
70          try {
71              MAIL_TEMPLATE_SERVICE.getFormat(key, MailTemplateFormat.HTML);
72              fail("This should not happen");
73          } catch (SyncopeClientException e) {
74              assertEquals(ClientExceptionType.NotFound, e.getType());
75          }
76          try {
77              MAIL_TEMPLATE_SERVICE.getFormat(key, MailTemplateFormat.TEXT);
78              fail("This should not happen");
79          } catch (SyncopeClientException e) {
80              assertEquals(ClientExceptionType.NotFound, e.getType());
81          }
82  
83          // 3. set TEXT
84          String textTemplate = "Hi there, I am ${username}.";
85          MAIL_TEMPLATE_SERVICE.setFormat(
86                  key, MailTemplateFormat.TEXT, IOUtils.toInputStream(textTemplate, StandardCharsets.UTF_8));
87  
88          response = MAIL_TEMPLATE_SERVICE.getFormat(key, MailTemplateFormat.TEXT);
89          assertEquals(200, response.getStatus());
90          assertTrue(response.getMediaType().toString().startsWith(MediaType.TEXT_PLAIN));
91          assertEquals(textTemplate, response.readEntity(String.class));
92  
93          // 3. set HTML
94          String htmlTemplate = "<html><body>Hi there, I am ${username}.</body></html>";
95          MAIL_TEMPLATE_SERVICE.setFormat(
96                  key, MailTemplateFormat.HTML, IOUtils.toInputStream(htmlTemplate, StandardCharsets.UTF_8));
97  
98          response = MAIL_TEMPLATE_SERVICE.getFormat(key, MailTemplateFormat.HTML);
99          assertEquals(200, response.getStatus());
100         assertTrue(response.getMediaType().toString().startsWith(MediaType.TEXT_HTML));
101         assertEquals(htmlTemplate, response.readEntity(String.class));
102 
103         // 4. remove HTML
104         MAIL_TEMPLATE_SERVICE.removeFormat(key, MailTemplateFormat.HTML);
105 
106         try {
107             MAIL_TEMPLATE_SERVICE.getFormat(key, MailTemplateFormat.HTML);
108             fail("This should not happen");
109         } catch (SyncopeClientException e) {
110             assertEquals(ClientExceptionType.NotFound, e.getType());
111         }
112 
113         response = MAIL_TEMPLATE_SERVICE.getFormat(key, MailTemplateFormat.TEXT);
114         assertEquals(200, response.getStatus());
115         assertTrue(response.getMediaType().toString().startsWith(MediaType.TEXT_PLAIN));
116         assertEquals(textTemplate, response.readEntity(String.class));
117 
118         // 5. remove mail template
119         MAIL_TEMPLATE_SERVICE.delete(key);
120 
121         try {
122             MAIL_TEMPLATE_SERVICE.read(key);
123             fail("This should not happen");
124         } catch (SyncopeClientException e) {
125             assertEquals(ClientExceptionType.NotFound, e.getType());
126         }
127         try {
128             MAIL_TEMPLATE_SERVICE.getFormat(key, MailTemplateFormat.HTML);
129             fail("This should not happen");
130         } catch (SyncopeClientException e) {
131             assertEquals(ClientExceptionType.NotFound, e.getType());
132         }
133         try {
134             MAIL_TEMPLATE_SERVICE.getFormat(key, MailTemplateFormat.TEXT);
135             fail("This should not happen");
136         } catch (SyncopeClientException e) {
137             assertEquals(ClientExceptionType.NotFound, e.getType());
138         }
139     }
140 
141     @Test
142     public void issueSYNCOPE866() {
143         MailTemplateTO mailTemplateTO = new MailTemplateTO();
144         mailTemplateTO.setKey("optin");
145         try {
146             MAIL_TEMPLATE_SERVICE.create(mailTemplateTO);
147             fail("This should not happen");
148         } catch (SyncopeClientException e) {
149             assertEquals(ClientExceptionType.EntityExists, e.getType());
150         }
151     }
152 }