Coverage Report - com.jcabi.github.mock.MkMilestones
 
Classes in this File Line Coverage Branch Coverage Complexity
MkMilestones
100%
30/30
N/A
1
MkMilestones$1
100%
3/3
N/A
1
 
 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.Coordinates;
 34  
 import com.jcabi.github.Milestone;
 35  
 import com.jcabi.github.Milestones;
 36  
 import com.jcabi.github.Repo;
 37  
 import com.jcabi.xml.XML;
 38  
 import java.io.IOException;
 39  
 import java.util.Map;
 40  
 import org.xembly.Directives;
 41  
 
 42  
 /**
 43  
  * Mock Github milestones.
 44  
  * @author Mihai Andronache (amihaiemil@gmail.com)
 45  
  * @version $Id: 8c20a40810cc4b540b7752bbcedb96394139249e $
 46  
  */
 47  
 @Immutable
 48  
 final class MkMilestones implements Milestones {
 49  
 
 50  
     /**
 51  
      * Storage.
 52  
      */
 53  
     private final transient MkStorage storage;
 54  
 
 55  
     /**
 56  
      * Login of the user logged in.
 57  
      */
 58  
     private final transient String self;
 59  
 
 60  
     /**
 61  
      * Repo name.
 62  
      */
 63  
     private final transient Coordinates coords;
 64  
 
 65  
     /**
 66  
      * MkMilestones ctor.
 67  
      * @param stg Storage
 68  
      * @param login User to login
 69  
      * @param rep Repo
 70  
      * @throws IOException - if any I/O problem occurs
 71  
      */
 72  
     MkMilestones(
 73  
         final MkStorage stg,
 74  
         final String login,
 75  
         final Coordinates rep
 76  6
     ) throws IOException {
 77  6
         this.storage = stg;
 78  6
         this.self = login;
 79  6
         this.coords = rep;
 80  12
         this.storage.apply(
 81  6
             new Directives().xpath(
 82  6
                 String.format("/github/repos/repo[@coords='%s']", this.coords)
 83  6
             ).addIf("milestones")
 84  
         );
 85  6
     }
 86  
     @Override
 87  
     public Repo repo() {
 88  1
         return new MkRepo(this.storage, this.self, this.coords);
 89  
     }
 90  
 
 91  
     @Override
 92  
     public Milestone create(
 93  
         final String title
 94  
     ) throws IOException {
 95  
         final int number;
 96  10
         number = 1 + this.storage.xml().xpath(
 97  5
             String.format("%s/milestone/number/text()", this.xpath())
 98  5
         ).size();
 99  10
         this.storage.apply(
 100  5
             new Directives().xpath(this.xpath()).add("milestone")
 101  5
                 .add("number").set(Integer.toString(number)).up()
 102  5
                 .add("title").set(title).up()
 103  5
                 .add("state").set(Milestone.OPEN_STATE).up()
 104  5
                 .add("description").set("mock milestone").up()
 105  
         );
 106  5
         return this.get(number);
 107  
     }
 108  
 
 109  
     @Override
 110  
     public Milestone get(final int number) {
 111  8
         return new MkMilestone(this.storage, this.self, this.coords, number);
 112  
     }
 113  
 
 114  
     @Override
 115  
     public Iterable<Milestone> iterate(
 116  
         final Map<String, String> params
 117  
     ) {
 118  6
         return new MkIterable<Milestone>(
 119  
             this.storage,
 120  3
             String.format("%s/milestone", this.xpath()),
 121  5
             new MkIterable.Mapping<Milestone>() {
 122  
                 @Override
 123  
                 public Milestone map(final XML xml) {
 124  4
                     return MkMilestones.this.get(
 125  2
                         Integer.parseInt(xml.xpath("number/text()").get(0))
 126  
                     );
 127  
                 }
 128  
             }
 129  
         );
 130  
     }
 131  
 
 132  
     @Override
 133  
     public void remove(final int number) throws IOException {
 134  2
         this.storage.apply(
 135  1
             new Directives().xpath(
 136  1
                 String.format("%s/milestone[number='%d']", this.xpath(), number)
 137  1
             ).remove()
 138  
         );
 139  1
     }
 140  
 
 141  
     /**
 142  
      * XPath of this element in XML tree.
 143  
      * @return XPath
 144  
      */
 145  
     private String xpath() {
 146  14
         return String.format(
 147  
             "/github/repos/repo[@coords='%s']/milestones",
 148  
             this.coords
 149  
         );
 150  
     }
 151  
 }