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