All files / src/models/inspect java-inspection.model.ts

84.95% Statements 79/93
80.82% Branches 59/73
100% Functions 7/7
81.33% Lines 61/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195                              9x 9x         9x 9x 9x                             9x 3x   9x 1x                       1x     9x   3x 1x           1x     2x   3x 3x             3x 2x   3x 2x   2x 2x         3x       3x 3x             45x 45x       9x       201x 201x 8x     193x 192x     193x 161x     32x 32x 32x 20x 20x 20x 20x 20x       32x 156x 156x       9x   9x 3x 3x 9x   9x 3x 3x 3x                     3x 3x                       3x               3x                 9x  
/*
    Copyright (C) 2017 Red Hat, Inc.
 
    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 { CollectionType, FieldType } from '../../contracts/common';
import {
  DocumentInspectionModel,
  DocumentInspectionRequestModel,
  DocumentInspectionRequestOptions,
} from './document-inspection.model';
import { EnumValue, Field } from '../field.model';
import { ErrorInfo, ErrorLevel, ErrorScope, ErrorType } from '../error.model';
import {
  IClassInspectionRequestContainer,
  IClassInspectionResponseContainer,
  IJavaClass,
  IJavaClassContainer,
  IJavaField,
  JAVA_INSPECTION_REQUEST_JSON_TYPE,
} from '../../contracts/documents/java';
 
import { ConfigModel } from '../config.model';
import { DocumentDefinition } from '../document-definition.model';
 
/**
 * Encapsulates Java class inspection context.
 */
export class JavaInspectionModel extends DocumentInspectionModel {
  request = new JavaInspectionRequestModel(this.cfg, this.doc);
 
  isOnlineInspectionCapable(): boolean {
    Iif (this.cfg.initCfg.baseJavaInspectionServiceUrl == null) {
      this.cfg.errorService.addError(
        new ErrorInfo({
          message: `Java inspection service is not configured. Document will not be loaded: ${this.doc.name}`,
          level: ErrorLevel.WARN,
          scope: ErrorScope.APPLICATION,
          type: ErrorType.INTERNAL,
          object: this.doc,
        })
      );
      return false;
    }
    return true;
  }
 
  parseResponse(responseJson: any): void {
    let javaClass: IJavaClass;
    if (responseJson.ClassInspectionResponse) {
      Iif (responseJson.errorMessage) {
        this.doc.errorOccurred = true;
        throw new Error(
          `Could not load JSON document, error: ${responseJson.errorMessage}`
        );
      }
      javaClass = (responseJson as IClassInspectionResponseContainer)
        .ClassInspectionResponse.javaClass;
    } else {
      javaClass = (responseJson as IJavaClassContainer).JavaClass;
    }
    const docIdentifier: string = this.doc.id;
    Iif (!javaClass || javaClass.status === 'NOT_FOUND') {
      this.doc.errorOccurred = true;
      throw new Error(
        `Could not load JAVA document. Document is not found: ${docIdentifier}`
      );
    }
 
    if (!this.doc.description) {
      this.doc.description = javaClass.className;
    }
    if (!this.doc.name) {
      this.doc.name = javaClass.className!;
      // Make doc name the class name rather than fully qualified name
      Eif (this.doc.name && this.doc.name.indexOf('.') !== -1) {
        this.doc.name = this.doc.name.substr(
          this.doc.name.lastIndexOf('.') + 1
        );
      }
    }
    Iif (javaClass.uri && (!this.doc.uri || this.doc.uri.length === 0)) {
      this.doc.uri = javaClass.uri;
    }
 
    let rootField = null;
    Iif (
      javaClass.collectionType &&
      javaClass.collectionType !== CollectionType.NONE.valueOf()
    ) {
      this.parseJavaFieldFromDocument(javaClass, null);
      rootField = this.doc.fields[0];
    }
    for (const field of javaClass.javaFields.javaField) {
      this.parseJavaFieldFromDocument(field, rootField);
    }
  }
 
  private parseJavaFieldFromDocument(
    field: IJavaField,
    parentField: Field | null
  ): void {
    const parsedField = this.parseFieldFromDocument(field, parentField);
    if (parsedField == null) {
      return;
    }
 
    if (field.className) {
      parsedField.classIdentifier = field.className;
    }
 
    if (field.fieldType !== FieldType.COMPLEX) {
      return;
    }
 
    const javaClass = field as IJavaClass;
    parsedField.enumeration = javaClass.enumeration;
    if (javaClass.enumeration && javaClass.javaEnumFields?.javaEnumField) {
      for (const enumValue of javaClass.javaEnumFields.javaEnumField) {
        const parsedEnumValue: EnumValue = new EnumValue();
        parsedEnumValue.name = enumValue.name;
        parsedEnumValue.ordinal = enumValue.ordinal;
        parsedField.enumValues.push(parsedEnumValue);
      }
    }
 
    if (javaClass.javaFields?.javaField?.length) {
      for (const childField of javaClass.javaFields.javaField) {
        this.parseJavaFieldFromDocument(childField, parsedField);
      }
    }
  }
}
 
export class JavaInspectionRequestModel extends DocumentInspectionRequestModel {
  url = this.cfg.initCfg.baseJavaInspectionServiceUrl + 'class';
  options = new JavaInspectionRequestOptions(this.cfg, this.doc);
}
 
export class JavaInspectionRequestOptions extends DocumentInspectionRequestOptions {
  constructor(protected cfg: ConfigModel, protected doc: DocumentDefinition) {
    super(cfg, doc);
    const request: IClassInspectionRequestContainer = {
      ClassInspectionRequest: {
        jsonType: JAVA_INSPECTION_REQUEST_JSON_TYPE,
        className: this.doc.inspectionSource,
        disablePrivateOnlyFields: this.cfg.initCfg.disablePrivateOnlyFields,
        disableProtectedOnlyFields: this.cfg.initCfg.disableProtectedOnlyFields,
        disablePublicOnlyFields: this.cfg.initCfg.disablePublicOnlyFields,
        disablePublicGetterSetterFields:
          this.cfg.initCfg.disablePublicGetterSetterFields,
      },
    };
    this.json = request;
    Iif (
      this.doc.initModel.collectionType &&
      (this.doc.initModel.collectionType as CollectionType) !==
        CollectionType.NONE
    ) {
      request.ClassInspectionRequest.collectionType =
        this.doc.initModel.collectionType;
      if (this.doc.initModel.collectionClassName) {
        request.ClassInspectionRequest.collectionClassName =
          this.doc.initModel.collectionClassName;
      }
    }
    Iif (
      this.cfg.initCfg.fieldNameExclusions &&
      this.cfg.initCfg.fieldNameExclusions.length
    ) {
      request.ClassInspectionRequest.fieldNameExclusions = {
        string: this.cfg.initCfg.fieldNameExclusions,
      };
    }
    Iif (
      this.cfg.initCfg.classNameExclusions &&
      this.cfg.initCfg.classNameExclusions.length
    ) {
      request.ClassInspectionRequest.classNameExclusions = {
        string: this.cfg.initCfg.classNameExclusions,
      };
    }
  }
}