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.assertNotNull;
23  
24  import java.io.InputStream;
25  import java.util.Properties;
26  import java.util.concurrent.TimeUnit;
27  import java.util.concurrent.atomic.AtomicReference;
28  import javax.mail.Flags;
29  import javax.mail.Folder;
30  import javax.mail.Message;
31  import javax.mail.Session;
32  import javax.mail.Store;
33  import org.junit.jupiter.api.BeforeAll;
34  
35  public abstract class AbstractNotificationTaskITCase extends AbstractTaskITCase {
36  
37      private static final String POP3_HOST = "localhost";
38  
39      private static int POP3_PORT;
40  
41      @BeforeAll
42      public static void conf() {
43          Properties props = new Properties();
44          try (InputStream propStream = AbstractNotificationTaskITCase.class.getResourceAsStream("/test.properties")) {
45              props.load(propStream);
46          } catch (Exception e) {
47              LOG.error("Could not load /test.properties", e);
48          }
49  
50          POP3_PORT = Integer.parseInt(props.getProperty("testmail.pop3port"));
51          assertNotNull(POP3_PORT);
52      }
53  
54      private static boolean pop3(final String sender, final String subject, final String mailAddress) throws Exception {
55          boolean found = false;
56          Store store = null;
57          try {
58              store = Session.getDefaultInstance(System.getProperties()).getStore("pop3");
59              store.connect(POP3_HOST, POP3_PORT, mailAddress, mailAddress);
60  
61              Folder inbox = store.getFolder("INBOX");
62              assertNotNull(inbox);
63              inbox.open(Folder.READ_WRITE);
64  
65              Message[] messages = inbox.getMessages();
66              for (Message message : messages) {
67                  if (sender.equals(message.getFrom()[0].toString()) && subject.equals(message.getSubject())) {
68                      found = true;
69                      message.setFlag(Flags.Flag.DELETED, true);
70                  }
71              }
72  
73              inbox.close(true);
74          } finally {
75              if (store != null) {
76                  store.close();
77              }
78          }
79          return found;
80      }
81  
82      protected static void verifyMail(
83              final String sender,
84              final String subject,
85              final String mailAddress,
86              final int maxWaitSeconds) throws Exception {
87  
88          AtomicReference<Boolean> read = new AtomicReference<>(false);
89          await().atMost(maxWaitSeconds, TimeUnit.SECONDS).pollInterval(1, TimeUnit.SECONDS).until(() -> {
90              try {
91                  read.set(pop3(sender, subject, mailAddress));
92                  return read.get();
93              } catch (Exception e) {
94                  return false;
95              }
96          });
97      }
98  }