Coverage Report - com.jcabi.http.mock.MkAnswer
 
Classes in this File Line Coverage Branch Coverage Complexity
MkAnswer
N/A
N/A
1.143
MkAnswer$Simple
75%
22/29
10%
2/20
1.143
 
 1  
 /**
 2  
  * Copyright (c) 2011-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.http.mock;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.http.ImmutableHeader;
 35  
 import com.jcabi.http.RequestBody;
 36  
 import com.jcabi.immutable.Array;
 37  
 import com.jcabi.log.Logger;
 38  
 import java.net.HttpURLConnection;
 39  
 import java.nio.charset.Charset;
 40  
 import java.util.LinkedList;
 41  
 import java.util.List;
 42  
 import java.util.Map;
 43  
 import java.util.concurrent.ConcurrentHashMap;
 44  
 import java.util.concurrent.ConcurrentMap;
 45  
 import lombok.EqualsAndHashCode;
 46  
 
 47  
 /**
 48  
  * Mock response.
 49  
  *
 50  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 51  
  * @version $Id: 5700aaab8c53ec1add1e050d8d7a40b44e72a90f $
 52  
  * @since 0.10
 53  
  */
 54  
 @Immutable
 55  
 public interface MkAnswer {
 56  
 
 57  
     /**
 58  
      * HTTP response status.
 59  
      * @return The status code
 60  
      */
 61  
     int status();
 62  
 
 63  
     /**
 64  
      * HTTP response headers.
 65  
      * @return The headers
 66  
      */
 67  
     Map<String, List<String>> headers();
 68  
 
 69  
     /**
 70  
      * HTTP response body.
 71  
      * @return The body, as a UTF-8 string
 72  
      */
 73  
     String body();
 74  
 
 75  
     /**
 76  
      * Simple implementation.
 77  
      */
 78  
     @Immutable
 79  0
     @EqualsAndHashCode(of = { "code", "hdrs", "content" })
 80  
     @Loggable(Loggable.DEBUG)
 81  
     final class Simple implements MkAnswer {
 82  
         /**
 83  
          * The Charset to use.
 84  
          */
 85  1
         private static final Charset CHARSET = Charset.forName("UTF-8");
 86  
         /**
 87  
          * Encapsulated response.
 88  
          */
 89  
         private final transient int code;
 90  
         /**
 91  
          * Headers.
 92  
          */
 93  
         private final transient Array<Map.Entry<String, String>> hdrs;
 94  
         /**
 95  
          * Content received.
 96  
          */
 97  
         @Immutable.Array
 98  
         private final transient byte[] content;
 99  
         /**
 100  
          * Public ctor.
 101  
          * @param body Body of HTTP response
 102  
          */
 103  
         public Simple(final String body) {
 104  72
             this(HttpURLConnection.HTTP_OK, body);
 105  72
         }
 106  
         /**
 107  
          * Public ctor (with empty HTTP body).
 108  
          * @param status HTTP status
 109  
          * @since 1.9
 110  
          */
 111  
         public Simple(final int status) {
 112  6
             this(status, "");
 113  6
         }
 114  
         /**
 115  
          * Public ctor.
 116  
          * @param status HTTP status
 117  
          * @param body Body of HTTP response
 118  
          */
 119  
         public Simple(final int status, final String body) {
 120  86
             this(
 121  
                 status, new Array<Map.Entry<String, String>>(),
 122  
                 body.getBytes(MkAnswer.Simple.CHARSET)
 123  
             );
 124  86
         }
 125  
         /**
 126  
          * Public ctor.
 127  
          * @param status HTTP status
 128  
          * @param headers HTTP headers
 129  
          * @param body Body of HTTP response
 130  
          */
 131  
         public Simple(final int status,
 132  
             final Iterable<Map.Entry<String, String>> headers,
 133  113
             final byte[] body) {
 134  113
             this.code = status;
 135  113
             this.hdrs = new Array<Map.Entry<String, String>>(headers);
 136  113
             this.content = body.clone();
 137  113
         }
 138  
         @Override
 139  
         public int status() {
 140  100
             return this.code;
 141  
         }
 142  
         @Override
 143  
         @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
 144  
         public Map<String, List<String>> headers() {
 145  123
             final ConcurrentMap<String, List<String>> map =
 146  
                 new ConcurrentHashMap<>(0);
 147  123
             for (final Map.Entry<String, String> header : this.hdrs) {
 148  62
                 map.putIfAbsent(header.getKey(), new LinkedList<String>());
 149  62
                 map.get(header.getKey()).add(header.getValue());
 150  62
             }
 151  123
             return map;
 152  
         }
 153  
         @Override
 154  
         public String body() {
 155  104
             return new String(this.content, MkAnswer.Simple.CHARSET);
 156  
         }
 157  
         @Override
 158  
         public String toString() {
 159  0
             final StringBuilder text = new StringBuilder(0)
 160  
                 .append(this.code).append('\n');
 161  0
             for (final Map.Entry<String, String> header : this.hdrs) {
 162  0
                 text.append(
 163  
                     Logger.format(
 164  
                         "%s: %s\n",
 165  
                         header.getKey(),
 166  
                         header.getValue()
 167  
                     )
 168  
                 );
 169  0
             }
 170  0
             return text.append('\n')
 171  
                 .append(new RequestBody.Printable(this.content))
 172  
                 .toString();
 173  
         }
 174  
         /**
 175  
          * Make a copy of this answer, with an extra header.
 176  
          * @param name Name of the header
 177  
          * @param value ImmutableHeader value
 178  
          * @return New answer
 179  
          */
 180  
         public MkAnswer.Simple withHeader(final String name,
 181  
             final String value) {
 182  21
             return new MkAnswer.Simple(
 183  
                 this.code,
 184  
                 this.hdrs.with(new ImmutableHeader(name, value)),
 185  
                 this.content
 186  
             );
 187  
         }
 188  
         /**
 189  
          * Make a copy of this answer, with another status code.
 190  
          * @param status Status code
 191  
          * @return New answer
 192  
          */
 193  
         public MkAnswer.Simple withStatus(final int status) {
 194  3
             return new MkAnswer.Simple(
 195  
                 status,
 196  
                 this.hdrs,
 197  
                 this.content
 198  
             );
 199  
         }
 200  
         /**
 201  
          * Make a copy of this answer, with another body.
 202  
          * @param body Body
 203  
          * @return New answer
 204  
          */
 205  
         public MkAnswer.Simple withBody(final String body) {
 206  0
             return new MkAnswer.Simple(
 207  
                 this.code,
 208  
                 this.hdrs,
 209  
                 body.getBytes(MkAnswer.Simple.CHARSET)
 210  
             );
 211  
         }
 212  
     }
 213  
 
 214  
 }