Skip to content

Commit 8f658eb

Browse files
committed
AMB-1741 add bare controller and service
1 parent 8f0aa90 commit 8f658eb

File tree

7 files changed

+63
-30
lines changed

7 files changed

+63
-30
lines changed

lambda_code/src/fhir_controller.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from dataclasses import dataclass
2+
3+
from fhir.resources.resource import Resource
4+
5+
6+
@dataclass
7+
class FhirResponse:
8+
headers: dict
9+
body: Resource
10+
status_code: int
11+
12+
13+
class FhirController:
14+
15+
def __init__(self, fhir_service):
16+
self.fhir_service = fhir_service
17+
18+
def get_immunisation_by_id(self, aws_event) -> FhirResponse:
19+
pass
20+
21+
@staticmethod
22+
def _create_response(status_code, body):
23+
return {
24+
"statusCode": status_code,
25+
"headers": {
26+
"Content-Type": "application/fhir+json",
27+
},
28+
"body": body
29+
}

lambda_code/src/fhir_service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class FhirService:
2+
pass

lambda_code/src/get_imms_handler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import uuid
44

55
from dynamodb import EventTable
6-
from immunisation_api import create_response
6+
from fhir_controller import FhirController
77

88

99
def create_operation_outcome(event_id, message, code):
@@ -51,12 +51,12 @@ def is_valid_id(_event_id):
5151
create_operation_outcome(str(uuid.uuid4()),
5252
"he provided event ID is either missing or not in the expected format.",
5353
"invalid"))
54-
return create_response(400, body)
54+
return FhirController._create_response(400, body)
5555

5656
query_result = dynamo_service.get_event_by_id(event_id)
5757
if query_result is None:
5858
body = json.dumps(
5959
create_operation_outcome(str(uuid.uuid4()), "The requested resource was not found.", "not-found"))
60-
return create_response(404, body)
60+
return FhirController._create_response(404, body)
6161

62-
return create_response(200, json.dumps(query_result))
62+
return FhirController._create_response(200, json.dumps(query_result))

lambda_code/src/immunisation_api.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
import requests
22

33

4-
def create_response(status_code, body):
5-
return {
6-
"statusCode": status_code,
7-
"headers": {
8-
"Content-Type": "application/fhir+json",
9-
},
10-
"body": body
11-
}
12-
13-
144
class ImmunisationApi:
155
def __init__(self, url):
166
self.url = url
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
import sys
3+
import unittest
4+
from unittest.mock import create_autospec
5+
6+
sys.path.append(f"{os.path.dirname(os.path.abspath(__file__))}/../src")
7+
8+
from fhir_controller import FhirController
9+
from fhir_service import FhirService
10+
11+
12+
class TestFhirController(unittest.TestCase):
13+
def setUp(self):
14+
self.service = create_autospec(FhirService)
15+
self.controller = FhirController(self.service)
16+
17+
def test_create_response(self):
18+
"""it should return application/fhir+json with correct status code"""
19+
res = self.controller._create_response(42, "a body")
20+
headers = res["headers"]
21+
22+
self.assertEqual(res["statusCode"], 42)
23+
self.assertDictEqual(headers, {
24+
"Content-Type": "application/fhir+json",
25+
})
26+
self.assertEqual(res["body"], "a bodys")

lambda_code/tests/test_get_imms.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from get_imms_handler import get_imms, create_operation_outcome
1212

1313

14-
class TestGetImms(unittest.TestCase):
14+
class TestGetImmunisationById(unittest.TestCase):
1515
def setUp(self):
1616
self.dynamodb_service = create_autospec(EventTable)
1717

@@ -26,7 +26,6 @@ def test_get_imms_happy_path(self):
2626
# Assert
2727
self.dynamodb_service.get_event_by_id.assert_called_once_with(lambda_event["pathParameters"]["id"])
2828
self.assertEqual(result['statusCode'], 200)
29-
self.assertEqual(result['headers']['Content-Type'], "application/fhir+json")
3029
self.assertDictEqual(json.loads(result['body']), {"message": "Mocked event data"})
3130

3231
def test_get_imms_handler_sad_path_400(self):
@@ -37,7 +36,6 @@ def test_get_imms_handler_sad_path_400(self):
3736

3837
# Assert
3938
assert result['statusCode'] == 400
40-
self.assertEqual(result['headers']['Content-Type'], "application/fhir+json")
4139
act_body = json.loads(result['body'])
4240
exp_body = create_operation_outcome(str(uuid.uuid4()),
4341
"he provided event ID is either missing or not in the expected format.",

lambda_code/tests/test_immunisation_api.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,7 @@
77

88
sys.path.append(f"{os.path.dirname(os.path.abspath(__file__))}/../src")
99

10-
from immunisation_api import ImmunisationApi, create_response
11-
12-
13-
class TestRequestResponseHelpers(unittest.TestCase):
14-
def test_create_response(self):
15-
res = create_response(42, "a body")
16-
headers = res["headers"]
17-
self.assertEqual(res["statusCode"], 42)
18-
19-
self.assertDictEqual(headers, {
20-
"Content-Type": "application/fhir+json",
21-
})
22-
self.assertEqual(res["body"], "a body")
10+
from immunisation_api import ImmunisationApi
2311

2412

2513
class TestImmsApiService(unittest.TestCase):

0 commit comments

Comments
 (0)