PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/etlunit-core/src/main/java/org/bitbucket/bradleysmithllc/etlunit/feature/ResourceFeatureOperation.java

https://bitbucket.org/bradleysmithllc/etl-unit
Java | 152 lines | 110 code | 20 blank | 22 comment | 7 complexity | 9f9c28215a4ceab4ba5e654850cd963a MD5 | raw file
  1. package org.bitbucket.bradleysmithllc.etlunit.feature;
  2. /*
  3. * #%L
  4. * etlunit-core
  5. * %%
  6. * Copyright (C) 2010 - 2014 bradleysmithllc
  7. * %%
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * #L%
  20. */
  21. import com.fasterxml.jackson.databind.JsonNode;
  22. import com.github.fge.jackson.JsonLoader;
  23. import com.github.fge.jsonschema.exceptions.ProcessingException;
  24. import com.github.fge.jsonschema.main.JsonSchema;
  25. import com.github.fge.jsonschema.report.ProcessingReport;
  26. import org.bitbucket.bradleysmithllc.etlunit.ExecutionContext;
  27. import org.bitbucket.bradleysmithllc.etlunit.TestAssertionFailure;
  28. import org.bitbucket.bradleysmithllc.etlunit.TestExecutionError;
  29. import org.bitbucket.bradleysmithllc.etlunit.TestWarning;
  30. import org.bitbucket.bradleysmithllc.etlunit.context.VariableContext;
  31. import org.bitbucket.bradleysmithllc.etlunit.parser.ETLTestMethod;
  32. import org.bitbucket.bradleysmithllc.etlunit.parser.ETLTestOperation;
  33. import org.bitbucket.bradleysmithllc.etlunit.parser.ETLTestValueObject;
  34. import javax.annotation.Nullable;
  35. import java.io.IOException;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. public class ResourceFeatureOperation implements FeatureOperation
  39. {
  40. private final String name;
  41. private final String description;
  42. private final ResourceFeatureMetaInfo resourceFeatureMetaInfo;
  43. private final List<String> signatures;
  44. private final List<FeatureOperationSignature> operationSignatures = new ArrayList<FeatureOperationSignature>();
  45. public static final JsonNode NULL_NODE;
  46. static
  47. {
  48. try
  49. {
  50. NULL_NODE = JsonLoader.fromString("{}");
  51. }
  52. catch (IOException e)
  53. {
  54. throw new RuntimeException(e);
  55. }
  56. }
  57. public ResourceFeatureOperation(ResourceFeatureMetaInfo resourceFeatureMetaInfo, String key, String description, List<String> signatur)
  58. {
  59. name = key;
  60. this.description = description;
  61. this.resourceFeatureMetaInfo = resourceFeatureMetaInfo;
  62. this.signatures = signatur;
  63. for (String sig : signatures)
  64. {
  65. operationSignatures.add(new ResourceFeatureOperationSignature(this, resourceFeatureMetaInfo, sig));
  66. }
  67. }
  68. @Override
  69. public String getName()
  70. {
  71. return name;
  72. }
  73. @Override
  74. public String getDescription()
  75. {
  76. return description;
  77. }
  78. @Override
  79. public String getUsage()
  80. {
  81. String val = resourceFeatureMetaInfo.getResource(name + ".usage");
  82. return val;
  83. }
  84. @Override
  85. public String getPrototype()
  86. {
  87. String val = resourceFeatureMetaInfo.getResource(name + ".prototype");
  88. return val;
  89. }
  90. @Override
  91. public List<FeatureOperationSignature> getSignatures()
  92. {
  93. return operationSignatures;
  94. }
  95. @Override
  96. public action_code process(@Nullable ETLTestMethod mt, ETLTestOperation op, ETLTestValueObject parameters, VariableContext vcontext, ExecutionContext econtext, int executor) throws TestAssertionFailure, TestExecutionError, TestWarning
  97. {
  98. if (name.equals(op.getOperationName()))
  99. {
  100. for (FeatureOperationSignature sig : operationSignatures)
  101. {
  102. JsonSchema schema = sig.getValidator();
  103. if (schema != null)
  104. {
  105. resourceFeatureMetaInfo.getApplicationLog().info("Verifying operation request '" + op.getOperationName() + "' against the schema signature '" + resourceFeatureMetaInfo.getDescribing().getFeatureName() + "." + sig.getId() + "'");
  106. try
  107. {
  108. ProcessingReport report = schema.validate(parameters != null ? parameters.getJsonNode() : NULL_NODE);
  109. if (report.isSuccess())
  110. {
  111. return sig.process(mt, op, parameters, vcontext, econtext, executor);
  112. }
  113. else
  114. {
  115. //System.out.println(report.toString());
  116. }
  117. }
  118. catch (ProcessingException e)
  119. {
  120. // didn't validate - pass on to the next one
  121. }
  122. }
  123. else
  124. {
  125. // no schema, this one wins
  126. return sig.process(mt, op, parameters, vcontext, econtext, executor);
  127. }
  128. }
  129. }
  130. return action_code.defer;
  131. }
  132. }