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