Skip to content

Commit

Permalink
Issue yegor256#748 Implemented email notifications on release (only u…
Browse files Browse the repository at this point in the history
…nit-test)
  • Loading branch information
alevohin committed Jan 20, 2015
1 parent c23cd07 commit 1033fac
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@
<artifactId>jcabi-github</artifactId>
<version>0.18.6</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-email</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/com/rultor/agents/Mails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright (c) 2009-2015, rultor.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rultor.agents;

import com.jcabi.aspects.Immutable;
import com.jcabi.email.Postman;
import com.jcabi.xml.XML;
import com.rultor.spi.Profile;
import java.io.IOException;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Send email after release done.
*
* @author Yuriy Alevohin ([email protected])
* @version $Id$
* @since 2.0
* @todo #748 Implement Mails agent. Similar to what we do in CommentsTag
* we should do here - send an email to all listed addresses. The body of
* the email should contain similar text to what we create for the tag
* comment. Postman's parameters are stored in Manifest.MF. We need 4
* parameters: Rultor-SMTPHost, Rultor-SMTPPort, Rultor-SMTPUsername,
* Rultor-SMTPPassword. New instance of Mails must be created at Agents,
* after CommentsTag.
* @todo #748 Describe in file 2014-07-13-reference.md config parameters for
* email after release and how it works shortly. Config format described
* in issue #748.
*/
@Immutable
@ToString
@EqualsAndHashCode(callSuper = false, of = { "profile", "postman" })
public final class Mails extends AbstractAgent {

/**
* Profile.
*/
private final transient Profile profile;

/**
* Postman for sending mails.
*/
private final transient Postman postman;

/**
* Ctor.
* @param prfl Profile
* @param pstmn Mail client
*/
public Mails(final Profile prfl,
final Postman pstmn) {
super(
"/talk/request[@id and type='release' and success='true']"
);
this.postman = pstmn;
this.profile = prfl;
}

@Override
public Iterable<Directive> process(final XML xml) throws IOException {
return new Directives();
}
}
147 changes: 147 additions & 0 deletions src/test/java/com/rultor/agents/MailsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* Copyright (c) 2009-2015, rultor.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rultor.agents;

import com.jcabi.email.Envelope;
import com.jcabi.email.Postman;
import com.jcabi.manifests.Manifests;
import com.jcabi.xml.XMLDocument;
import com.rultor.spi.Agent;
import com.rultor.spi.Profile;
import com.rultor.spi.Talk;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.xembly.Directives;

/**
* Tests for ${@link com.rultor.agents.Mails}.
*
* @author Yuriy Alevohin ([email protected])
* @version $Id$
* @since 2.0
*/
public final class MailsTest {

/**
* Mails can send a mail.
* @throws Exception In case of error.
*/
@Test
@Ignore
public void sendsMail() throws Exception {
final Postman postman = Mockito.spy(Postman.CONSOLE);
final Profile profile = this.profile();
final Agent agent = new Mails(
profile,
postman
);
final Talk talk = MailsTest.talk();
agent.execute(talk);
final ArgumentCaptor<Envelope> captor =
ArgumentCaptor.forClass(Envelope.class);
Mockito.verify(postman).send(captor.capture());
final Envelope envelope = captor.getValue();
MatcherAssert.assertThat(
envelope.unwrap().getContent().toString(),
Matchers.allOf(
Matchers.containsString("See #456, release log:"),
Matchers.containsString("Released by Rultor"),
Matchers.containsString(Manifests.read("Rultor-Version")),
Matchers.containsString(
"see [build log](http://www.rultor.com/t/123-abcdef)"
)
)
);
MatcherAssert.assertThat(
envelope.unwrap().getSubject(),
Matchers.equalTo("user/repo v2.0 released!")
);
}

/**
* Mails can send a mail to recipients.
* @throws Exception In case of error.
* @todo #748 Implement method sendsToRecipients. It must check that
* mail is sent to all recipients. Recipients are defined in Profile.
*/
@Test
@Ignore
@SuppressWarnings("PMD.UncommentedEmptyMethod")
public void sendsToRecipients() throws Exception {
}

/**
* Profile for test
* @return Profile
*/
private Profile.Fixed profile() {
return new Profile.Fixed(
new XMLDocument(
StringUtils.join(
"<p><entry key='release'>",
"<entry key='email'>",
"<item>test1@localhost</item>",
"<item>test2@localhost</item>",
"</entry>",
"</entry></p>"
)
)
);
}

/**
* Make a talk with this tag.
* @return Talk
* @throws java.io.IOException If fails
*/
private static Talk talk() throws IOException {
final Talk talk = new Talk.InFile();
talk.modify(
new Directives().xpath("/talk")
.attr("number", "123")
.add("wire")
.add("github-issue").set("456")
.add("github-repo").set("user/repo")
.up()
.up()
.add("request").attr("id", "abcdef")
.add("type").set("release").up()
.add("success").set("true").up()
.add("args").add("arg").attr("name", "tag").set("v2.0")
);
return talk;
}
}

0 comments on commit 1033fac

Please sign in to comment.