You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	Optimized json handling during Patch Request build. No fix json struc… (#3976)
* Optimized json handling during Patch Request build. No fix json struct for Config Base Json * Error handling * Update abapEnvironmentPushATCSystemConfig.go Error Handling 2 * Update abapEnvironmentPushATCSystemConfig.go Error Handling 3 Co-authored-by: Daniel Bernd <93763187+danManSAP@users.noreply.github.com>
This commit is contained in:
		| @@ -225,19 +225,18 @@ func buildATCSystemConfigBatchRequest(confUUID string, atcSystemConfiguartionJso | ||||
| 	var batchRequestString string | ||||
|  | ||||
| 	//splitting json into configuration base and configuration properties & build a batch request for oData - patch config & patch priorities | ||||
| 	var configBaseJson parsedConfigJsonBase | ||||
| 	err := json.Unmarshal(atcSystemConfiguartionJsonFile, &configBaseJson) | ||||
| 	//first remove expansion to priorities to get only "base" Configuration | ||||
| 	configBaseJsonBody, err := buildParsedATCSystemConfigBaseJsonBody(confUUID, bytes.NewBuffer(atcSystemConfiguartionJsonFile).String()) | ||||
| 	if err != nil { | ||||
| 		return batchRequestString, err | ||||
| 	} | ||||
|  | ||||
| 	var parsedConfigPriorities parsedConfigPriorities | ||||
| 	err = json.Unmarshal(atcSystemConfiguartionJsonFile, &parsedConfigPriorities) | ||||
| 	if err != nil { | ||||
| 		return batchRequestString, err | ||||
| 	} | ||||
|  | ||||
| 	configBaseJson.ConfUUID = confUUID | ||||
|  | ||||
| 	//build the Batch request string | ||||
| 	contentID := 1 | ||||
|  | ||||
| @@ -245,7 +244,6 @@ func buildATCSystemConfigBatchRequest(confUUID string, atcSystemConfiguartionJso | ||||
| 	//now adding opening Changeset as at least config base is to be patched | ||||
| 	batchRequestString = addChangesetBegin(batchRequestString, contentID) | ||||
|  | ||||
| 	configBaseJsonBody, err := json.Marshal(&configBaseJson) | ||||
| 	if err != nil { | ||||
| 		return batchRequestString, err | ||||
| 	} | ||||
| @@ -267,7 +265,7 @@ func buildATCSystemConfigBatchRequest(confUUID string, atcSystemConfiguartionJso | ||||
| 			batchRequestString = addChangesetBegin(batchRequestString, contentID) | ||||
|  | ||||
| 			//now PATCH command for priority | ||||
| 			batchRequestString = addPatchSinglePriorityChangeset(batchRequestString, confUUID, priorityLine.Test, priorityLine.MessageId, priorityJsonBody) | ||||
| 			batchRequestString = addPatchSinglePriorityChangeset(batchRequestString, confUUID, priorityLine.Test, priorityLine.MessageId, string(priorityJsonBody)) | ||||
|  | ||||
| 		} | ||||
| 	} | ||||
| @@ -281,7 +279,34 @@ func buildATCSystemConfigBatchRequest(confUUID string, atcSystemConfiguartionJso | ||||
|  | ||||
| } | ||||
|  | ||||
| func addPatchConfigBaseChangeset(inputString string, confUUID string, configBaseJsonBody []byte) string { | ||||
| func buildParsedATCSystemConfigBaseJsonBody(confUUID string, atcSystemConfiguartionJsonFile string) (string, error) { | ||||
|  | ||||
| 	var i interface{} | ||||
| 	var outputString string = `` | ||||
|  | ||||
| 	if err := json.Unmarshal([]byte(atcSystemConfiguartionJsonFile), &i); err != nil { | ||||
| 		log.Entry().Errorf("problem with unmarshall input "+atcSystemConfiguartionJsonFile, err) | ||||
| 		return outputString, err | ||||
| 	} | ||||
| 	if m, ok := i.(map[string]interface{}); ok { | ||||
| 		delete(m, "_priorities") | ||||
| 	} | ||||
|  | ||||
| 	output, err := json.Marshal(i) | ||||
| 	if err != nil { | ||||
| 		log.Entry().Errorf("problem with marshall output "+atcSystemConfiguartionJsonFile, err) | ||||
| 		return outputString, err | ||||
| 	} | ||||
| 	//injecting the configuration ID | ||||
| 	output = output[1:] // remove leading '{' | ||||
| 	outputString = string(output) | ||||
| 	confIDString := `{"conf_id":"` + confUUID + `",` | ||||
| 	outputString = confIDString + outputString | ||||
|  | ||||
| 	return outputString, err | ||||
| } | ||||
|  | ||||
| func addPatchConfigBaseChangeset(inputString string, confUUID string, configBaseJsonBody string) string { | ||||
|  | ||||
| 	entityIdString := `(root_id='1',conf_id=` + confUUID + `)` | ||||
| 	newString := addCommandEntityChangeset("PATCH", "configuration", entityIdString, inputString, configBaseJsonBody) | ||||
| @@ -289,7 +314,7 @@ func addPatchConfigBaseChangeset(inputString string, confUUID string, configBase | ||||
| 	return newString | ||||
| } | ||||
|  | ||||
| func addPatchSinglePriorityChangeset(inputString string, confUUID string, test string, messageId string, priorityJsonBody []byte) string { | ||||
| func addPatchSinglePriorityChangeset(inputString string, confUUID string, test string, messageId string, priorityJsonBody string) string { | ||||
|  | ||||
| 	entityIdString := `(root_id='1',conf_id=` + confUUID + `,test='` + test + `',message_id='` + messageId + `')` | ||||
| 	newString := addCommandEntityChangeset("PATCH", "priority", entityIdString, inputString, priorityJsonBody) | ||||
| @@ -327,7 +352,7 @@ func addEndOfBatch(inputString string) string { | ||||
| 	return newString | ||||
| } | ||||
|  | ||||
| func addCommandEntityChangeset(command string, entity string, entityIdString string, inputString string, jsonBody []byte) string { | ||||
| func addCommandEntityChangeset(command string, entity string, entityIdString string, inputString string, jsonBody string) string { | ||||
|  | ||||
| 	newString := inputString + ` | ||||
| ` + command + ` ` + entity + entityIdString + ` HTTP/1.1 | ||||
| @@ -335,7 +360,7 @@ Content-Type: application/json | ||||
|  | ||||
| ` | ||||
| 	if len(jsonBody) > 0 { | ||||
| 		newString += string(jsonBody) + ` | ||||
| 		newString += jsonBody + ` | ||||
| ` | ||||
| 	} | ||||
|  | ||||
| @@ -507,21 +532,6 @@ type parsedConfigJsonWithExpand struct { | ||||
| 	Priorities    []parsedConfigPriority `json:"_priorities"` | ||||
| } | ||||
|  | ||||
| type parsedConfigJsonBase struct { | ||||
| 	ConfName             string `json:"conf_name"` | ||||
| 	ConfUUID             string `json:"conf_id"` | ||||
| 	Checkvariant         string `json:"checkvariant"` | ||||
| 	PseudoCommentPolicy  string `json:"pseudo_comment_policy"` | ||||
| 	BlockFindings        string `json:"block_findings"` | ||||
| 	InformFindings       string `json:"inform_findings"` | ||||
| 	TransportCheckPolicy string `json:"transport_check_policy"` | ||||
| 	CheckTasks           bool   `json:"check_tasks"` | ||||
| 	CheckRequests        bool   `json:"check_requests"` | ||||
| 	ChechToCs            bool   `json:"check_tocs"` | ||||
| 	IsDefault            bool   `json:"is_default"` | ||||
| 	IsProxyVariant       bool   `json:"is_proxy_variant"` | ||||
| } | ||||
|  | ||||
| type parsedConfigPriorities struct { | ||||
| 	Priorities []parsedConfigPriority `json:"_priorities"` | ||||
| } | ||||
|   | ||||
| @@ -230,7 +230,7 @@ Content-ID: 1 | ||||
| PATCH configuration(root_id='1',conf_id=4711) HTTP/1.1 | ||||
| Content-Type: application/json | ||||
|  | ||||
| {"conf_name":"UNITTEST_PIPERSTEP","conf_id":"4711","checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","pseudo_comment_policy":"MK","block_findings":"0","inform_findings":"1","transport_check_policy":"C","check_tasks":true,"check_requests":false,"check_tocs":true,"is_default":false,"is_proxy_variant":false} | ||||
| {"conf_id":"4711","block_findings":"0","check_requests":false,"check_tasks":true,"check_tocs":true,"checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","conf_name":"UNITTEST_PIPERSTEP","inform_findings":"1","pseudo_comment_policy":"MK","transport_check_policy":"C"} | ||||
|  | ||||
| --changeset | ||||
| Content-Type: application/http | ||||
| @@ -257,8 +257,6 @@ Content-Type: application/json | ||||
| 			"check_tasks": true, | ||||
| 			"check_requests": false, | ||||
| 			"check_tocs": true, | ||||
| 			"is_default": false, | ||||
| 			"is_proxy_variant": false, | ||||
| 			"_priorities": [ | ||||
| 				{ | ||||
| 					"test": "CL_CI_TEST_AMDP_HDB_MIGRATION", | ||||
| @@ -291,7 +289,7 @@ Content-ID: 1 | ||||
| PATCH configuration(root_id='1',conf_id=4711) HTTP/1.1 | ||||
| Content-Type: application/json | ||||
|  | ||||
| {"conf_name":"UNITTEST_PIPERSTEP","conf_id":"4711","checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","pseudo_comment_policy":"MK","block_findings":"0","inform_findings":"1","transport_check_policy":"C","check_tasks":true,"check_requests":false,"check_tocs":true,"is_default":false,"is_proxy_variant":false} | ||||
| {"conf_id":"4711","block_findings":"0","check_requests":false,"check_tasks":true,"check_tocs":true,"checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","conf_name":"UNITTEST_PIPERSTEP","inform_findings":"1","pseudo_comment_policy":"MK","transport_check_policy":"C"} | ||||
|  | ||||
| --changeset | ||||
| Content-Type: application/http | ||||
| @@ -327,9 +325,7 @@ Content-Type: application/json | ||||
| 			"transport_check_policy": "C", | ||||
| 			"check_tasks": true, | ||||
| 			"check_requests": false, | ||||
| 			"check_tocs": true, | ||||
| 			"is_default": false, | ||||
| 			"is_proxy_variant": false,		 | ||||
| 			"check_tocs": true,	 | ||||
| 			"_priorities": [ | ||||
| 				{ | ||||
| 					"test": "CL_CI_TEST_AMDP_HDB_MIGRATION", | ||||
| @@ -367,7 +363,7 @@ Content-ID: 1 | ||||
| PATCH configuration(root_id='1',conf_id=4711) HTTP/1.1 | ||||
| Content-Type: application/json | ||||
|  | ||||
| {"conf_name":"UNITTEST_PIPERSTEP","conf_id":"4711","checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","pseudo_comment_policy":"MK","block_findings":"0","inform_findings":"1","transport_check_policy":"C","check_tasks":true,"check_requests":false,"check_tocs":true,"is_default":false,"is_proxy_variant":false} | ||||
| {"conf_id":"4711","block_findings":"0","check_requests":false,"check_tasks":true,"check_tocs":true,"checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","conf_name":"UNITTEST_PIPERSTEP","inform_findings":"1","pseudo_comment_policy":"MK","transport_check_policy":"C"} | ||||
|  | ||||
| --changeset-- | ||||
|  | ||||
| @@ -383,9 +379,7 @@ Content-Type: application/json | ||||
| 			"transport_check_policy": "C", | ||||
| 			"check_tasks": true, | ||||
| 			"check_requests": false, | ||||
| 			"check_tocs": true, | ||||
| 			"is_default": false, | ||||
| 			"is_proxy_variant": false | ||||
| 			"check_tocs": true | ||||
| 		} | ||||
| 		` | ||||
|  | ||||
| @@ -410,7 +404,7 @@ Content-ID: 1 | ||||
| PATCH configuration(root_id='1',conf_id=4711) HTTP/1.1 | ||||
| Content-Type: application/json | ||||
|  | ||||
| {"conf_name":"UNITTEST_PIPERSTEP","conf_id":"4711","checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","pseudo_comment_policy":"MK","block_findings":"0","inform_findings":"1","transport_check_policy":"C","check_tasks":true,"check_requests":false,"check_tocs":true,"is_default":false,"is_proxy_variant":false} | ||||
| {"conf_id":"4711","block_findings":"0","check_requests":false,"check_tasks":true,"check_tocs":true,"checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","conf_name":"UNITTEST_PIPERSTEP","inform_findings":"1","pseudo_comment_policy":"MK","transport_check_policy":"C"} | ||||
|  | ||||
| --changeset-- | ||||
|  | ||||
| @@ -427,8 +421,46 @@ Content-Type: application/json | ||||
| 			"check_tasks": true, | ||||
| 			"check_requests": false, | ||||
| 			"check_tocs": true, | ||||
| 			"is_default": false, | ||||
| 			"is_proxy_variant": false, | ||||
| 			"_priorities": [ | ||||
| 			] | ||||
| 		} | ||||
| 		` | ||||
|  | ||||
| 		confUUID := "4711" | ||||
| 		batchATCSystemConfigFile, err := buildATCSystemConfigBatchRequest(confUUID, []byte(atcSystemConfigFileString)) | ||||
| 		if err != nil { | ||||
| 			t.Fatal("Failed to Build ATC System Config Batch") | ||||
| 		} | ||||
| 		assert.Equal(t, batchATCSystemConfigFileExpected, batchATCSystemConfigFile) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("success case: BuildATCSystemConfigBatch - Config Base only (empty expand _priorities) - Settings Global", func(t *testing.T) { | ||||
| 		batchATCSystemConfigFileExpected := ` | ||||
| --request-separator | ||||
| Content-Type: multipart/mixed;boundary=changeset | ||||
|  | ||||
| --changeset | ||||
| Content-Type: application/http | ||||
| Content-Transfer-Encoding: binary | ||||
| Content-ID: 1 | ||||
|  | ||||
| PATCH configuration(root_id='1',conf_id=4711) HTTP/1.1 | ||||
| Content-Type: application/json | ||||
|  | ||||
| {"conf_id":"4711","block_findings":"0","checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","conf_name":"UNITTEST_PIPERSTEP","inform_findings":"1","pseudo_comment_policy":"MK","transport_check_policy":"G"} | ||||
|  | ||||
| --changeset-- | ||||
|  | ||||
| --request-separator--` | ||||
|  | ||||
| 		// no Configuration name supplied | ||||
| 		atcSystemConfigFileString := `{ | ||||
| 			"conf_name": "UNITTEST_PIPERSTEP", | ||||
| 			"checkvariant": "SAP_CLOUD_PLATFORM_ATC_DEFAULT", | ||||
| 			"pseudo_comment_policy": "MK", | ||||
| 			"block_findings": "0", | ||||
| 			"inform_findings": "1", | ||||
| 			"transport_check_policy": "G", | ||||
| 			"_priorities": [ | ||||
| 			] | ||||
| 		} | ||||
| @@ -456,8 +488,6 @@ Content-Type: application/json | ||||
| 			"check_tasks": true, | ||||
| 			"check_requests": false, | ||||
| 			"check_tocs": true, | ||||
| 			"is_default": false, | ||||
| 			"is_proxy_variant": false, | ||||
| 			"_priorities": [ | ||||
| 				{ | ||||
| 					"test": "CL_CI_TEST_AMDP_HDB_MIGRATION", | ||||
|   | ||||
		Reference in New Issue
	
	Block a user