Commit 82aa33c8147a0257d21f8687af51e41e76ead214

Authored by Alex Mukha
1 parent 3e6401a922
Exists in master

Added a mocked test.

... ... @@ -17,6 +17,8 @@
17 17 <aws.dynamodb.sdk.version>1.11.69</aws.dynamodb.sdk.version>
18 18 <aws.lambda.core.version>1.1.0</aws.lambda.core.version>
19 19 <jackson.core.version>2.8.5</jackson.core.version>
  20 + <junit.version>4.12</junit.version>
  21 + <mockito.version>1.10.19</mockito.version>
20 22 </properties>
21 23  
22 24 <dependencies>
... ... @@ -35,6 +37,18 @@
35 37 <artifactId>jackson-core</artifactId>
36 38 <version>${jackson.core.version}</version>
37 39 </dependency>
  40 + <dependency>
  41 + <groupId>junit</groupId>
  42 + <artifactId>junit</artifactId>
  43 + <version>${junit.version}</version>
  44 + <scope>test</scope>
  45 + </dependency>
  46 + <dependency>
  47 + <groupId>org.mockito</groupId>
  48 + <artifactId>mockito-all</artifactId>
  49 + <version>${mockito.version}</version>
  50 + <scope>test</scope>
  51 + </dependency>
38 52 </dependencies>
39 53  
40 54 <!--Custom repository:-->
... ...
src/main/java/org/alfresco/heartbeat/service/HeartbeatService.java
... ... @@ -22,21 +22,37 @@ public class HeartbeatService
22 22  
23 23 private static HeartbeatService instance;
24 24  
25   - private HeartbeatService()
  25 + /**
  26 + * Default constructor for testing purposes.
  27 + * {@link HeartbeatService#getInstance()} should be used instead of this.
  28 + */
  29 + HeartbeatService()
  30 + {
  31 + }
  32 +
  33 + private HeartbeatService(HeartbeatDAO heartbeatDAO)
26 34 {
27   - heartbeatDAO = new HeartbeatDAO();
  35 + this.heartbeatDAO = heartbeatDAO;
28 36 }
29 37  
30 38 public static HeartbeatService getInstance()
31 39 {
32 40 if (instance == null)
33 41 {
34   - instance = new HeartbeatService();
  42 + instance = new HeartbeatService(new HeartbeatDAO());
35 43 }
36 44 return instance;
37 45 }
38 46  
39 47 /**
  48 + * Setter for testing purposes
  49 + */
  50 + public void setHeartbeatDAO(HeartbeatDAO heartbeatDAO)
  51 + {
  52 + this.heartbeatDAO = heartbeatDAO;
  53 + }
  54 +
  55 + /**
40 56 * @param requestBody the body of the request as JSON containing all necessary data to save
41 57 * @return response body JSON as String
42 58 */
... ...
src/test/java/org/alfresco/heartbeat/service/HeartbeatServiceTest.java
... ... @@ -0,0 +1,51 @@
  1 +package org.alfresco.heartbeat.service;
  2 +
  3 +import org.junit.Before;
  4 +import org.junit.Test;
  5 +import org.mockito.Mockito;
  6 +
  7 +import static org.alfresco.heartbeat.service.HeartbeatService.FEATURE_KEY;
  8 +import static org.alfresco.heartbeat.service.HeartbeatService.PAYLOAD_KEY;
  9 +import static org.alfresco.heartbeat.service.HeartbeatService.UUID_KEY;
  10 +import static org.alfresco.heartbeat.service.HeartbeatService.VERSION_KEY;
  11 +import static org.mockito.Matchers.eq;
  12 +import static org.mockito.Mockito.mock;
  13 +import static org.mockito.Mockito.verify;
  14 +
  15 +/**
  16 + * Tests for {@link HeartbeatService}
  17 + *
  18 + * @author amukha
  19 + */
  20 +public class HeartbeatServiceTest
  21 +{
  22 + private HeartbeatDAO dao = mock(HeartbeatDAO.class);
  23 + private HeartbeatService heartbeatService;
  24 +
  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\"}";
  29 + private static final String VALID_INPUT_JSON =
  30 + "{\n" +
  31 + "\"" + UUID_KEY + "\" : \"%s\",\n" +
  32 + "\"" + FEATURE_KEY + "\" : \"%s\",\n" +
  33 + "\"" + VERSION_KEY + "\" : \"%s\",\n" +
  34 + "\"" + PAYLOAD_KEY + "\" : %s" +
  35 + "}";
  36 +
  37 + @Before
  38 + public void before()
  39 + {
  40 + heartbeatService = new HeartbeatService();
  41 + heartbeatService.setHeartbeatDAO(dao);
  42 + }
  43 +
  44 + @Test
  45 + public void testSaveWithValidData() throws Exception
  46 + {
  47 + heartbeatService.saveData(String.format(VALID_INPUT_JSON, UUID, FEATURE, VERSION, PAYLOAD));
  48 + verify(dao).putItem(eq(UUID), eq(FEATURE), eq(VERSION), Mockito.anyString(), eq(PAYLOAD));
  49 + }
  50 +
  51 +}
... ...