Example #1
0
 def testReadConfig(self):
     modelFile = 'resources/configurations/java_bean_example.json'
     modelFileExists = os.path.isfile(modelFile)
     self.assertTrue('model file exists: ' + modelFile, modelFileExists)
     modelDictArray = getParsedSchemaFromJson(modelFile)
     configJob = config.Job(modelDictArray[0])
     self.assertIsNotNone(configJob)
Example #2
0
def convertModel(model, dryRun, destDir):
    parsedSchema = builder.getParsedSchemaFromJson(model)
    traverseDictAndReplaceRefExtensions(parsedSchema, True)
    if dryRun:
        print(yaml.dump(parsedSchema, sort_keys=False))
    else:
        _printYaml(parsedSchema, model, destDir)
Example #3
0
def main():
    args = parser.parse_args()
    if not doesFileExist(args.model):
        printError('\nModel file not found ... cancel: {}'.format(args.model))
        sys.exit(1)
    if not _checkValidVersion(args.version):
        printError(
            '\nNo valid version argument was given, check the help: {}'.format(
                args.version))
        sys.exit(1)
    if not _checkDirToCheckForRefs(args.dirToCheckForRefs):
        printError(
            "\nThe given directory to check for references isn't a valid dir: {}"
            .format(args.dirToCheckForRefs))
        sys.exit(1)
    parsedSchema = builder.getParsedSchemaFromJson(args.model)
    currentVersion = parsedSchema.get("version", None)
    if currentVersion is None:
        printInfo('\nModel file does not contain a version: {}'.format(
            args.model))
        sys.exit(0)
    if not _checkValidVersion(currentVersion):
        printError(
            '\nCurrent version is no valid semver: {}'.format(currentVersion))
        sys.exit(1)
    newVersion = _calcNewVersion(currentVersion, args.version, True)
    logging.info("modelFile: {}, currentVersion: {}, newVersion: {}".format(
        args.model, currentVersion, newVersion))
    _printOutput(args.model, args.backupExt, args.dryRun, newVersion,
                 parsedSchema, currentVersion)
    if args.dirToCheckForRefs is not None:
        filesToCheckList = _getJsonSchemaFileNames(args.dirToCheckForRefs)
        _checkForReferences(args, newVersion, args.model, filesToCheckList,
                            [args.model])
Example #4
0
 def test_getOpenApiTags(self):
     modelFile = 'tests/resources/models/json/examples/openapi_v3_example_small.json'
     parsedSchema = dictionaryBuilder.getParsedSchemaFromJson(modelFile)
     modelTypes = dictionaryBuilder.extractTypes(parsedSchema, modelFile, [])
     self.assertIsNotNone(modelTypes)
     self.assertEqual(15, len(modelTypes))
     tags = modelFuncs.getOpenApiTags(modelTypes)
     self.assertEqual(1, len(tags))
Example #5
0
 def test_separateOpenApiPathTypes(self):
     modelFile = 'tests/resources/models/json/examples/openapi_v3_example_small.json'
     parsedSchema = dictionaryBuilder.getParsedSchemaFromJson(modelFile)
     modelTypes = dictionaryBuilder.extractTypes(parsedSchema, modelFile, [])
     self.assertIsNotNone(modelTypes)
     self.assertEqual(15, len(modelTypes))
     (pathTypes, otherTypes, enumTypes) = modelFuncs.separateOpenApiPathTypes(modelTypes)
     self.assertEqual(4, len(pathTypes))
     self.assertEqual(6, len(otherTypes))
     self.assertEqual(5, len(enumTypes))
Example #6
0
 def __doTest(self,
              modelFile,
              skipScopeTest,
              requestBodyMimeTypeCanBeNone=False):
     modelFileExists = os.path.isfile(modelFile)
     self.assertTrue('model file exists: ' + modelFile, modelFileExists)
     parsedSchema = dictionaryBuilder.getParsedSchemaFromJson(modelFile)
     modelTypes = dictionaryBuilder.extractTypes(parsedSchema, modelFile,
                                                 [])
     self.assertIsNotNone(modelTypes)
     self.assertEqual(18, len(modelTypes))
     pathTypes = []
     coreModelTypes = []
     foundScopedCommand = False
     for type in modelTypes:
         if isinstance(type, openapi.PathType):
             pathTypes.append(type)
         else:
             coreModelTypes.append(type)
     self.assertEqual(4, len(pathTypes))
     for path in pathTypes:
         self.assertTrue(len(path.commands) > 0)
         self.assertIsNotNone(path.pathPattern)
         for command in path.commands:
             self.assertIsNotNone(command.command)
             self.assertTrue(len(command.tags) > 0)
             self.assertTrue((len(command.parameters) > 0)
                             or (command.requestBody is not None))
             for param in command.parameters:
                 self.assertIsNotNone(param.name)
                 self.assertIsNotNone(param.type)
             if command.requestBody is not None:
                 self._checkContent(command.requestBody,
                                    requestBodyMimeTypeCanBeNone)
             self.assertTrue(len(command.responses) > 0)
             for response in command.responses:
                 self.assertIsNotNone(response.returnCode)
                 if response.returnCode == '200':
                     self._checkContent(response,
                                        requestBodyMimeTypeCanBeNone)
             if skipScopeTest:
                 continue
             if (path.pathPattern == '/pet/{petId}') and (
                     command.command == openapi.CommandCommandEnum.GET):
                 foundScopedCommand = True
                 self.assertEqual(2, len(command.security.scopes))
                 self.assertEqual('scope1', command.security.scopes[0])
                 self.assertEqual('scope2', command.security.scopes[1])
     if not skipScopeTest:
         self.assertTrue(foundScopedCommand)
Example #7
0
    def _testYamlAndJsonEquality(self, jsonModelFile, yamlModelFile):
        jsonModelFileExists = os.path.isfile(jsonModelFile)
        self.assertTrue('json model file exists: ' + jsonModelFile,
                        jsonModelFileExists)

        yamlModelFileExists = os.path.isfile(yamlModelFile)
        self.assertTrue('yaml model file exists: ' + yamlModelFile,
                        yamlModelFileExists)

        parsedJsonSchema = getParsedSchemaFromJson(jsonModelFile)
        self.assertIsNotNone(parsedJsonSchema)
        parsedYamlSchema = getParsedSchemaFromYaml(yamlModelFile)
        self.assertIsNotNone(parsedYamlSchema)

        self.assertEqual(parsedYamlSchema, parsedYamlSchema)
Example #8
0
def _checkForReferences(args, newVersion, modelFile, filesToCheckList,
                        alreadyCheckedList):
    """This function search for all json files in the configured dirToCheckForRefs.
    In the found json files it looks for references to the originally changed model file
    and increments there the version. The following rules are processed:
    1. If the new version was given by [major, minor, patch], then the version of the
        referencing files is incremented in the same way.
    2. If the new version was given as semver, then the major version of referencing
        files is incremented

    Keyword arguments:
    args -- command line arguments
    newVersion -- version that was set for the first model in the command
    modelFile -- model files that could be referenced
    filesToCheckList -- list of files that have to be tests for references
    alreadyCheckedList -- list with all files that are already checked ... to avoid circular dependencies
    """

    newModelsToCheck = []
    for file in filesToCheckList:
        if file in alreadyCheckedList:
            continue
        parsedSchema = builder.getParsedSchemaFromJson(file)
        # search form modelFileReference
        if not isinstance(parsedSchema, dict):
            continue
        currentVersion = parsedSchema.get("version", None)
        if currentVersion is None:
            continue
        logging.info("checking {} for references ...".format(file))
        if _hasModelReference(parsedSchema, file, modelFile):
            logging.info("      ... found")
            newVersion = _calcNewVersion(currentVersion, args.version, False)
            _printOutput(file, args.backupExt, args.dryRun, newVersion,
                         parsedSchema, currentVersion)
            if file not in newModelsToCheck:
                newModelsToCheck.append(file)
            alreadyCheckedList.append(file)
    for model in newModelsToCheck:
        _checkForReferences(args, newVersion, model, filesToCheckList,
                            alreadyCheckedList)
Example #9
0
 def __doTest(self, modelFile, requestBodyMimeTypeCanBeNone=False):
     modelFileExists = os.path.isfile(modelFile)
     self.assertTrue('model file exists: ' + modelFile, modelFileExists)
     parsedSchema = dictionaryBuilder.getParsedSchemaFromJson(modelFile)
     modelTypes = dictionaryBuilder.extractTypes(parsedSchema, modelFile,
                                                 [])
     self.assertIsNotNone(modelTypes)
     self.assertEqual(15, len(modelTypes))
     pathTypes = []
     coreModelTypes = []
     for type in modelTypes:
         if isinstance(type, openapi.PathType):
             pathTypes.append(type)
         else:
             coreModelTypes.append(type)
     self.assertEqual(4, len(pathTypes))
     for path in pathTypes:
         self.assertTrue(len(path.commands) > 0)
         self.assertIsNotNone(path.pathPattern)
         for command in path.commands:
             self.assertIsNotNone(command.command)
             self.assertTrue(len(command.tags) > 0)
             self.assertTrue((len(command.parameters) > 0)
                             or (command.requestBody is not None))
             for param in command.parameters:
                 self.assertIsNotNone(param.name)
                 self.assertIsNotNone(param.type)
             if command.requestBody is not None:
                 self._checkContent(command.requestBody,
                                    requestBodyMimeTypeCanBeNone)
             self.assertTrue(len(command.responses) > 0)
             for response in command.responses:
                 self.assertIsNotNone(response.returnCode)
                 if response.returnCode == '200':
                     self._checkContent(response,
                                        requestBodyMimeTypeCanBeNone)