Restructuring due to sonar results 58/4758/1
authorBjornMagnussonXA <bjorn.magnusson@est.tech>
Wed, 23 Sep 2020 06:53:27 +0000 (08:53 +0200)
committerBjornMagnussonXA <bjorn.magnusson@est.tech>
Wed, 23 Sep 2020 07:00:56 +0000 (09:00 +0200)
Issue-ID: NONRTRIC-279

Signed-off-by: BjornMagnussonXA <bjorn.magnusson@est.tech>
Change-Id: If469b5b5f6822abcdd7ee9751e224a66c2fd5184

near-rt-ric-simulator/README.md
near-rt-ric-simulator/tests/test_osc_2_1_0.py [moved from near-rt-ric-simulator/tests/OSC_2.1.0/test_osc_2_1_0.py with 89% similarity]
near-rt-ric-simulator/tests/test_std_1_1_3.py [moved from near-rt-ric-simulator/tests/STD_1.1.3/test_std_1_1_3.py with 93% similarity]
near-rt-ric-simulator/tests/unittest_setup.py [new file with mode: 0644]
tox.ini

index 1789ce1..fc67bb1 100644 (file)
@@ -16,7 +16,8 @@ The overall folder structure is \(relative to the location of this README file\)
 |.                 |Dockerfile and README |
 |api               |The open api yaml for each supported version |
 |src               |Python source code for each supported version |
-|test              |Basic test |
+|test              |Basic test using script|
+|tests             |Basic test using pytest unit test|
 |certificate       |A self-signed certificate and a key
 
 The simulator handles the requests that are defined in the A1 open API yaml file. All these requests are implemented in the a1.py file in the source folder. In addition, a number of administrative functions are also supported and implemented by the main.py in the source folder.
@@ -168,11 +169,11 @@ This script runs a number of tests towards the simulator to make sure it works p
 Basic test, or unit test, using a python script is also supported. This test basically the same thing as the bash script mentioned in the section above. Follow the instruction of how to clone the repo described in that section.
 Only http is tested as the internal flask server is only using http (https is part of the webserver inteface).
 
-Navigate to 'near-rt-ric-simulator/tests'. This location contains one dir for each simulator version. Choose the version to test and go to that sub dir.
+Navigate to 'near-rt-ric-simulator/tests'. Choose the version to test and use that file for test.
 
-Use 'python3 -m unittest' to run unit test only with no coverage check
+Use 'python3 -m pytest <filename>' to run unit test only with no coverage check
 
-Or use 'coverage run  -m unittest' to run unit test and produce coverage data.
+Or use 'coverage run  -m pytest <filename>' to run unit test and produce coverage data.
 List coverage data by 'coverage report -m --include=../../*' - the include flag makes the list to only contain coverage data from the simulator python file.
 
 To use the 'coverage' cmd, coverage need to be installed use 'pip install coverage'
 
 # This test case test the OSC_2.1.0 version of the simulator
 
-import pytest
-import sys
-import os
 import json
 
-
-#Constants for the test case
+#Version of simulator
 INTERFACE_VERSION="OSC_2.1.0"
-PORT_NUMBER="2222"
-HOST_IP="localhost"
-SERVER_URL="http://"+HOST_IP+":"+PORT_NUMBER+"/"
-
-cwd=os.getcwd()+"/"
-# Env TESTS_BASE_PATH is set when executed via tox.ini
-# If basic test is executed from cmd line, that env var is not needed
-if 'TESTS_BASE_PATH' in os.environ:
-     cwd=os.environ['TESTS_BASE_PATH']+"/"+INTERFACE_VERSION+"/"
-TESTDATA=cwd+"/../../test/"+INTERFACE_VERSION+"/jsonfiles/"
-
-#Env var to setup version and host logging
-os.environ['APIPATH'] = cwd+"/../../api/"+INTERFACE_VERSION
-os.environ['REMOTE_HOSTS_LOGGING'] = "ON"
-
-# Paths need to run the sim, including needed source file dirs
-sys.path.append(os.path.abspath(cwd+'../../src/common'))
-sys.path.append(os.path.abspath(cwd+'../../test/common'))
-sys.path.append(os.path.abspath(cwd+'../../src/'+INTERFACE_VERSION))
-os.chdir(cwd+"../../src/"+INTERFACE_VERSION)
-
-import main
-from main import app
-from compare_json import compare
 
-@pytest.fixture
-def client():
-    with app.app.test_client() as c:
-        yield c
+from unittest_setup import SERVER_URL, setup_env, get_testdata_dir, client
+
+#Setup env and import paths
+setup_env(INTERFACE_VERSION)
+
+from compare_json import compare
 
 def test_apis(client):
 
-    # Header for json payload
-    header = {
-        "Content-Type" : "application/json"
-    }
+    testdata=get_testdata_dir()
 
     # Simulator hello world
     response=client.get(SERVER_URL)
     assert response.status_code == 200
 
-
     # Check used and implemented interfaces
     response=client.get(SERVER_URL+'container_interfaces')
     assert response.status_code == 200
@@ -100,14 +70,19 @@ def test_apis(client):
     response=client.get(SERVER_URL+'a1-p/policytypes/1/policies')
     assert response.status_code == 404
 
+    # Header for json payload
+    header = {
+        "Content-Type" : "application/json"
+    }
+
     # API: Put a policy type: 1
-    with open(TESTDATA+'pt1.json') as json_file:
+    with open(testdata+'pt1.json') as json_file:
         policytype_1 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/1', headers=header, data=json.dumps(policytype_1))
         assert response.status_code == 201
 
     # API: Put a policy type: 1 again
-    with open(TESTDATA+'pt1.json') as json_file:
+    with open(testdata+'pt1.json') as json_file:
         policytype_1 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/1', headers=header, data=json.dumps(policytype_1))
         assert response.status_code == 201
@@ -125,7 +100,7 @@ def test_apis(client):
     assert res == True
 
     # API: Put a policy type: 1
-    with open(TESTDATA+'pt1.json') as json_file:
+    with open(testdata+'pt1.json') as json_file:
         policytype_1 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/1', headers=header, data=json.dumps(policytype_1))
         assert response.status_code == 201
@@ -147,13 +122,13 @@ def test_apis(client):
     assert res == True
 
     # API: Create policy instance pi1 of type: 1
-    with open(TESTDATA+'pi1.json') as json_file:
+    with open(testdata+'pi1.json') as json_file:
         policy_1 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/1/policies/pi1', headers=header, data=json.dumps(policy_1))
         assert response.status_code == 202
 
     # API: Get policy instance pi1 of type: 1
-    with open(TESTDATA+'pi1.json') as json_file:
+    with open(testdata+'pi1.json') as json_file:
         policy_1 = json.load(json_file)
         response=client.get(SERVER_URL+'a1-p/policytypes/1/policies/pi1')
         assert response.status_code == 200
@@ -162,13 +137,13 @@ def test_apis(client):
         assert res == True
 
     # API: Update policy instance pi1 of type: 1
-    with open(TESTDATA+'pi1.json') as json_file:
+    with open(testdata+'pi1.json') as json_file:
         policy_1 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/1/policies/pi1', headers=header, data=json.dumps(policy_1))
         assert response.status_code == 202
 
     # API: Update policy type: 1, shall fail
-    with open(TESTDATA+'pt1.json') as json_file:
+    with open(testdata+'pt1.json') as json_file:
         policytype_1 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/1', headers=header, data=json.dumps(policytype_1))
         assert response.status_code == 400
@@ -182,7 +157,7 @@ def test_apis(client):
     assert res == True
 
     # API: Create policy instance pi2 (copy of pi1) of type: 1. Shall fail
-    with open(TESTDATA+'pi1.json') as json_file:
+    with open(testdata+'pi1.json') as json_file:
         policy_2 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/1/policies/pi2', headers=header, data=json.dumps(policy_2))
         assert response.status_code == 400
@@ -208,14 +183,14 @@ def test_apis(client):
     assert res == True
 
     # Load a policy type: 2
-    with open(TESTDATA+'pt2.json') as json_file:
+    with open(testdata+'pt2.json') as json_file:
         policytype_2 = json.load(json_file)
         response=client.put(SERVER_URL+'policytype?id=2', headers=header, data=json.dumps(policytype_2))
         assert response.status_code == 201
         assert response.data == b"Policy type 2 is OK."
 
     # Load a policy type: 2, again
-    with open(TESTDATA+'pt2.json') as json_file:
+    with open(testdata+'pt2.json') as json_file:
         policytype_2 = json.load(json_file)
         response=client.put(SERVER_URL+'policytype?id=2', headers=header, data=json.dumps(policytype_2))
         assert response.status_code == 200
@@ -238,7 +213,7 @@ def test_apis(client):
     assert res == True
 
     # API: Get policy type 2
-    with open(TESTDATA+'pt2.json') as json_file:
+    with open(testdata+'pt2.json') as json_file:
         policytype_2 = json.load(json_file)
         response=client.get(SERVER_URL+'a1-p/policytypes/2')
         assert response.status_code == 200
@@ -259,14 +234,14 @@ def test_apis(client):
     assert res == True
 
     # Load a policy type: 2
-    with open(TESTDATA+'pt2.json') as json_file:
+    with open(testdata+'pt2.json') as json_file:
         policytype_2 = json.load(json_file)
         response=client.put(SERVER_URL+'policytype?id=2', headers=header, data=json.dumps(policytype_2))
         assert response.status_code == 201
         assert response.data == b"Policy type 2 is OK."
 
     # API: Get policy type 2
-    with open(TESTDATA+'pt2.json') as json_file:
+    with open(testdata+'pt2.json') as json_file:
         policytype_2 = json.load(json_file)
         response=client.get(SERVER_URL+'a1-p/policytypes/2')
         assert response.status_code == 200
@@ -283,24 +258,24 @@ def test_apis(client):
     assert res == True
 
     # API: Create policy instance pi1 of type: 2, shall fail
-    with open(TESTDATA+'pi1.json') as json_file:
+    with open(testdata+'pi1.json') as json_file:
         policy_1 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/2/policies/pi1', headers=header, data=json.dumps(policy_1))
         assert response.status_code == 400
 
     # API: Create policy instance pi2 of type: 2. Missing param, shall fail
-    with open(TESTDATA+'pi2_missing_param.json') as json_file:
+    with open(testdata+'pi2_missing_param.json') as json_file:
         policy_2 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/2/policies/pi1', headers=header, data=json.dumps(policy_2))
         assert response.status_code == 400
 
     # API: Create policy instance pi2 of type: 2
-    with open(TESTDATA+'pi2.json') as json_file:
+    with open(testdata+'pi2.json') as json_file:
         policy_2 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/2/policies/pi2', headers=header, data=json.dumps(policy_2))
         assert response.status_code == 202
 
-    with open(TESTDATA+'pi2.json') as json_file:
+    with open(testdata+'pi2.json') as json_file:
         policy_2 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/2/policies/pi2', headers=header, data=json.dumps(policy_2))
         assert response.status_code == 202
@@ -322,7 +297,7 @@ def test_apis(client):
     assert res == True
 
     # API: Create policy instance pi11 (copy of pi1) of type: 1. Shall fail
-    with open(TESTDATA+'pi1.json') as json_file:
+    with open(testdata+'pi1.json') as json_file:
         policy_1 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/1/policies/pi11', headers=header, data=json.dumps(policy_1))
         assert response.status_code == 400
@@ -469,7 +444,7 @@ def test_apis(client):
     response=client.post(SERVER_URL+'forceresponse?code=503')
     assert response.status_code == 200
 
-    with open(TESTDATA+'pi1.json') as json_file:
+    with open(testdata+'pi1.json') as json_file:
         policy_1 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/1/policies/pi11', headers=header, data=json.dumps(policy_1))
         assert response.status_code == 503
@@ -504,7 +479,7 @@ def test_apis(client):
     assert response.status_code == 404
 
     # API: Create policy instance pi80 of type: 5
-    with open(TESTDATA+'pi1.json') as json_file:
+    with open(testdata+'pi1.json') as json_file:
         policy_80 = json.load(json_file)
         response=client.put(SERVER_URL+'a1-p/policytypes/5/policies/pi80', headers=header, data=json.dumps(policy_80))
         assert response.status_code == 404
@@ -523,13 +498,13 @@ def test_apis(client):
     assert response.status_code == 404
 
     # Load policy type, no type in url - shall faill
-    with open(TESTDATA+'pt2.json') as json_file:
+    with open(testdata+'pt2.json') as json_file:
         policytype_2 = json.load(json_file)
         response=client.put(SERVER_URL+'policytype', headers=header, data=json.dumps(policytype_2))
         assert response.status_code == 400
 
     # Load policy type - duplicatee - shall faill
-    with open(TESTDATA+'pt1.json') as json_file:
+    with open(testdata+'pt1.json') as json_file:
         policytype_1 = json.load(json_file)
         response=client.put(SERVER_URL+'policytype?id=2', headers=header, data=json.dumps(policytype_1))
         assert response.status_code == 400
 
 # This test case test the STD_1.1.3 version of the simulator
 
-import pytest
-import sys
-import os
 import json
 
-#Constants for the test case
+#Version of simulator
 INTERFACE_VERSION="STD_1.1.3"
-PORT_NUMBER="2224"
-HOST_IP="localhost"
-SERVER_URL="http://"+HOST_IP+":"+PORT_NUMBER+"/"
-
-cwd=os.getcwd()+"/"
-# Env TESTS_BASE_PATH is set when executed via tox.ini
-# If basic test is executed from cmd line, that env var is not needed
-if 'TESTS_BASE_PATH' in os.environ:
-     cwd=os.environ['TESTS_BASE_PATH']+"/"+INTERFACE_VERSION+"/"
-TESTDATA=cwd+"/../../test/"+INTERFACE_VERSION+"/jsonfiles/"
-
-#Env var to setup api version and host logging
-os.environ['APIPATH'] = cwd+"/../../api/"+INTERFACE_VERSION
-os.environ['REMOTE_HOSTS_LOGGING'] = "ON"
-
-# Paths need to run the sim, including needed source file dirs
-sys.path.append(os.path.abspath(cwd+'../../src/common'))
-sys.path.append(os.path.abspath(cwd+'../../test/common'))
-sys.path.append(os.path.abspath(cwd+'../../src/'+INTERFACE_VERSION))
-os.chdir(cwd+"../../src/"+INTERFACE_VERSION)
-
-import main
-from main import app
-from compare_json import compare
 
-@pytest.fixture
-def client():
-    with app.app.test_client() as c:
-        yield c
+from unittest_setup import SERVER_URL, HOST_IP, PORT_NUMBER, setup_env, client
 
-def test_apis(client):
+#Setup env and import paths
+setup_env(INTERFACE_VERSION)
 
-    # header for json payload
-    header = {
-        "Content-Type" : "application/json"
-    }
+from compare_json import compare
+
+def test_apis(client):
 
     # Simulator hello world
     response=client.get(SERVER_URL)
@@ -99,6 +69,10 @@ def test_apis(client):
             "priorityLevel": 5
         }
     }
+    # header for json payload
+    header = {
+        "Content-Type" : "application/json"
+    }
     response=client.put(SERVER_URL+'A1-P/v1/policies/pi1', headers=header, data=json.dumps(data_pi1))
     assert response.status_code == 201
     result=json.loads(response.data)
diff --git a/near-rt-ric-simulator/tests/unittest_setup.py b/near-rt-ric-simulator/tests/unittest_setup.py
new file mode 100644 (file)
index 0000000..0dbbd22
--- /dev/null
@@ -0,0 +1,58 @@
+#  ============LICENSE_START===============================================
+#  Copyright (C) 2020 Nordix Foundation. All rights reserved.
+#  ========================================================================
+#  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.
+#  ============LICENSE_END=================================================
+#
+
+# Setting up dir and env for unit test of simualators
+import sys
+import os
+import pytest
+
+#Server port and base path
+PORT_NUMBER="2222"
+HOST_IP="localhost"
+SERVER_URL="http://"+HOST_IP+":"+PORT_NUMBER+"/"
+
+#Dir for json test data files
+testdata=""
+
+def setup_env(interface_version):
+    global testdata
+    cwd=os.getcwd()+"/"
+    # Env TESTS_BASE_PATH is set when executed via tox.ini
+    # If basic test is executed from cmd line, that env var is not needed
+    if 'TESTS_BASE_PATH' in os.environ:
+        cwd=os.environ['TESTS_BASE_PATH']+"/"
+    testdata=cwd+"../test/"+interface_version+"/jsonfiles/"
+
+    #Env var to setup version and host logging
+    os.environ['APIPATH'] = cwd+"../api/"+interface_version
+    os.environ['REMOTE_HOSTS_LOGGING'] = "ON"
+
+    # Paths need to run the sim, including needed source file dirs
+    sys.path.append(os.path.abspath(cwd+'../src/common'))
+    sys.path.append(os.path.abspath(cwd+'../test/common'))
+    sys.path.append(os.path.abspath(cwd+'../src/'+interface_version))
+    os.chdir(cwd+"../src/"+interface_version)
+
+def get_testdata_dir():
+    return testdata
+
+#Test client for rest calls
+@pytest.fixture
+def client():
+    from main import app
+    with app.app.test_client() as c:
+        yield c
\ No newline at end of file
diff --git a/tox.ini b/tox.ini
index ba5b8d2..e27e15d 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -38,9 +38,9 @@ commands =
 ; from the previous tests. The first pytest shall not have the 'append' since the it is supposed to erase previous coverage data
 ; from earlier tox run.
     pytest --cov {toxinidir}/near-rt-ric-simulator --cov-report xml --cov-report term-missing --cov-report html --cov-fail-under=70 \
-    {toxinidir}/near-rt-ric-simulator/tests/OSC_2.1.0/test_osc_2_1_0.py
+    {toxinidir}/near-rt-ric-simulator/tests/test_osc_2_1_0.py
     pytest --cov-append --cov {toxinidir}/near-rt-ric-simulator --cov-report xml --cov-report term-missing --cov-report html \
-    --cov-fail-under=70 {toxinidir}/near-rt-ric-simulator/tests/STD_1.1.3/test_std_1_1_3.py
+    --cov-fail-under=70 {toxinidir}/near-rt-ric-simulator/tests/test_std_1_1_3.py
     coverage xml -i
 
 # doc jobs