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