Coverage Report - com.jcabi.dynamo.mock.MkItem
 
Classes in this File Line Coverage Branch Coverage Complexity
MkItem
80%
17/21
6%
2/30
1.429
MkItem$1
100%
2/2
N/A
1.429
MkItem$AjcClosure1
100%
1/1
N/A
1.429
MkItem$AjcClosure3
100%
1/1
N/A
1.429
MkItem$AjcClosure5
100%
1/1
N/A
1.429
MkItem$AjcClosure7
100%
1/1
N/A
1.429
MkItem$AjcClosure9
0%
0/1
N/A
1.429
 
 1  0
 /**
 2  
  * Copyright (c) 2012-2016, 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.dynamo.mock;
 31  
 
 32  
 import com.amazonaws.services.dynamodbv2.model.AttributeValue;
 33  
 import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
 34  
 import com.google.common.base.Function;
 35  
 import com.google.common.collect.ImmutableMap;
 36  
 import com.google.common.collect.Maps;
 37  
 import com.jcabi.aspects.Immutable;
 38  
 import com.jcabi.aspects.Loggable;
 39  
 import com.jcabi.dynamo.AttributeUpdates;
 40  
 import com.jcabi.dynamo.Attributes;
 41  
 import com.jcabi.dynamo.Conditions;
 42  
 import com.jcabi.dynamo.Frame;
 43  
 import com.jcabi.dynamo.Item;
 44  
 import java.io.IOException;
 45  
 import java.util.HashMap;
 46  
 import java.util.Locale;
 47  
 import java.util.Map;
 48  
 import lombok.EqualsAndHashCode;
 49  
 import lombok.ToString;
 50  
 
 51  
 /**
 52  
  * Mock version of {@link Item}.
 53  
  *
 54  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 55  
  * @version $Id: 18b26b1e1faec17c898db76bfca2925a7c66be91 $
 56  
  * @since 0.10
 57  
  */
 58  
 @Immutable
 59  2
 @ToString
 60  
 @Loggable(Loggable.DEBUG)
 61  0
 @EqualsAndHashCode(of = { "data", "table", "attributes" })
 62  
 final class MkItem implements Item {
 63  
 
 64  
     /**
 65  
      * Data.
 66  
      */
 67  
     private final transient MkData data;
 68  
 
 69  
     /**
 70  
      * Table name.
 71  
      */
 72  
     private final transient String table;
 73  
 
 74  
     /**
 75  
      * Attributes.
 76  
      */
 77  
     private final transient Attributes attributes;
 78  
 
 79  
     /**
 80  
      * Public ctor.
 81  
      * @param dta Data
 82  
      * @param tbl Table
 83  
      * @param attribs Map of attributes
 84  
      */
 85  4
     MkItem(final MkData dta, final String tbl, final Attributes attribs) {
 86  4
         this.data = dta;
 87  4
         this.table = tbl;
 88  4
         this.attributes = attribs;
 89  4
     }
 90  
 
 91  
     @Override
 92  
     public AttributeValue get(final String name) throws IOException {
 93  8
         return this.data.iterate(
 94  
             this.table,
 95  
             new Conditions().withAttributes(
 96  
                 this.attributes.only(this.data.keys(this.table))
 97  
             )
 98  
         ).iterator().next().get(name);
 99  
     }
 100  
 
 101  
     @Override
 102  
     public boolean has(final String name) throws IOException {
 103  2
         return this.data.iterate(
 104  
             this.table,
 105  
             new Conditions().withAttributes(
 106  
                 this.attributes.only(this.data.keys(this.table))
 107  
             )
 108  
         ).iterator().next().containsKey(name.toLowerCase(Locale.ENGLISH));
 109  
     }
 110  
 
 111  
     @Override
 112  
     public Map<String, AttributeValue> put(
 113  
         final String name, final AttributeValueUpdate value)
 114  
         throws IOException {
 115  4
         return this.put(
 116  
             new ImmutableMap.Builder<String, AttributeValueUpdate>()
 117  
                 .put(name, value).build()
 118  
         );
 119  
     }
 120  
 
 121  
     @Override
 122  
     public Map<String, AttributeValue> put(
 123  
         final Map<String, AttributeValueUpdate> attrs) throws IOException {
 124  4
         final Map<String, AttributeValue> keys =
 125  
             new HashMap<String, AttributeValue>(0);
 126  2
         for (final String attr : this.data.keys(this.table)) {
 127  2
             keys.put(attr, this.attributes.get(attr));
 128  2
         }
 129  
         try {
 130  2
             this.data.update(
 131  
                 this.table,
 132  
                 new Attributes(keys),
 133  
                 new AttributeUpdates(attrs)
 134  
             );
 135  0
         } catch (final IOException ex) {
 136  0
             throw new IllegalStateException(ex);
 137  2
         }
 138  2
         return Maps.transformValues(
 139  
             attrs,
 140  6
             new Function<AttributeValueUpdate, AttributeValue>() {
 141  
                 @Override
 142  
                 public AttributeValue apply(final AttributeValueUpdate update) {
 143  4
                     return update.getValue();
 144  
                 }
 145  
             }
 146  
         );
 147  
     }
 148  
 
 149  
     @Override
 150  
     public Frame frame() {
 151  0
         return new MkFrame(this.data, this.table);
 152  
     }
 153  
 }