|
| 1 | +{{! |
| 2 | + Copyright (c) 2022-Present, Okta, Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +}} |
| 16 | +{{>licenseInfo}} |
| 17 | +package {{package}}; |
| 18 | + |
| 19 | +import {{invokerPackage}}.ApiException; |
| 20 | +import {{modelPackage}}.*; |
| 21 | +import com.okta.sdk.helper.PresetHelper; |
| 22 | +import com.okta.sdk.helper.TerraformHelper; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.DisplayName; |
| 26 | + |
| 27 | +{{#imports}} |
| 28 | +import {{import}}; |
| 29 | +{{/imports}} |
| 30 | + |
| 31 | +import java.util.*; |
| 32 | + |
| 33 | +/** |
| 34 | + * Auto-generated unit tests for {{classname}} operations |
| 35 | + * |
| 36 | + * NOTE: This test class requires Terraform-generated prerequisite data to run. |
| 37 | + * Tests will fail if the TF_OUTPUTS environment variable is not set with the |
| 38 | + * required test prerequisite data. |
| 39 | + */ |
| 40 | +public class {{classname}}Test { |
| 41 | +
|
| 42 | + private {{classname}} apiClient; |
| 43 | + private PresetHelper presetHelper; |
| 44 | + private TerraformHelper terraformHelper; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + public void setUp() { |
| 48 | + presetHelper = new PresetHelper(); |
| 49 | + terraformHelper = new TerraformHelper(); |
| 50 | + apiClient = new {{classname}}(presetHelper.getApiClient()); |
| 51 | + } |
| 52 | + |
| 53 | +{{#operations}} |
| 54 | +{{#operation}} |
| 55 | + /** |
| 56 | + * Test case for {{operationId}} operation. |
| 57 | + * |
| 58 | + * This test executes the actual API method with parameters extracted from |
| 59 | + * prerequisite data created by Terraform. It validates HTTP response codes. |
| 60 | + * |
| 61 | + * Method name: test_{{operationId}} |
| 62 | + */ |
| 63 | + @Test |
| 64 | + @DisplayName("Test {{operationId}} operation") |
| 65 | + @SuppressWarnings("unchecked") |
| 66 | + public void test_{{operationId}}() throws Exception { |
| 67 | + // Load prerequisite data from Terraform |
| 68 | + Map<String, Object> prerequisiteData = terraformHelper.getPrerequisiteDataForTest("test_{{operationId}}"); |
| 69 | + |
| 70 | + // Fail if no prerequisite data found - tests require Terraform data |
| 71 | + if (prerequisiteData.isEmpty()) { |
| 72 | + throw new AssertionError("No prerequisite data found in Terraform output for test: test_{{operationId}}"); |
| 73 | + } |
| 74 | + |
| 75 | + // Execute the API method with parameters extracted from prerequisiteData |
| 76 | + try { |
| 77 | + {{#returnType}} |
| 78 | + {{{returnType}}} response = apiClient.{{operationId}}( |
| 79 | + {{#allParams}} |
| 80 | + {{#isBodyParam}}extractParameterWithType(prerequisiteData, "{{paramName}}", null, "{{datatype}}", "{{paramName}}"){{/isBodyParam}}{{^isBodyParam}}extractParameter(prerequisiteData, "{{paramName}}", null){{/isBodyParam}}{{^-last}}, {{/-last}} |
| 81 | + {{/allParams}} |
| 82 | + ); |
| 83 | + |
| 84 | + // Verify response is not null |
| 85 | + if (response == null) { |
| 86 | + throw new AssertionError("Response should not be null for {{operationId}}"); |
| 87 | + } |
| 88 | + {{/returnType}} |
| 89 | + {{^returnType}} |
| 90 | + apiClient.{{operationId}}( |
| 91 | + {{#allParams}} |
| 92 | + {{#isBodyParam}}extractParameterWithType(prerequisiteData, "{{paramName}}", null, "{{datatype}}", "{{paramName}}"){{/isBodyParam}}{{^isBodyParam}}extractParameter(prerequisiteData, "{{paramName}}", null){{/isBodyParam}}{{^-last}}, {{/-last}} |
| 93 | + {{/allParams}} |
| 94 | + ); |
| 95 | + {{/returnType}} |
| 96 | + } catch (ApiException e) { |
| 97 | + // Verify the HTTP response status code is valid (200, 201, 202, 204) |
| 98 | + int code = e.getCode(); |
| 99 | + if (!(code == 200 || code == 201 || code == 202 || code == 204)) { |
| 100 | + throw new AssertionError("API returned unexpected status code: " + code); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +{{/operation}} |
| 106 | +{{/operations}} |
| 107 | + |
| 108 | + /** |
| 109 | + * Helper method to extract a parameter from prerequisite data. |
| 110 | + * Delegates to TerraformHelper.extractParameter for smart field name resolution. |
| 111 | + * |
| 112 | + * @param prerequisiteData Map containing Terraform prerequisite data |
| 113 | + * @param paramName Name of the parameter to extract |
| 114 | + * @param defaultValue Default value if parameter is not found |
| 115 | + * @return The extracted parameter value or default value |
| 116 | + */ |
| 117 | + @SuppressWarnings("unchecked") |
| 118 | + private <T> T extractParameter(Map<String, Object> prerequisiteData, String paramName, T defaultValue) { |
| 119 | + Object value = TerraformHelper.extractParameter(prerequisiteData, paramName, defaultValue); |
| 120 | + return (T) value; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Helper method to extract a parameter with a specific target class for deserialization. |
| 125 | + * This is useful when a parameter can be multiple model types (e.g., Group vs AddGroupRequest). |
| 126 | + * |
| 127 | + * @param prerequisiteData Map containing Terraform prerequisite data |
| 128 | + * @param paramName Name of the parameter to extract |
| 129 | + * @param defaultValue Default value if parameter is not found |
| 130 | + * @param targetClassName Fully qualified class name for deserialization |
| 131 | + * @param typeHint Parameter name hint for intelligent type inference |
| 132 | + * @return The extracted parameter value deserialized to the target class, or default value |
| 133 | + */ |
| 134 | + @SuppressWarnings("unchecked") |
| 135 | + private <T> T extractParameterWithType(Map<String, Object> prerequisiteData, String paramName, T defaultValue, String targetClassName, String typeHint) { |
| 136 | + Object value = TerraformHelper.extractParameter(prerequisiteData, paramName, defaultValue, targetClassName, typeHint); |
| 137 | + return (T) value; |
| 138 | + } |
| 139 | +} |
0 commit comments