from __future__ import absolute_import

import os
import json

from jsonschema import validate, SchemaError, ValidationError

from app.validation import validate_json


def test_all_schemas_are_valid():
    for file_name in os.listdir('json_schemas'):
        if os.path.isfile('json_schemas/%s' % file_name):
            print('Testing schema: %s' % file_name)
            with open('json_schemas/%s' % file_name) as json_schema_file:
                assert check_schema(json.load(json_schema_file))


def test_example_json_validates_correctly():
    with open('example_listings/SSP-JSON-SCS.json') as json_file:
        json_scs = json.load(json_file)
        assert validate_json(json_scs) == 'G6-SCS'

    with open('example_listings/SSP-JSON-SAAS.json') as json_file:
        json_scs = json.load(json_file)
        assert validate_json(json_scs) == 'G6-SaaS'

    with open('example_listings/SSP-JSON-PAAS.json') as json_file:
        json_scs = json.load(json_file)
        assert validate_json(json_scs) == 'G6-PaaS'

    with open('example_listings/SSP-JSON-IAAS.json') as json_file:
        json_scs = json.load(json_file)
        assert validate_json(json_scs) == 'G6-IaaS'

    with open('example_listings/SSP-INVALID.json') as json_file:
        json_scs = json.load(json_file)
        assert validate_json(json_scs) is False


def check_schema(schema):
    try:
        validate({}, schema)
    except SchemaError as ex:
        print('Invalid JSON schema: %s' % ex.message)
        return False
    except ValidationError:
        return True
    else:
        return True

@@ -56,16 +56,66 @@
 t json%0A%0A
+from nose.tools import assert_equal, assert_false%0A
 from jso
@@ -212,16 +212,176 @@
 _json%0A%0A%0A
+EXAMPLE_LISTING_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),%0A                                                    '..', 'example_listings'))%0A%0A%0A
 def test
@@ -751,35 +751,30 @@
 ctly():%0A    
-with open('
+check_
 example_list
@@ -768,34 +768,34 @@
 _example_listing
-s/
+(%22
 SSP-JSON-SCS.jso
@@ -794,137 +794,45 @@
 -SCS
-.json') as json_file:%0A        json_scs = json.load(json_file)%0A        assert validate_json(json_scs) == '
+%22, assert_equal, %22
 G6-SCS
-'%0A
+%22)
 %0A    
-with open('
+check_
 exam
@@ -834,34 +834,34 @@
 _example_listing
-s/
+(%22
 SSP-JSON-SAAS.js
@@ -858,141 +858,49 @@
 ON-S
-AAS.json') as json_file:%0A        json_scs = json.load(json_file)%0A        assert validate_json(json_scs) == '
+aaS%22, assert_equal, %22
 G6-SaaS
-'%0A
+%22)
 %0A    
-with open('
+check_
 exam
@@ -902,34 +902,34 @@
 _example_listing
-s/
+(%22
 SSP-JSON-PAAS.js
@@ -926,141 +926,117 @@
 ON-P
-AAS.json') as json_file:%0A        json_scs = json.load(json_file)%0A        assert validate_json(json_scs) == 'G6-PaaS'%0A
+aaS%22, assert_equal, %22G6-PaaS%22)%0A    check_example_listing(%22SSP-JSON-IaaS%22, assert_equal, %22G6-IaaS%22)
 %0A    
-with open('
+check_
 exam
@@ -1050,187 +1050,195 @@
 ting
-s/
+(%22
 SSP-
-JSON-IAAS.json') as json_file:%0A        json_scs = json.load(json_file)%0A        assert validate_json(json_scs) == 'G6-IaaS'%0A%0A    with open('example_listings/SSP-INVALID.json'
+INVALID%22, assert_false)%0A%0A%0Adef check_example_listing(name, assertion, *args):%0A    listing_path = os.path.join(EXAMPLE_LISTING_PATH, '%7B%7D.json'.format(name))%0A    with open(listing_path
 ) as
@@ -1262,19 +1262,20 @@
    json_
-scs
+data
  = json.
@@ -1304,17 +1304,20 @@
   assert
- 
+ion(
 validate
@@ -1331,21 +1331,21 @@
 son_
-scs) is False
+data), *args)
 %0A%0A%0Ad

Set absolute path to example listings