Преглед на файлове

Create example mock requirements

Arkadiusz Ryś преди 2 години
родител
ревизия
f946762c50
променени са 5 файла, в които са добавени 90 реда и са изтрити 12 реда
  1. 7 0
      README.md
  2. 1 0
      data/mock_requirements.txt
  3. 35 12
      mocka/router.py
  4. 1 0
      requirements.txt
  5. 46 0
      tests/test_mock.py

+ 7 - 0
README.md

@@ -1,3 +1,10 @@
 # Mocka
 
 Mock Activity Endpoint.
+
+    Automated activity enactments must be HTTP calls where users must specify the endpoint in the FTG's Transformation, and the timeout in the PM's Activity.
+    The request must have a JSON body containing possible artefacts in the format: "[inputDataPortName] : {type: inline, content: [file content]},..."
+    The endpoint must provide a response in the form of a JSON as well, where it specifies the control port that should be executed and the data from the output artefacts. The control output port could have a key like "ctrl : [name of the port]" and the artefacts must have as keys the output data ports and the content in a similar way as for the request.
+    The output artefacts will be added to the knowledge graph according to the output port key provided. The original file should be saved using the storage service a.k.a. "backend".
+    WEE should have a mechanism to update the interface when an automated activity terminates. Refresh button? Event Listeners?
+    Not all artefacts may be provided when the activity terminates according to the result of the service. For instance, if an error occurs, some of the outputs may not be available. This may result in errors during the enactment.

+ 1 - 0
data/mock_requirements.txt

@@ -0,0 +1 @@
+1. Don't be evil!

+ 35 - 12
mocka/router.py

@@ -1,7 +1,12 @@
+# https://fastapi.tiangolo.com/tutorial/body-multiple-params/#singular-values-in-body
+from pathlib import Path
 from typing import Any
+import magic
+import arklog
 from fastapi import APIRouter, Query, Request, Response
 from fastapi.responses import JSONResponse
 
+
 class MockRouter(APIRouter):
     """"""
 
@@ -12,21 +17,39 @@ class MockRouter(APIRouter):
         self.configuration = configuration
         super().__init__(*args, **kwargs)
 
-        # Make this a put
         @self.get("/")
         async def root_mock(request: Request, query: str | None = Query(None)) -> Response:
-            """
-            Example request response for a simulated activity.
-            """
-            match query.lower():
-                case "error":
-                    return JSONResponse(status_code=400, content={"port": "error"})
-                case "get":
-                    return JSONResponse(status_code=200, content={"port": "ok"})
+            """Example request response for a simulated activity."""
+            if query:
+                match query.lower():
+                    case "error":
+                        return JSONResponse(status_code=400, content={"ctrl": "error"})
+                    case "get":
+                        return JSONResponse(status_code=200, content={"ctrl": "ok"})
+
+            body = await request.json()
+            control = body.get("ctrl")
+            arklog.debug(control)
+            content = body.get("input").get("din").get("content")
+            file_path = Path(__file__).parent.parent / Path("data/mock_requirements.txt")
+            requirements = file_path.read_text() + "\n\nChecked!"
+            mime = magic.Magic(mime=True).from_file(file_path)
+            assert content + "\n\nChecked!" == requirements
             return JSONResponse(status_code=201, content={
-                "port": "ok",
+                "ctrl": "ok",
                 "output": {
-                    "artefact_1": "<uri>",
-                    "artefact_2": "<uri>",
+                    "dout": {
+                        "type": "inline",
+                        "content": requirements,
+                        "encoding": mime
+                    }
                 }
             })
+
+        @self.put("/")
+        async def root_put_mock(request: Request, query: str | None = Query(None)) -> Response:
+            return await root_mock(request, query)
+
+        @self.post("/")
+        async def root_post_mock(request: Request, query: str | None = Query(None)) -> Response:
+            return await root_mock(request, query)

+ 1 - 0
requirements.txt

@@ -9,6 +9,7 @@ starlette         ~= 0.31.0
 python-magic      ~= 0.4.27
 uvicorn[standard] ~= 0.23.2
 # Test
+httpx  ~= 0.25.0
 pytest ~= 7.4.0
 # Doc
 sphinx ~= 7.2.6

+ 46 - 0
tests/test_mock.py

@@ -0,0 +1,46 @@
+from pathlib import Path
+
+from fastapi.testclient import TestClient
+from mocka.main import get_application
+from mocka.configuration import Configuration, Server
+
+
+client = TestClient(get_application(Configuration(Server("localhost", 8585))))
+
+
+# def test_read_main():
+#     response = client.get("/", headers={})
+#     assert response.status_code == 201
+#     assert response.json() == {
+#                 "port": "ok",
+#                 "output": {
+#                     "artefact_1": "<uri>",
+#                     "artefact_2": "<uri>",
+#                 }
+#             }
+
+def test_post_main():
+    requirements = (Path(__file__).parent.parent / Path("data/mock_requirements.txt")).read_text()
+    mock_input = {
+        "ctrl": "cout",
+        "input": {
+            "din": {
+                "type": "inline",
+                "content": requirements
+            }
+        }
+    }
+    response = client.post("/", json=mock_input)
+    assert response.status_code == 201
+    assert response.json() == {
+        "ctrl": "ok",
+        "output": {
+            "dout": {
+                "type": "inline",
+                "content": requirements + "\n\nChecked!",
+                "encoding": "text/plain"
+            }
+        }
+    }
+
+