JSIEVE-107 Added unit test to verify issue JSIEVE-107
[james-jsieve.git] / util / src / test / java / org / apache / jsieve / util / ScriptCheckMailAdapterTest.java
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.jsieve.util;
20
21 import static org.assertj.core.api.Assertions.assertThat;
22
23 import java.io.ByteArrayInputStream;
24
25 import javax.mail.Session;
26 import javax.mail.internet.MimeMessage;
27
28 import org.apache.jsieve.ConfigurationManager;
29 import org.apache.jsieve.SieveFactory;
30 import org.apache.jsieve.mail.ActionRedirect;
31 import org.apache.jsieve.parser.generated.Node;
32 import org.apache.jsieve.util.check.ScriptCheckMailAdapter;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 public class ScriptCheckMailAdapterTest {
37
38 private static final String FOLDED_SUBJECT_MATCH = "if header :is \"subject\" \"Test with long long long long long long long long long "
39 + "long long long long long long long long long long long very very long folded subject email\" { "
40 + "redirect \"test@test.test\";" + "}";
41
42 private static final String ENCODED_SUBJECT_MATCH = "if header :is \"subject\" \"Test però £ ù €\" { "
43 + "redirect \"test@test.test\";" + "}";
44
45 private SieveFactory sieveFactory;
46
47
48 @Before
49 public void setUp() throws Exception {
50 this.sieveFactory = new ConfigurationManager().build();
51 }
52
53 @Test
54 public void askedActionMustBeExecutedWhenFoldedHeaderMatchs() throws Exception {
55
56 //Given
57 // Load test message file
58 MimeMessage message = new MimeMessage(Session.getDefaultInstance(System.getProperties()),
59 ClassLoader.getSystemResourceAsStream("org/apache/jsieve/util/FoldedSubjectEmail.eml"));
60
61 // Associate it to the adapter
62 ScriptCheckMailAdapter testee = new ScriptCheckMailAdapter();
63 testee.setMail(message);
64
65 //When
66 // Parse sieve script
67 Node parsedSieve = sieveFactory.parse(new ByteArrayInputStream(FOLDED_SUBJECT_MATCH.getBytes("UTF-8")));
68
69 // Evaluate the script against the message
70 sieveFactory.evaluate(testee, parsedSieve);
71
72 //Then
73 // Test is OK if we have the "redirect" action
74 assertThat(testee.getActions()).hasSize(1);
75 assertThat(testee.getActions().get(0)).isInstanceOf(ActionRedirect.class);
76 }
77
78 @Test
79 public void askedActionMustBeExecutedWhenEncodedHeaderMatchs() throws Exception {
80
81 //Given
82 // Load test message file
83 MimeMessage message = new MimeMessage(Session.getDefaultInstance(System.getProperties()),
84 ClassLoader.getSystemResourceAsStream("org/apache/jsieve/util/EncodedSubjectEmail.eml"));
85
86 // Associate it to the adapter
87 ScriptCheckMailAdapter testee = new ScriptCheckMailAdapter();
88 testee.setMail(message);
89
90 //When
91 // Parse sieve script
92 Node parsedSieve = sieveFactory.parse(new ByteArrayInputStream(ENCODED_SUBJECT_MATCH.getBytes("UTF-8")));
93
94 // Evaluate the script against the message
95 sieveFactory.evaluate(testee, parsedSieve);
96
97 //Then
98 // Test is OK if we have the "redirect" action
99 assertThat(testee.getActions()).hasSize(1);
100 assertThat(testee.getActions().get(0)).isInstanceOf(ActionRedirect.class);
101 }
102 }