2 * Copyright 2020 Intel Corporation, Inc
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 gpic "github.com/onap/multicloud-k8s/src/orchestrator/pkg/gpic"
25 "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
28 func TestCreateAppIntent(t *testing.T) {
29 testCases := []struct {
31 inputAppIntent AppIntent
33 inputCompositeApp string
34 inputCompositeAppVersion string
35 inputGenericPlacementIntent string
41 label: "Create AppIntent",
42 inputAppIntent: AppIntent{
44 Name: "testAppIntent",
45 Description: "A sample AppIntent",
46 UserData1: "userData1",
47 UserData2: "userData2",
51 Intent: gpic.IntentStruc{
52 AllOfArray: []gpic.AllOf{
56 //ClusterLabelName: "edge1",
61 //ClusterLabelName: "edge2",
64 AnyOfArray: []gpic.AnyOf{
66 ClusterLabelName: "east-us1"},
68 ClusterLabelName: "east-us2"},
69 //{ClusterName: "east-us1"},
70 //{ClusterName: "east-us2"},
75 AnyOfArray: []gpic.AnyOf{},
80 inputProject: "testProject",
81 inputCompositeApp: "testCompositeApp",
82 inputCompositeAppVersion: "testCompositeAppVersion",
83 inputGenericPlacementIntent: "testIntent",
86 Name: "testAppIntent",
87 Description: "A sample AppIntent",
88 UserData1: "userData1",
89 UserData2: "userData2",
93 Intent: gpic.IntentStruc{
94 AllOfArray: []gpic.AllOf{
98 //ClusterLabelName: "edge1",
102 ClusterName: "edge2",
103 //ClusterLabelName: "edge2",
106 AnyOfArray: []gpic.AnyOf{
107 {ProviderName: "aws",
108 ClusterLabelName: "east-us1"},
109 {ProviderName: "aws",
110 ClusterLabelName: "east-us2"},
111 //{ClusterName: "east-us1"},
112 //{ClusterName: "east-us2"},
116 AnyOfArray: []gpic.AnyOf{},
122 Items: map[string]map[string][]byte{
123 ProjectKey{ProjectName: "testProject"}.String(): {
124 "projectmetadata": []byte(
125 "{\"project-name\":\"testProject\"," +
126 "\"description\":\"Test project for unit testing\"}"),
128 CompositeAppKey{CompositeAppName: "testCompositeApp",
129 Version: "testCompositeAppVersion", Project: "testProject"}.String(): {
130 "compositeappmetadata": []byte(
132 "\"name\":\"testCompositeApp\"," +
133 "\"description\":\"description\"," +
134 "\"userData1\":\"user data\"," +
135 "\"userData2\":\"user data\"" +
138 "\"version\":\"version of the composite app\"}}"),
140 GenericPlacementIntentKey{
142 Project: "testProject",
143 CompositeApp: "testCompositeApp",
144 Version: "testCompositeAppVersion",
146 "genericplacementintentmetadata": []byte(
147 "{\"metadata\":{\"Name\":\"testIntent\"," +
148 "\"Description\":\"A sample intent for testing\"," +
149 "\"UserData1\": \"userData1\"," +
150 "\"UserData2\": \"userData2\"}," +
151 "\"spec\":{\"Logical-Cloud\": \"logicalCloud1\"}}"),
157 for _, testCase := range testCases {
158 t.Run(testCase.label, func(t *testing.T) {
159 db.DBconn = testCase.mockdb
160 appIntentCli := NewAppIntentClient()
161 got, err := appIntentCli.CreateAppIntent(testCase.inputAppIntent, testCase.inputProject, testCase.inputCompositeApp, testCase.inputCompositeAppVersion, testCase.inputGenericPlacementIntent)
163 if testCase.expectedError == "" {
164 t.Fatalf("CreateAppIntent returned an unexpected error %s, ", err)
166 if strings.Contains(err.Error(), testCase.expectedError) == false {
167 t.Fatalf("CreateAppIntent returned an unexpected error %s", err)
170 if reflect.DeepEqual(testCase.expected, got) == false {
171 t.Errorf("CreateAppIntent returned unexpected body: got %v; "+" expected %v", got, testCase.expected)
178 func TestGetAppIntent(t *testing.T) {
179 testCases := []struct {
186 compositeAppName string
187 compositeAppVersion string
188 genericPlacementIntent string
192 appIntentName: "testAppIntent",
193 projectName: "testProject",
194 compositeAppName: "testCompositeApp",
195 compositeAppVersion: "testCompositeAppVersion",
196 genericPlacementIntent: "testIntent",
199 Name: "testAppIntent",
200 Description: "testAppIntent",
201 UserData1: "userData1",
202 UserData2: "userData2",
205 AppName: "SampleApp",
206 Intent: gpic.IntentStruc{
207 AllOfArray: []gpic.AllOf{
210 ClusterName: "edge1",
214 ClusterName: "edge2",
217 AnyOfArray: []gpic.AnyOf{
218 {ProviderName: "aws",
219 ClusterLabelName: "east-us1"},
220 {ProviderName: "aws",
221 ClusterLabelName: "east-us2"},
230 Items: map[string]map[string][]byte{
232 Name: "testAppIntent",
233 Project: "testProject",
234 CompositeApp: "testCompositeApp",
235 Version: "testCompositeAppVersion",
236 Intent: "testIntent",
238 "appintentmetadata": []byte(
239 "{\"metadata\":{\"Name\":\"testAppIntent\"," +
240 "\"Description\":\"testAppIntent\"," +
241 "\"UserData1\": \"userData1\"," +
242 "\"UserData2\": \"userData2\"}," +
243 "\"spec\":{\"app-name\": \"SampleApp\"," +
247 "\"provider-name\":\"aws\"," +
248 "\"cluster-name\":\"edge1\"}," +
250 "\"provider-name\":\"aws\"," +
251 "\"cluster-name\":\"edge2\"}," +
255 "\"provider-name\":\"aws\"," +
256 "\"cluster-label-name\":\"east-us1\"}," +
258 "\"provider-name\":\"aws\"," +
259 "\"cluster-label-name\":\"east-us2\"}" +
268 for _, testCase := range testCases {
269 t.Run(testCase.label, func(t *testing.T) {
270 db.DBconn = testCase.mockdb
271 appIntentCli := NewAppIntentClient()
272 got, err := appIntentCli.GetAppIntent(testCase.appIntentName, testCase.projectName, testCase.compositeAppName, testCase.compositeAppVersion,
273 testCase.genericPlacementIntent)
275 if testCase.expectedError == "" {
276 t.Fatalf("GetAppIntent returned an unexpected error: %s", err)
278 if strings.Contains(err.Error(), testCase.expectedError) == false {
279 t.Fatalf("GetAppIntent returned an unexpected error: %s", err)
282 if reflect.DeepEqual(testCase.expected, got) == false {
283 t.Errorf("GetAppIntent returned unexpected body: got %v;"+
284 " expected %v", got, testCase.expected)