Coverage Report - com.jcabi.github.mock.MkStars
 
Classes in this File Line Coverage Branch Coverage Complexity
MkStars
82%
14/17
11%
3/26
1.167
MkStars$AjcClosure1
0%
0/1
N/A
1.167
MkStars$AjcClosure3
100%
1/1
N/A
1.167
MkStars$AjcClosure5
100%
1/1
N/A
1.167
MkStars$AjcClosure7
100%
1/1
N/A
1.167
 
 1  4
 /**
 2  
  * Copyright (c) 2013-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.github.mock;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.github.Coordinates;
 35  
 import com.jcabi.github.Repo;
 36  
 import com.jcabi.github.Stars;
 37  
 import java.io.IOException;
 38  
 import java.util.List;
 39  
 import javax.validation.constraints.NotNull;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import lombok.ToString;
 42  
 import org.apache.commons.lang3.StringUtils;
 43  
 import org.xembly.Directives;
 44  
 
 45  
 /**
 46  
  * Github starring API.
 47  
  * @author Paul Polishchuk (ppol@ua.fm)
 48  
  * @version $Id$
 49  
  * @since 0.15
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @ToString
 54  0
 @EqualsAndHashCode(of = { "storage", "self", "coords" })
 55  
 final class MkStars implements Stars {
 56  
 
 57  
     /**
 58  
      * Storage.
 59  
      */
 60  
     private final transient MkStorage storage;
 61  
     /**
 62  
      * Login of the user logged in.
 63  
      */
 64  
     private final transient String self;
 65  
     /**
 66  
      * Repo's name.
 67  
      */
 68  
     private final transient Coordinates coords;
 69  
 
 70  
     /**
 71  
      * Public ctor.
 72  
      * @param stg The storage.
 73  
      * @param login The login name.
 74  
      * @param rep The Repository.
 75  
      * @throws java.io.IOException If something goes wrong.
 76  
      */
 77  
     MkStars(
 78  
         @NotNull(message = "stg can't be NULL") final MkStorage stg,
 79  
         @NotNull(message = "login can't be NULL") final String login,
 80  
         @NotNull(message = "rep can't be NULL") final Coordinates rep
 81  3
     ) throws IOException {
 82  3
         this.storage = stg;
 83  3
         this.self = login;
 84  3
         this.coords = rep;
 85  3
         this.storage.apply(
 86  
             new Directives().xpath("/github/repos/repo")
 87  
                 .addIf("stars")
 88  
         );
 89  3
     }
 90  
 
 91  
     @Override
 92  
     public Repo repo() {
 93  0
         return new MkRepo(this.storage, this.self, this.coords);
 94  
     }
 95  
 
 96  
     @Override
 97  
     public boolean starred() throws IOException {
 98  4
         final List<String> xpath = this.storage.xml().xpath(
 99  
             String.format("%s/star/login/text()", this.xpath())
 100  
         );
 101  2
         return !xpath.isEmpty()
 102  
             && StringUtils.equalsIgnoreCase(this.self, xpath.get(0));
 103  
     }
 104  
 
 105  
     @Override
 106  
     public void star() throws IOException {
 107  4
         this.storage.apply(
 108  
             new Directives().xpath(this.xpath()).add("star").add("login")
 109  
                 .set(this.self)
 110  
         );
 111  2
     }
 112  
 
 113  
     @Override
 114  
     public void unstar() throws IOException {
 115  2
         this.storage.apply(
 116  
             new Directives().xpath(this.xpath())
 117  
                 .xpath(String.format("star/login[.='%s']", this.self))
 118  
                 .remove()
 119  
         );
 120  1
     }
 121  
 
 122  
     /**
 123  
      * XPath of this element in XML tree.
 124  
      * @return XPath
 125  
      */
 126  
     @NotNull(message = "Xpath is never NULL")
 127  
     private String xpath() {
 128  5
         return String.format(
 129  
             "/github/repos/repo[@coords='%s']/stars",
 130  
             this.coords
 131  
         );
 132  
     }
 133  
 }