Coverage Report - com.jcabi.github.mock.MkAssignees
 
Classes in this File Line Coverage Branch Coverage Complexity
MkAssignees
72%
18/25
62%
5/8
3.4
MkAssignees$1
16%
1/6
N/A
3.4
 
 1  2
 /**
 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.github.Assignees;
 34  
 import com.jcabi.github.Coordinates;
 35  
 import com.jcabi.github.User;
 36  
 import com.jcabi.xml.XML;
 37  
 import java.io.IOException;
 38  
 import java.util.HashSet;
 39  
 import java.util.List;
 40  
 import java.util.Set;
 41  
 import org.apache.commons.lang3.StringUtils;
 42  
 
 43  
 /**
 44  
  * Mock for Github Assignees.
 45  
  *
 46  
  * @author Paul Polishchuk (ppol@ua.fm)
 47  
  * @version $Id: d70565aabdecf38bc90d7203f15a84c2b1ea9907 $
 48  
  * @since 0.7
 49  
  */
 50  
 @Immutable
 51  0
 final class MkAssignees implements Assignees {
 52  
 
 53  
     /**
 54  
      * Storage.
 55  
      */
 56  
     private final transient MkStorage storage;
 57  
 
 58  
     /**
 59  
      * Login of the user logged in.
 60  
      */
 61  
     private final transient String self;
 62  
 
 63  
     /**
 64  
      * Repo name.
 65  
      */
 66  
     private final transient Coordinates coords;
 67  
 
 68  
     /**
 69  
      * Public ctor.
 70  
      * @param stg Storage
 71  
      * @param login User to login
 72  
      * @param rep Repo
 73  
      * @throws IOException If there is any I/O problem
 74  
      */
 75  
     MkAssignees(
 76  
         final MkStorage stg,
 77  
         final String login,
 78  
         final Coordinates rep
 79  3
     ) throws IOException {
 80  3
         this.storage = stg;
 81  3
         this.self = login;
 82  3
         this.coords = rep;
 83  3
     }
 84  
 
 85  
     @Override
 86  
     public Iterable<User> iterate() {
 87  
         try {
 88  1
             final Set<User> assignees = new HashSet<User>();
 89  1
             assignees.add(new MkUser(this.storage, this.self));
 90  1
             final Iterable<User> collaborators = new MkIterable<User>(
 91  
                 this.storage,
 92  1
                 String.format("%s/user", this.xpath()),
 93  1
                 new MkIterable.Mapping<User>() {
 94  
                     @Override
 95  
                     public User map(final XML xml) {
 96  
                         try {
 97  0
                             return new MkUser(
 98  0
                                 MkAssignees.this.storage,
 99  0
                                 xml.xpath("login/text()").get(0)
 100  
                             );
 101  0
                         } catch (final IOException ex) {
 102  0
                             throw new IllegalStateException(ex);
 103  
                         }
 104  
                     }
 105  
                 }
 106  
             );
 107  1
             for (final User collab : collaborators) {
 108  0
                 assignees.add(collab);
 109  0
             }
 110  1
             return assignees;
 111  0
         } catch (final IOException ex) {
 112  0
             throw new IllegalStateException(ex);
 113  
         }
 114  
     }
 115  
 
 116  
     @Override
 117  
     public boolean check(
 118  
         final String login
 119  
     ) {
 120  
         try {
 121  4
             final List<String> xpath = this.storage.xml().xpath(
 122  2
                 String.format("%s/user/login/text()", this.xpath())
 123  
             );
 124  4
             return this.self.equalsIgnoreCase(login) || (
 125  1
                 !xpath.isEmpty()
 126  1
                     && StringUtils.equalsIgnoreCase(login, xpath.get(0))
 127  
                 );
 128  0
         } catch (final IOException ex) {
 129  0
             throw new IllegalStateException(ex);
 130  
         }
 131  
     }
 132  
 
 133  
     /**
 134  
      * XPath of the Collaborators element in the XML tree.
 135  
      * @return XPath
 136  
      */
 137  
     private String xpath() {
 138  3
         return String.format(
 139  
             "/github/repos/repo[@coords='%s']/collaborators",
 140  
             this.coords
 141  
         );
 142  
     }
 143  
 }