From 15c9f627f79fd9b7dc682605fe164035f9e60881 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Thu, 11 Apr 2019 11:03:25 +0200
Subject: [PATCH] test coriant response against schema

---
 inventory_provider/db/opsdb.py          | 32 ++++++++++---
 inventory_provider/routes/classifier.py |  3 ++
 test/test_opsdb_queries.py              | 60 +++++++++++++++++++++++--
 3 files changed, 87 insertions(+), 8 deletions(-)

diff --git a/inventory_provider/db/opsdb.py b/inventory_provider/db/opsdb.py
index 35fae900..1c0b155c 100644
--- a/inventory_provider/db/opsdb.py
+++ b/inventory_provider/db/opsdb.py
@@ -254,14 +254,23 @@ SELECT
     vcc.circuit_type,
     vcc.service_type,
     vcc.peering_type,
+    vcc.status,
     eq_a.name as equipment_name_a,
     eq_b.name as equipment_name_b,
     eqc_a.card_id as card_id_a,
     eqc_a.card_id as card_id_b,
     pop_a.name as pop_name_a,
     pop_b.name as pop_name_b,
-    vcc.port_a as port_a,
-    vcc.port_b as port_b
+    pop_a.abbreviation as pop_abbreviation_a,
+    pop_b.abbreviation as pop_abbreviation_b,
+    pop_a.absid as pop_absid_a,
+    pop_b.absid as pop_absid_b,
+    pop_a.city as pop_city_a,
+    pop_b.city as pop_city_b,
+    pop_a.country as pop_country_a,
+    pop_b.country as pop_country_b,
+    vcc.port_a,
+    vcc.port_b
 
  FROM vcircuitconns vcc
     LEFT JOIN equipment eq_a ON eq_a.absid = vcc.PTR_equip_a
@@ -279,22 +288,35 @@ SELECT
             'circuit_type': row['circuit_type'],
             'service_type': row['service_type'],
             'peering_type': row['peering_type'],
+            'status': row['status'],
             'a': {
                 'name': row['equipment_name_a'],
                 'card id': row['card_id_a'],
                 'port number': row['port_a'],
-                'pop': row['pop_name_a']
+                'pop': {
+                    'absid': row['pop_absid_a'],
+                    'name': row['pop_name_a'],
+                    'city': row['pop_city_a'],
+                    'country': row['pop_country_a'],
+                    'abbreviation': row['pop_abbreviation_a']
+                }
             },
             'b': {
                 'name': row['equipment_name_b'],
                 'card id': row['card_id_b'],
                 'port number': row['port_b'],
-                'pop': row['pop_name_b']
+                'pop': {
+                    'absid': row['pop_absid_b'],
+                    'name': row['pop_name_b'],
+                    'city': row['pop_city_b'],
+                    'country': row['pop_country_b'],
+                    'abbreviation': row['pop_abbreviation_b']
+                }
             }
         }
 
     circuit_query = base_query + """
-WHERE vcc.status = 'Operational'
+WHERE vcc.status <> 'Terminated'
     AND (
         (eq_a.name = %(equipment_name)s
             AND eqc_a.card_id = %(card_id)s
diff --git a/inventory_provider/routes/classifier.py b/inventory_provider/routes/classifier.py
index 1a942124..b20be5b9 100644
--- a/inventory_provider/routes/classifier.py
+++ b/inventory_provider/routes/classifier.py
@@ -294,7 +294,9 @@ def get_coriant_info(equipment_name, card_id, port_number):
     if result:
         result = result.decode('utf-8')
     else:
+
         config = current_app.config['INVENTORY_PROVIDER_CONFIG']
+
         with db.connection(config['ops-db']) as cx:
             path = opsdb.get_coriant_path(
                 cx, equipment_name, card_id, port_number)
@@ -307,6 +309,7 @@ def get_coriant_info(equipment_name, card_id, port_number):
                 mimetype="text/html")
 
         result = json.dumps({'path': path})
+
         # cache this data for the next call
         r.set(cache_key, result.encode('utf-8'))
 
diff --git a/test/test_opsdb_queries.py b/test/test_opsdb_queries.py
index 5fbada77..9cbc752c 100644
--- a/test/test_opsdb_queries.py
+++ b/test/test_opsdb_queries.py
@@ -1,6 +1,8 @@
 import os
 import pytest
 
+import jsonschema
+
 from inventory_provider.db import db
 from inventory_provider.db import opsdb
 
@@ -35,6 +37,60 @@ def connection(db_params):
         yield c
 
 
+CORIANT_PATH_METADATA = {
+    "$schema": "http://json-schema.org/draft-07/schema#",
+    "type": "object",
+
+    "definitions": {
+        "pop-info": {
+            "type": "object",
+            "properties": {
+                "absid": {"type": "integer"},
+                "name": {"type": "string"},
+                "abbreviation": {"type": "string"},
+                "country": {"type": "string"},
+                "city": {"type": "string"}
+            },
+            "required": ["absid", "name", "abbreviation", "country", "city"],
+            "additionalProperties": False
+        },
+        "endpoint": {
+            "type": "object",
+            "properties": {
+                "name": {"type": "string"},
+                "card id": {"type": "string"},
+                "port number": {"type": "string"},
+                "pop": {"$ref": "#/definitions/pop-info"}
+            },
+            "required": ["name", "port number", "pop"],
+            "additionalProperties": False
+        }
+    },
+
+    "type": "object",
+    "properties": {
+        'absid': {"type": "integer"},
+        'category': {"type": "string"},
+        'circuit_type': {"type": "string"},
+        'service_type': {"type": "string"},
+        'peering_type': {"type": "string"},
+        'status': {"type": "string"},
+        'a': {"$ref": "#/definitions/endpoint"},
+        'b': {"$ref": "#/definitions/endpoint"}
+    },
+    "required": [
+        "absid",
+        "category",
+        "circuit_type",
+        "service_type",
+        "peering_type",
+        "status",
+        "a",
+        "b"],
+    "additionalProperties": False
+}
+
+
 @pytest.mark.parametrize('equipment,card,port', [
     ('grv3.lon.uk.geant.net', '1-1', '3'),
     ('grv3.lon.uk.geant.net', '1-1', '5'),
@@ -43,6 +99,4 @@ def connection(db_params):
 ])
 def test_query(connection, equipment, card, port):
     circuit = opsdb.get_coriant_path(connection, equipment, card, port)
-
-    from pprint import pprint
-    pprint(circuit)
+    jsonschema.validate(circuit, CORIANT_PATH_METADATA)
-- 
GitLab