View Javadoc
1   /**
2    * Copyright (c) 2012-2015, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.odesk;
31  
32  import com.jcabi.log.Logger;
33  import org.junit.rules.TestRule;
34  import org.junit.runner.Description;
35  import org.junit.runners.model.Statement;
36  
37  /**
38   * Rule that creates {@link Odesk} instance.
39   *
40   * @author Yegor Bugayenko (yegor@tpc2.com)
41   * @version $Id: 5d0049c69112eeaaaa2f913b80f3d975416f0124 $
42   * @since 0.3
43   */
44  final class OdeskRule implements TestRule {
45  
46      /**
47       * Odesk key.
48       */
49      private static final String KEY =
50          System.getProperty("failsafe.odesk.key");
51  
52      /**
53       * Odesk secret.
54       */
55      private static final String SECRET =
56          System.getProperty("failsafe.odesk.secret");
57  
58      /**
59       * Odesk token.
60       */
61      private static final String TOKEN =
62          System.getProperty("failsafe.odesk.token");
63  
64      /**
65       * Token secret.
66       */
67      private static final String TSECRET =
68          System.getProperty("failsafe.odesk.tsecret");
69  
70      /**
71       * Odesk we're working with.
72       */
73      private transient Odesk subj;
74  
75      /**
76       * Get odesk.
77       * @return Odesk
78       */
79      public Odesk odesk() {
80          return this.subj;
81      }
82  
83      @Override
84      public Statement apply(final Statement stmt, final Description desc) {
85          // @checkstyle IllegalThrows (10 lines)
86          return new Statement() {
87              @Override
88              public void evaluate() throws Throwable {
89                  if (OdeskRule.KEY == null) {
90                      Logger.warn(
91                          this,
92                          "sys prop failsafe.odesk.key is not set, skipping"
93                      );
94                  } else {
95                      OdeskRule.this.connect();
96                      stmt.evaluate();
97                  }
98              }
99          };
100     }
101 
102     /**
103      * Create Odesk subj.
104      * @throws Exception If fails
105      */
106     private void connect() throws Exception {
107         this.subj = new RtOdesk(
108             OdeskRule.KEY,
109             OdeskRule.SECRET,
110             OdeskRule.TOKEN,
111             OdeskRule.TSECRET
112         );
113     }
114 
115 }