Skip to content

Commit 40e5cc6

Browse files
minor code review comments and Updating license header
1 parent 7a13012 commit 40e5cc6

File tree

9 files changed

+285
-2
lines changed

9 files changed

+285
-2
lines changed

serverless/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ permalink: /patterns/serverless/
66
categories: Architectural
77
tags:
88
- Java
9-
- Difficulty-Intermittent
9+
- Difficulty-Intermediate
1010
---
1111

1212
## Serverless

serverless/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<parent>
2727
<groupId>com.iluwatar</groupId>
2828
<artifactId>java-design-patterns</artifactId>
29-
<version>1.19.0-SNAPSHOT</version>
29+
<version>1.20.0-SNAPSHOT</version>
3030
</parent>
3131

3232
<dependencies>

serverless/src/main/java/com/iluwatar/serverless/baas/api/AbstractDynamoDbHandler.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
123
package com.iluwatar.serverless.baas.api;
224

325
import com.amazonaws.regions.Regions;

serverless/src/main/java/com/iluwatar/serverless/baas/api/FindPersonApiHandler.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
123
package com.iluwatar.serverless.baas.api;
224

325
import com.amazonaws.services.lambda.runtime.Context;

serverless/src/main/java/com/iluwatar/serverless/baas/api/SavePersonApiHandler.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
123
package com.iluwatar.serverless.baas.api;
224

325
import com.amazonaws.services.lambda.runtime.Context;

serverless/src/main/java/com/iluwatar/serverless/baas/model/Address.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
123
package com.iluwatar.serverless.baas.model;
224

325
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;

serverless/src/main/java/com/iluwatar/serverless/baas/model/Person.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
123
package com.iluwatar.serverless.baas.model;
224

325
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.serverless.baas.api;
24+
25+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
26+
import com.amazonaws.services.lambda.runtime.Context;
27+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
28+
import com.iluwatar.serverless.baas.api.FindPersonApiHandler;
29+
import com.iluwatar.serverless.baas.api.SavePersonApiHandler;
30+
import com.iluwatar.serverless.baas.model.Person;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.mockito.Mock;
35+
import org.mockito.runners.MockitoJUnitRunner;
36+
37+
import java.util.Collections;
38+
39+
import static org.mockito.Mockito.mock;
40+
import static org.mockito.Mockito.times;
41+
import static org.mockito.Mockito.verify;
42+
43+
/**
44+
* Unit tests for FindPersonApiHandler
45+
* Created by dheeraj.mummar on 3/5/18.
46+
*/
47+
@RunWith(MockitoJUnitRunner.class)
48+
public class FindPersonApiHandlerTest {
49+
50+
private FindPersonApiHandler findPersonApiHandler;
51+
52+
@Mock
53+
private DynamoDBMapper dynamoDbMapper;
54+
55+
@Before
56+
public void setUp() {
57+
this.findPersonApiHandler = new FindPersonApiHandler();
58+
this.findPersonApiHandler.setDynamoDbMapper(dynamoDbMapper);
59+
}
60+
61+
@Test
62+
public void handleRequest() {
63+
findPersonApiHandler.handleRequest(apiGatewayProxyRequestEvent(), mock(Context.class));
64+
verify(dynamoDbMapper, times(1)).load(Person.class, "37e7a1fe-3544-473d-b764-18128f02d72d");
65+
}
66+
67+
private APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent() {
68+
return new APIGatewayProxyRequestEvent()
69+
.withPathParamters(Collections
70+
.singletonMap("id", "37e7a1fe-3544-473d-b764-18128f02d72d"));
71+
}
72+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.serverless.baas.api;
24+
25+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
26+
import com.amazonaws.services.lambda.runtime.Context;
27+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
28+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
29+
import com.fasterxml.jackson.core.JsonProcessingException;
30+
import com.fasterxml.jackson.databind.ObjectMapper;
31+
import com.iluwatar.serverless.baas.api.SavePersonApiHandler;
32+
import com.iluwatar.serverless.baas.model.Address;
33+
import com.iluwatar.serverless.baas.model.Person;
34+
import org.junit.Assert;
35+
import org.junit.Before;
36+
import org.junit.Test;
37+
import org.junit.runner.RunWith;
38+
import org.mockito.Mock;
39+
import org.mockito.runners.MockitoJUnitRunner;
40+
41+
import static org.mockito.Mockito.*;
42+
43+
/**
44+
* Unit tests for SavePersonApiHandler
45+
* Created by dheeraj.mummar on 3/4/18.
46+
*/
47+
@RunWith(MockitoJUnitRunner.class)
48+
public class SavePersonApiHandlerTest {
49+
50+
private SavePersonApiHandler savePersonApiHandler;
51+
52+
@Mock
53+
private DynamoDBMapper dynamoDbMapper;
54+
55+
private ObjectMapper objectMapper = new ObjectMapper();
56+
57+
@Before
58+
public void setUp() {
59+
this.savePersonApiHandler = new SavePersonApiHandler();
60+
this.savePersonApiHandler.setDynamoDbMapper(dynamoDbMapper);
61+
}
62+
63+
@Test
64+
public void handleRequestSavePersonSuccessful() throws JsonProcessingException {
65+
Person person = newPerson();
66+
APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent =
67+
this.savePersonApiHandler
68+
.handleRequest(apiGatewayProxyRequestEvent(objectMapper.writeValueAsString(person)), mock(Context.class));
69+
verify(dynamoDbMapper, times(1)).save(person);
70+
Assert.assertNotNull(apiGatewayProxyResponseEvent);
71+
Assert.assertEquals(new Integer(201), apiGatewayProxyResponseEvent.getStatusCode());
72+
}
73+
74+
@Test
75+
public void handleRequestSavePersonException() {
76+
APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent =
77+
this.savePersonApiHandler
78+
.handleRequest(apiGatewayProxyRequestEvent("invalid sample request"), mock(Context.class));
79+
Assert.assertNotNull(apiGatewayProxyResponseEvent);
80+
Assert.assertEquals(new Integer(400), apiGatewayProxyResponseEvent.getStatusCode());
81+
}
82+
83+
private APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent(String body) {
84+
APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent = new APIGatewayProxyRequestEvent();
85+
return apiGatewayProxyRequestEvent.withBody(body);
86+
}
87+
88+
private Person newPerson() {
89+
Person person = new Person();
90+
person.setFirstName("Thor");
91+
person.setLastName("Odinson");
92+
Address address = new Address();
93+
address.setAddressLineOne("1 Odin ln");
94+
address.setCity("Asgard");
95+
address.setState("country of the Gods");
96+
address.setZipCode("00001");
97+
person.setAddress(address);
98+
99+
return person;
100+
}
101+
}

0 commit comments

Comments
 (0)