Commit d34e6bbf5f5bbeda3ddf1095c704f1f343f89664

Authored by Alex Mukha
1 parent 7fadfcf6af
Exists in master

Added tests.

src/main/java/org/alfresco/heartbeat/service/HeartbeatDAO.java
... ... @@ -17,10 +17,10 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
17 17 */
18 18 public class HeartbeatDAO
19 19 {
20   - private static final String TABLE_NAME_KEY = "TABLE_NAME";
21   - private static final String PRIMARY_PARTITION_KEY = "partitionKey";
22   - private static final String PRIMARY_SORT_KEY = "timestamp";
23   - private static final String TIMESTAMP_KEY = PRIMARY_SORT_KEY;
  20 + static final String TABLE_NAME_KEY = "TABLE_NAME";
  21 + static final String PRIMARY_PARTITION_KEY = "partitionKey";
  22 + static final String PRIMARY_SORT_KEY = "timestamp";
  23 + static final String TIMESTAMP_KEY = PRIMARY_SORT_KEY;
24 24  
25 25 private Table table;
26 26  
... ... @@ -42,13 +42,18 @@ public class HeartbeatDAO
42 42 table = dynamoDB.getTable(getTableName());
43 43 }
44 44  
  45 + public HeartbeatDAO(DynamoDB dynamoDB, String tableName)
  46 + {
  47 + table = dynamoDB.getTable(tableName);
  48 + }
  49 +
45 50 public PutItemOutcome putItem(String uuid,
46 51 String feature,
47 52 String version,
48 53 String timestamp,
49 54 String payload)
50 55 {
51   - String primaryPartitionKey = createPrimaryPartitionKey(uuid, feature, timestamp);
  56 + String primaryPartitionKey = createPrimaryPartitionKey(uuid, feature);
52 57  
53 58 Item item = new Item()
54 59 .withPrimaryKey(PRIMARY_PARTITION_KEY, primaryPartitionKey)
... ... @@ -60,7 +65,7 @@ public class HeartbeatDAO
60 65 return table.putItem(item);
61 66 }
62 67  
63   - private String createPrimaryPartitionKey(String uuid, String feature, String timestamp)
  68 + public String createPrimaryPartitionKey(String uuid, String feature)
64 69 {
65 70 ObjectMapper mapper = new ObjectMapper();
66 71 ObjectNode json = mapper.createObjectNode();
... ...
src/test/java/org/alfresco/heartbeat/service/HeartbeatDAOTest.java
1 1 package org.alfresco.heartbeat.service;
2 2  
  3 +import com.amazonaws.regions.Region;
  4 +import com.amazonaws.regions.Regions;
  5 +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
  6 +import com.amazonaws.services.dynamodbv2.document.DynamoDB;
  7 +import com.amazonaws.services.dynamodbv2.document.Item;
  8 +import com.amazonaws.services.dynamodbv2.document.PrimaryKey;
  9 +import com.amazonaws.services.dynamodbv2.document.Table;
  10 +import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
  11 +import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
  12 +import com.amazonaws.services.dynamodbv2.model.KeyType;
  13 +import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
  14 +import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
  15 +import org.junit.AfterClass;
  16 +import org.junit.BeforeClass;
  17 +import org.junit.Test;
  18 +
  19 +import java.util.ArrayList;
  20 +import java.util.List;
  21 +
  22 +import static org.junit.Assert.assertEquals;
  23 +import static org.junit.Assert.assertNotNull;
  24 +
3 25 /**
4   - * Created by amukha on 03/01/17.
  26 + * Integration tests for {@link HeartbeatDAO}
  27 + *
  28 + * @author amukha
5 29 */
6 30 public class HeartbeatDAOTest
7 31 {
  32 + private static DynamoDB dynamoDB;
  33 + private static final String TABLE_NAME = HeartbeatDAOTest.class.getName() + "-" + System.currentTimeMillis();
  34 +
  35 + @BeforeClass
  36 + public static void beforeClass() throws Exception
  37 + {
  38 + AmazonDynamoDBClient client = new AmazonDynamoDBClient();
  39 + client.setRegion(Region.getRegion(Regions.EU_WEST_1));
  40 + dynamoDB = new DynamoDB(client);
  41 + createTable(TABLE_NAME);
  42 + }
  43 +
  44 + @AfterClass
  45 + public static void afterClass() throws Exception
  46 + {
  47 + Table table = dynamoDB.getTable(TABLE_NAME);
  48 + table.delete();
  49 + table.waitForDelete();
  50 + }
  51 +
  52 +
  53 + private static void createTable(String tableName) throws Exception
  54 + {
  55 + List<KeySchemaElement> keySchema = new ArrayList<>();
  56 + keySchema.add(new KeySchemaElement(HeartbeatDAO.PRIMARY_PARTITION_KEY, KeyType.HASH));
  57 + keySchema.add(new KeySchemaElement(HeartbeatDAO.PRIMARY_SORT_KEY, KeyType.RANGE));
  58 + List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
  59 + attributeDefinitions.add(new AttributeDefinition(HeartbeatDAO.PRIMARY_PARTITION_KEY, ScalarAttributeType.S));
  60 + attributeDefinitions.add(new AttributeDefinition(HeartbeatDAO.PRIMARY_SORT_KEY, ScalarAttributeType.S));
  61 + Table table = dynamoDB.createTable(
  62 + tableName,
  63 + keySchema,
  64 + attributeDefinitions,
  65 + new ProvisionedThroughput(new Long(5), new Long(5)));
  66 + table.waitForActive();
  67 + }
  68 +
  69 + @Test
  70 + public void verifyTableExists()
  71 + {
  72 + Table table = dynamoDB.getTable(TABLE_NAME);
  73 + assertNotNull("The table should not be null.", table);
  74 + }
  75 +
  76 + @Test
  77 + public void putValidItem()
  78 + {
  79 + HeartbeatDAO dao = new HeartbeatDAO(dynamoDB, TABLE_NAME);
  80 + String timestamp = Long.toString(System.currentTimeMillis());
  81 + dao.putItem(
  82 + HeartbeatServiceTest.UUID,
  83 + HeartbeatServiceTest.FEATURE,
  84 + HeartbeatServiceTest.VERSION,
  85 + timestamp,
  86 + HeartbeatServiceTest.PAYLOAD
  87 + );
  88 + Table table = dynamoDB.getTable(TABLE_NAME);
  89 + PrimaryKey primaryKey = new PrimaryKey(
  90 + HeartbeatDAO.PRIMARY_PARTITION_KEY,
  91 + dao.createPrimaryPartitionKey(HeartbeatServiceTest.UUID, HeartbeatServiceTest.FEATURE),
  92 + HeartbeatDAO.PRIMARY_SORT_KEY,
  93 + timestamp);
  94 + Item item = table.getItem(primaryKey);
  95 + assertNotNull("The item should not be null.", item);
  96 + assertEquals("The values of the saved item should match.",
  97 + timestamp, item.getString(HeartbeatDAO.PRIMARY_SORT_KEY));
  98 + assertEquals("The values of the saved item should match.",
  99 + HeartbeatServiceTest.UUID, item.getString(HeartbeatService.UUID_KEY));
  100 + assertEquals("The values of the saved item should match.",
  101 + HeartbeatServiceTest.FEATURE, item.getString(HeartbeatService.FEATURE_KEY));
  102 + assertEquals("The values of the saved item should match.",
  103 + HeartbeatServiceTest.VERSION, item.getString(HeartbeatService.VERSION_KEY));
  104 + assertEquals("The values of the saved item should match.",
  105 + HeartbeatServiceTest.PAYLOAD, item.getString(HeartbeatService.PAYLOAD_KEY));
  106 + }
8 107 }
... ...
src/test/java/org/alfresco/heartbeat/service/HeartbeatServiceTest.java
... ... @@ -8,6 +8,7 @@ import static org.alfresco.heartbeat.service.HeartbeatService.FEATURE_KEY;
8 8 import static org.alfresco.heartbeat.service.HeartbeatService.PAYLOAD_KEY;
9 9 import static org.alfresco.heartbeat.service.HeartbeatService.UUID_KEY;
10 10 import static org.alfresco.heartbeat.service.HeartbeatService.VERSION_KEY;
  11 +import static org.junit.Assert.fail;
11 12 import static org.mockito.Matchers.eq;
12 13 import static org.mockito.Mockito.mock;
13 14 import static org.mockito.Mockito.verify;
... ... @@ -22,10 +23,10 @@ public class HeartbeatServiceTest
22 23 private HeartbeatDAO dao = mock(HeartbeatDAO.class);
23 24 private HeartbeatService heartbeatService;
24 25  
25   - private static final String UUID = "test-uuid-1";
26   - private static final String FEATURE = "Alfresco Content Services";
27   - private static final String VERSION = "5.2.1";
28   - private static final String PAYLOAD = "{\"users\":\"1000\",\"documents\":\"10000\"}";
  26 + static final String UUID = "test-uuid-1";
  27 + static final String FEATURE = "Alfresco Content Services";
  28 + static final String VERSION = "5.2.1";
  29 + static final String PAYLOAD = "{\"users\":\"1000\",\"documents\":\"10000\"}";
29 30 private static final String VALID_INPUT_JSON =
30 31 "{\n" +
31 32 "\"" + UUID_KEY + "\" : \"%s\",\n" +
... ... @@ -34,6 +35,14 @@ public class HeartbeatServiceTest
34 35 "\"" + PAYLOAD_KEY + "\" : %s" +
35 36 "}";
36 37  
  38 + /* doesn't have the payload */
  39 + private static final String INVALID_INPUT_JSON =
  40 + "{\n" +
  41 + "\"" + UUID_KEY + "\" : \"%s\",\n" +
  42 + "\"" + FEATURE_KEY + "\" : \"%s\",\n" +
  43 + "\"" + VERSION_KEY + "\" : \"%s\"" +
  44 + "}";
  45 +
37 46 @Before
38 47 public void before()
39 48 {
... ... @@ -48,4 +57,18 @@ public class HeartbeatServiceTest
48 57 verify(dao).putItem(eq(UUID), eq(FEATURE), eq(VERSION), Mockito.anyString(), eq(PAYLOAD));
49 58 }
50 59  
  60 + @Test
  61 + public void testSaveWithInvalidData() throws Exception
  62 + {
  63 + try
  64 + {
  65 + heartbeatService.saveData(String.format(INVALID_INPUT_JSON, UUID, FEATURE, VERSION));
  66 + fail("The IllegalArgumentException should have been thrown.");
  67 + }
  68 + catch (IllegalArgumentException iae)
  69 + {
  70 + // expected
  71 + }
  72 + }
  73 +
51 74 }
... ...