Coverage Report - co.stateful.RtCounters
 
Classes in this File Line Coverage Branch Coverage Complexity
RtCounters
0%
0/16
0%
0/12
1
RtCounters$AjcClosure1
0%
0/1
N/A
1
RtCounters$AjcClosure3
0%
0/1
N/A
1
RtCounters$AjcClosure5
0%
0/1
N/A
1
RtCounters$AjcClosure7
0%
0/1
N/A
1
 
 1  0
 /**
 2  
  * Copyright (c) 2014-2016, stateful.co
 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 stateful.co 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 co.stateful;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.http.Request;
 35  
 import com.jcabi.http.response.RestResponse;
 36  
 import com.jcabi.http.response.XmlResponse;
 37  
 import com.jcabi.log.Logger;
 38  
 import java.io.IOException;
 39  
 import java.net.HttpURLConnection;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import lombok.ToString;
 42  
 
 43  
 /**
 44  
  * Counters.
 45  
  *
 46  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 47  
  * @version $Id$
 48  
  * @since 0.1
 49  
  * @checkstyle MultipleStringLiteralsCheck (500 lines)
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @ToString(of = { })
 54  0
 @EqualsAndHashCode(of = "request")
 55  
 final class RtCounters implements Counters {
 56  
 
 57  
     /**
 58  
      * Entry request.
 59  
      */
 60  
     private final transient Request request;
 61  
 
 62  
     /**
 63  
      * Ctor.
 64  
      * @param req Request
 65  
      */
 66  0
     RtCounters(final Request req) {
 67  0
         this.request = req;
 68  0
     }
 69  
 
 70  
     @Override
 71  
     public Iterable<String> names() throws IOException {
 72  0
         return this.request.fetch()
 73  
             .as(RestResponse.class)
 74  
             .assertStatus(HttpURLConnection.HTTP_OK)
 75  
             .as(XmlResponse.class)
 76  
             .xml()
 77  
             .xpath("/page/counters/counter/name/text()");
 78  
     }
 79  
 
 80  
     @Override
 81  
     public Counter create(final String name) throws IOException {
 82  0
         final long start = System.currentTimeMillis();
 83  0
         this.request.fetch()
 84  
             .as(RestResponse.class)
 85  
             .assertStatus(HttpURLConnection.HTTP_OK)
 86  
             .as(XmlResponse.class)
 87  
             .rel("/page/links/link[@rel='add']/@href")
 88  
             .method(Request.POST)
 89  
             .body().formParam("name", name).back()
 90  
             .fetch()
 91  
             .as(RestResponse.class)
 92  
             .assertStatus(HttpURLConnection.HTTP_SEE_OTHER);
 93  0
         Logger.info(
 94  
             this, "counter \"%s\" created in %[ms]s",
 95  
             name, System.currentTimeMillis() - start
 96  
         );
 97  0
         return this.get(name);
 98  
     }
 99  
 
 100  
     @Override
 101  
     public void delete(final String name) throws IOException {
 102  0
         final long start = System.currentTimeMillis();
 103  0
         this.request.fetch()
 104  
             .as(RestResponse.class)
 105  
             .assertStatus(HttpURLConnection.HTTP_OK)
 106  
             .as(XmlResponse.class)
 107  
             .rel(
 108  
                 String.format(
 109  
                     // @checkstyle LineLength (1 line)
 110  
                     "/page/counters/counter[name='%s']/links/link[@rel='delete']/@href",
 111  
                     name
 112  
                 )
 113  
             )
 114  
             .uri().queryParam("name", name).back()
 115  
             .fetch()
 116  
             .as(RestResponse.class)
 117  
             .assertStatus(HttpURLConnection.HTTP_SEE_OTHER);
 118  0
         Logger.info(
 119  
             this, "counter \"%s\" deleted in %[ms]s",
 120  
             name, System.currentTimeMillis() - start
 121  
         );
 122  0
     }
 123  
 
 124  
     @Override
 125  
     public Counter get(final String name) throws IOException {
 126  0
         return new RtCounter(name, this.request);
 127  
     }
 128  
 }