test_create_energistics_manifest.py 4.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#  Copyright 2021 Google LLC
#  Copyright 2021 EPAM Systems
#  Copyright 2021 Energistics
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.


import json
import os

import jsonschema
import pytest
import create_energistics_manifest
from data_file_paths import TRAJECTORY_XML_FILE_PATH, MANIFEST_SCHEMA_FILE_PATH
25
26
27
from osdu_ingestion.libs.refresh_token import BaseTokenRefresher
from osdu_ingestion.libs.context import Context
from osdu_ingestion.libs.handle_file import FileHandler
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

MOCK_EXECUTION_CONTEXT = {
    "Context": {
        "acl": {
            "viewers": [
                "data.default.viewers@test.osdu-gcp.go3-nrg.projects.epam.com"
            ],
            "owners": [
                "data.default.owners@test.osdu-gcp.go3-nrg.projects.epam.com"
            ]
        },
        "legal": {
            "legaltags": [
                "test-demo-legaltag"
            ],
            "otherRelevantDataCountries": [
                "US"
            ],
            "status": "compliant"
        },
        "kind": "test:wks:dataset--File.Generic:1.0.0",
        "file_name": "trace_trajectory",
        "preload_file_path": "gs://energistics-test-data/energistics_witsml_data_Trajectory.xml",
        "version": 5
    }
}


class MockSignedUrl:

    @property
    def signed_url(self):
60
        return "https://test/test-host/test-area/test-location-some-hash/test-another-cash?test-other-data"
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80


class TestCreateEnergisticsManifest:

    @pytest.fixture(autouse=True)
    def cloud_provider(self):
        os.environ["CLOUD_PROVIDER"] = "gcp"

    @pytest.fixture
    def mock_get_file_content(self, monkeypatch, file_path):
        with open(file_path) as f:
            file_content = f.read()
        monkeypatch.setattr(create_energistics_manifest, "get_file_content", lambda *args, kwargs: file_content)
        return file_content

    @pytest.fixture
    def mock_file_handler(self, monkeypatch):
        token_refresher = BaseTokenRefresher(creds="test")
        context = Context("test", "test")
        monkeypatch.setattr(FileHandler, "save_file_record", lambda *args, **kwargs: "test")
81
82
        monkeypatch.setattr(FileHandler, "upload_file",
                            lambda *args, **kwargs: "/test-location-some-hash/test-another-cash")
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        monkeypatch.setattr(FileHandler, "_get_download_signed_url", lambda *args, **kwargs: MockSignedUrl())
        file_handler = FileHandler(
            file_service_host="",
            token_refresher=token_refresher,
            context=context,
            blob_storage_client="test"
        )
        return file_handler

    @pytest.mark.parametrize(
        "xml_file",
        [
            pytest.param(
                TRAJECTORY_XML_FILE_PATH,
                id="Trajectory XML"),
        ]
    )
    def test_create_manifest_from_xml(self, monkeypatch, mock_file_handler, xml_file):
        """
        Test if a Manifest is created from XML and passes validation against GenericManifestSchema

        :param monkeypatch:
        :param mock_file_handler:
        :param xml_file:
        :return:
        """

        def get_file_content(file):
            with open(file) as f:
112
                return f.read()
113
114
115
116
117
118

        with open(MANIFEST_SCHEMA_FILE_PATH) as f:
            schema = json.load(f)

        monkeypatch.setattr(create_energistics_manifest, "get_file_content",
                            lambda *args, **kwargs: get_file_content(xml_file))
119
        token_refresher = BaseTokenRefresher(creds="test")
120
        context = Context("test", "test")
121
        manifest_creator = create_energistics_manifest.EnergisticsManifestCreator(mock_file_handler, token_refresher, context)
122
123
124
        manifest = manifest_creator.create_manifest(MOCK_EXECUTION_CONTEXT)

        jsonschema.validate(manifest, schema)