Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Jun 1, 2024
1 parent 99a36a8 commit 84a1cb2
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package com.formkiq.stacks.dynamodb.attributes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -44,14 +43,16 @@
import software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate;
import software.amazon.awssdk.services.dynamodb.model.QueryResponse;

import static com.formkiq.aws.dynamodb.SiteIdKeyGenerator.createDatabaseKey;


/**
* DynamoDB implementation for {@link AttributeService}.
*/
public class AttributeServiceDynamodb implements AttributeService, DbKeys {

/** {@link DynamoDbService}. */
private DynamoDbService db;
private final DynamoDbService db;

/**
* constructor.
Expand All @@ -67,11 +68,9 @@ public AttributeServiceDynamodb(final DynamoDbService dbService) {
public Collection<ValidationError> addAttribute(final String siteId, final String key,
final AttributeDataType dataType, final AttributeType type) {

Collection<ValidationError> errors = Collections.emptyList();
Collection<ValidationError> errors = validate(siteId, key);

if (key == null || key.isEmpty()) {
errors = Arrays.asList(new ValidationErrorImpl().key("key").error("'key' is required"));
} else {
if (errors.isEmpty()) {

AttributeRecord a = new AttributeRecord().documentId(key).key(key)
.type(type != null ? type : AttributeType.STANDARD)
Expand All @@ -82,6 +81,24 @@ public Collection<ValidationError> addAttribute(final String siteId, final Strin
return errors;
}

private Collection<ValidationError> validate(final String siteId, final String key) {

Collection<ValidationError> errors = Collections.emptyList();

if (key == null || key.isEmpty()) {
errors = Collections
.singletonList(new ValidationErrorImpl().key("key").error("'key' is required"));
} else {
AttributeRecord attribute = getAttribute(siteId, key);
if (attribute != null) {
errors = Collections.singletonList(
new ValidationErrorImpl().key("key").error("attribute '" + key + "' already exists"));
}
}

return errors;
}

@Override
public Collection<ValidationError> deleteAttribute(final String siteId, final String key) {

Expand Down Expand Up @@ -114,7 +131,7 @@ public PaginationResults<AttributeRecord> findAttributes(final String siteId,

Map<String, AttributeValue> startkey = new PaginationToAttributeValue().apply(token);
QueryConfig config = new QueryConfig().indexName(DbKeys.GSI1).scanIndexForward(Boolean.TRUE);
AttributeValue pk = AttributeValue.fromS("attr#");
AttributeValue pk = AttributeValue.fromS(createDatabaseKey(siteId, "attr#"));
AttributeValue sk = AttributeValue.fromS("attr#");
QueryResponse response = this.db.queryBeginsWith(config, pk, sk, startkey, limit);

Expand Down Expand Up @@ -154,7 +171,7 @@ public Map<String, AttributeRecord> getAttributes(final String siteId,
List<Map<String, AttributeValue>> values = this.db.getBatch(new BatchGetConfig(), keys);

return values.stream().map(a -> new AttributeRecord().getFromAttributes(siteId, a))
.collect(Collectors.toMap(a -> a.getKey(), a -> a));
.collect(Collectors.toMap(AttributeRecord::getKey, a -> a));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* MIT License
*
* Copyright (c) 2018 - 2020 FormKiQ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.api.awstest;

import com.formkiq.client.api.MappingsApi;
import com.formkiq.client.invoker.ApiClient;
import com.formkiq.client.invoker.ApiException;
import com.formkiq.client.model.AddMapping;
import com.formkiq.client.model.AddMappingRequest;
import com.formkiq.client.model.AttributeDataType;
import com.formkiq.client.model.MappingAttribute;
import com.formkiq.client.model.MappingAttributeLabelMatchingType;
import com.formkiq.client.model.MappingAttributeSourceType;
import com.formkiq.testutils.aws.AbstractAwsIntegrationTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import java.util.Arrays;
import java.util.List;

import static com.formkiq.testutils.aws.FkqAttributeService.addAttribute;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Integration Tests for.
* <p>
* GET /mappings
* </p>
* <p>
* POST /mappings
* </p>
* <p>
* GET /mappings/{mappingId}
* </p>
* <p>
* PUT /mappings/{mappingId}
* </p>
* <p>
* DELETE /mappings/{mappingId}
* </p>
*/
public class MappingRequestTest extends AbstractAwsIntegrationTest {

/** JUnit Test Timeout. */
private static final int TEST_TIMEOUT = 30;

/**
* Add Attribute empty request.
*
* @throws ApiException ApiException
*/
@Test
@Timeout(value = TEST_TIMEOUT)
public void addMappingRequest01() throws ApiException {
// given
for (String siteId : Arrays.asList(null, SITE_ID)) {

List<ApiClient> apiClients = getApiClients(siteId);

for (ApiClient apiClient : apiClients) {

MappingsApi api = new MappingsApi(apiClient);

// when
try {
api.addMapping(new AddMappingRequest(), siteId);
fail();
} catch (ApiException e) {
// then
assertEquals("{\"message\":\"invalid request body\"}", e.getResponseBody());
}
}
}
}

/**
* Add Attribute empty request.
*
* @throws ApiException ApiException
*/
@Test
@Timeout(value = TEST_TIMEOUT)
public void addMappingRequest02() throws ApiException {
// given
for (String siteId : Arrays.asList(null, SITE_ID)) {

List<ApiClient> apiClients = getApiClients(siteId);
ApiClient client = apiClients.get(0);

addAttribute(client, siteId, "invoice", AttributeDataType.STRING, null);
MappingsApi api = new MappingsApi(client);

AddMapping addMapping = new AddMapping().name("test")
.addAttributesItem(new MappingAttribute().attributeKey("invoice")
.sourceType(MappingAttributeSourceType.CONTENT)
.labelMatchingType(MappingAttributeLabelMatchingType.CONTAINS)
.labelTexts(List.of("invoice")));

// when
api.addMapping(new AddMappingRequest().mapping(addMapping), siteId);

// then
}
}
}
Loading

0 comments on commit 84a1cb2

Please sign in to comment.