forked from wso2/product-apim-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicData_testUtils.go
More file actions
179 lines (147 loc) · 7.32 KB
/
dynamicData_testUtils.go
File metadata and controls
179 lines (147 loc) · 7.32 KB
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
/*
* Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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.
*/
package testutils
import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wso2/product-apim-tooling/import-export-cli/integration/apim"
"github.com/wso2/product-apim-tooling/import-export-cli/integration/base"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)
const (
devEnvProdUrl = "DEV_ENV_PROD_URL"
devEnvSandUrl = "DEV_ENV_SAND_URL"
devEnvProdRetryDelay = "DEV_ENV_PROD_RE_DELAY"
devEnvProdRetryTimeOut = "DEV_ENV_PROD_RE_TO"
envKey = "ENV_KEY"
)
func SetEnvVariablesForAPI(t *testing.T, client *apim.Client) {
t.Log("Setting up the environment variable values")
os.Setenv(devEnvProdUrl, "https://localhost:"+strconv.Itoa(9443+client.GetPortOffset())+
"/am/sample/pizzashack/v1/api/")
os.Setenv(devEnvSandUrl, "https://localhost:"+strconv.Itoa(9443+client.GetPortOffset())+
"/am/sample/pizzashack/v1/api/")
os.Setenv(devEnvProdRetryDelay, "10")
os.Setenv(devEnvProdRetryTimeOut, "5")
os.Setenv(envKey, "dev_101")
t.Cleanup(func() {
t.Log("Unsetting the environment variable values")
os.Unsetenv(devEnvProdUrl)
os.Unsetenv(devEnvSandUrl)
os.Unsetenv(devEnvProdRetryDelay)
os.Unsetenv(devEnvProdRetryTimeOut)
os.Unsetenv(envKey)
})
}
func ValidateDynamicData(t *testing.T, api *apim.API) {
// Retrieve the endpointConfig of the imported API
endpointConfig := api.GetEndPointConfig()
// Check whether the production endpoint has the expected value set using the env variable
productionEndpoints := endpointConfig.(map[string]interface{})["production_endpoints"].(map[string]interface{})
assert.Equal(t, os.Getenv(devEnvProdUrl), productionEndpoints["url"], "Production endpoint value mismatched")
// Check whether the sandbox endpoint has the expected value set using the env variable
sandboxEndpoints := endpointConfig.(map[string]interface{})["sandbox_endpoints"].(map[string]interface{})
assert.Equal(t, os.Getenv(devEnvSandUrl), sandboxEndpoints["url"], "Sandbox endpoint value mismatched")
// Check whether the retryDelay and retryTimeOut roduction endpoint
// config values has the expected values set using the env variables
assert.Equal(t, os.Getenv(devEnvProdRetryDelay),
productionEndpoints["config"].(map[string]interface{})["retryDelay"].(string),
"Retry delay value of the production endpoint value mismatched")
assert.Equal(t, os.Getenv(devEnvProdRetryTimeOut),
productionEndpoints["config"].(map[string]interface{})["retryTimeOut"].(string),
"Retry time out value of the production endpoint config mismatched")
}
func AddSequenceWithDynamicDataToAPIProject(t *testing.T, args *InitTestArgs) apim.APIFile {
operationPolicyPathInProject := args.InitFlag + PoliciesDirectory
// Move sequence file to created project
srcPathForSequence, _ := filepath.Abs(DynamicDataSampleCaseArtifactPath + string(os.PathSeparator) + DynamicDataInSequence)
destPathForSequence := operationPolicyPathInProject + string(os.PathSeparator) + DynamicDataInSequence
base.Copy(srcPathForSequence, destPathForSequence)
base.WaitForIndexing()
// Move sequence definition file to created project
srcPathForSequenceDefinition, _ := filepath.Abs(DynamicDataSampleCaseArtifactPath + string(os.PathSeparator) +
DynamicDataInSequenceDefinition)
destPathForSequenceDefinition := operationPolicyPathInProject + string(os.PathSeparator) + DynamicDataInSequenceDefinition
base.Copy(srcPathForSequenceDefinition, destPathForSequenceDefinition)
base.WaitForIndexing()
// Update api.yaml file of initialized project with sequence related metadata
apiDefinitionFilePath := args.InitFlag + string(os.PathSeparator) + utils.APIDefinitionFileYaml
apiDefinitionFileContent := ReadAPIDefinition(t, apiDefinitionFilePath)
// Operation policy that will be added
var requestPolicies []interface{}
operationPolicies := apim.OperationPolicies{
Request: append(requestPolicies, map[string]interface{}{
"policyName": TestSampleDynamicDataPolicyName,
"policyType": "api",
}),
Response: []string{},
Fault: []string{},
}
// Assign the above operation policy to a resource
apiOperationWithPolicy := apim.APIOperations{
Target: TestSampleDynamicDataOperationTarget,
Verb: TestSampleDynamicDataOperationVerb,
AuthType: TestSampleDynamicDataOperationAuthType,
ThrottlingPolicy: TestSampleDynamicDataOperationThrottlingPolicy,
OperationPolicies: operationPolicies,
}
// Add the operation policy added resource to the API
apiOperations := []apim.APIOperations{apiOperationWithPolicy}
apiDefinitionFileContent.Data.Operations = apiOperations
// Write the modified API definition to the directory
WriteToAPIDefinition(t, apiDefinitionFileContent, apiDefinitionFilePath)
return apiDefinitionFileContent
}
func ValidateExportedSequenceWithDynamicData(t *testing.T, args *InitTestArgs, api apim.API) {
exportedOutput := ValidateExportImportedAPI(t, args, api.Name, api.Version)
// Unzip exported API and check whether the imported sequence is in there
exportedPath := base.GetExportedPathFromOutput(exportedOutput)
relativePath := strings.ReplaceAll(exportedPath, ".zip", "")
base.Unzip(relativePath, exportedPath)
exportedAPIRelativePath := relativePath + string(os.PathSeparator) + api.Name + "-" + api.Version
sequencePathOfExportedAPI := exportedAPIRelativePath +
PoliciesDirectory + string(os.PathSeparator) + DynamicDataInSequence
// Check whether the sequence file is available
isSequenceExported := base.IsFileAvailable(t, sequencePathOfExportedAPI)
base.Log("Sequence is Exported ", isSequenceExported)
assert.Equal(t, true, isSequenceExported, "Error while exporting API with the sequence")
// The environment variable must have been substituted twice in the sequence
dynamicDataSubstitutedSequencePath, _ := filepath.Abs(DynamicDataSubstitutedInSequence)
isSequenceDataSubstituted := base.IsFileContentIdentical(sequencePathOfExportedAPI, dynamicDataSubstitutedSequencePath)
base.Log("Exported operation policy definition has substitued environment variables", isSequenceDataSubstituted)
assert.Equal(t, true, isSequenceDataSubstituted, "Error while substituting the environment variables to the sequence")
t.Cleanup(func() {
base.RemoveDir(relativePath)
base.RemoveDir(exportedPath)
})
}
func ValidateImportProjectFailedWithoutSettingEnvVariables(t *testing.T, args *InitTestArgs, paramsPath string, preserveProvider bool) {
t.Helper()
result, _ := importApiFromProject(t, args.InitFlag, args.APIName, paramsPath, args.SrcAPIM, &args.CtlUser, false,
preserveProvider)
assert.Contains(t, base.GetValueOfUniformResponse(result), "Exit status 1")
base.WaitForIndexing()
//Remove Created project and logout
t.Cleanup(func() {
base.RemoveDir(args.InitFlag)
})
}