github.com/polarismesh/polaris@v1.17.8/apiserver/eurekaserver/model_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package eurekaserver 19 20 import ( 21 "encoding/xml" 22 "fmt" 23 "strings" 24 "testing" 25 ) 26 27 const ( 28 instanceXMLStr = "<application>\n" + 29 "<name>XXL-JOB-EXECUTOR-PAAS</name>\n" + 30 "<instance>\n" + 31 "<instanceId>xxl-job-executor-b7c89dcf4-wrk7s:xxl-job-executor-paas:8081</instanceId>\n" + 32 "<hostName>xxl-job-executor-b7c89dcf4-wrk7s</hostName>\n" + 33 "<app>XXL-JOB-EXECUTOR-PAAS</app>\n" + 34 "<ipAddr>10.157.22.100</ipAddr>\n" + 35 "<status>UP</status>\n" + 36 "<overriddenstatus>UNKNOWN</overriddenstatus>\n" + 37 "<port enabled=\"true\">8081</port>\n" + 38 "<securePort enabled=\"false\">443</securePort>\n" + 39 "<countryId>1</countryId>\n" + 40 "<dataCenterInfo class=\"com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo\">\n" + 41 "<name>MyOwn</name>\n" + 42 "</dataCenterInfo>\n" + 43 "<leaseInfo>\n" + 44 "<renewalIntervalInSecs>30</renewalIntervalInSecs>\n" + 45 "<durationInSecs>90</durationInSecs>\n" + 46 "<registrationTimestamp>1631448499517</registrationTimestamp>\n" + 47 "<lastRenewalTimestamp>1632368761523</lastRenewalTimestamp>\n" + 48 "<evictionTimestamp>0</evictionTimestamp>\n" + 49 "<serviceUpTimestamp>1631448499517</serviceUpTimestamp>\n" + 50 "</leaseInfo>\n" + 51 "<metadata class=\"java.util.Collections$EmptyMap\">\n" + 52 "<region>shanghai</region>\n" + 53 "</metadata>\n" + 54 "<homePageUrl>http://xxl-job-executor-b7c89dcf4-wrk7s:8081/</homePageUrl>\n" + 55 "<statusPageUrl>http://xxl-job-executor-b7c89dcf4-wrk7s:8081/info</statusPageUrl>\n" + 56 "<healthCheckUrl>http://xxl-job-executor-b7c89dcf4-wrk7s:8081/health</healthCheckUrl>\n" + 57 "<vipAddress>xxl-job-executor-paas</vipAddress>\n" + 58 "<secureVipAddress>xxl-job-executor-paas</secureVipAddress>\n" + 59 "<isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>\n" + 60 "<lastUpdatedTimestamp>1631448499517</lastUpdatedTimestamp>\n" + 61 "<lastDirtyTimestamp>1631443454988</lastDirtyTimestamp>\n" + 62 "<actionType>ADDED</actionType>\n" + 63 "</instance>\n" + 64 "</application>" 65 instanceNoMetaXMLStr = "<application>\n" + 66 "<name>XXL-JOB-EXECUTOR-PAAS</name>\n" + 67 "<instance>\n" + 68 "<instanceId>xxl-job-executor-b7c89dcf4-wrk7s:xxl-job-executor-paas:8081</instanceId>\n" + 69 "<hostName>xxl-job-executor-b7c89dcf4-wrk7s</hostName>\n" + 70 "<app>XXL-JOB-EXECUTOR-PAAS</app>\n" + 71 "<ipAddr>10.157.22.100</ipAddr>\n" + 72 "<status>UP</status>\n" + 73 "<overriddenstatus>UNKNOWN</overriddenstatus>\n" + 74 "<port enabled=\"true\">8081</port>\n" + 75 "<securePort enabled=\"false\">443</securePort>\n" + 76 "<countryId>1</countryId>\n" + 77 "<dataCenterInfo class=\"com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo\">\n" + 78 "<name>MyOwn</name>\n" + 79 "</dataCenterInfo>\n" + 80 "<leaseInfo>\n" + 81 "<renewalIntervalInSecs>30</renewalIntervalInSecs>\n" + 82 "<durationInSecs>90</durationInSecs>\n" + 83 "<registrationTimestamp>1631448499517</registrationTimestamp>\n" + 84 "<lastRenewalTimestamp>1632368761523</lastRenewalTimestamp>\n" + 85 "<evictionTimestamp>0</evictionTimestamp>\n" + 86 "<serviceUpTimestamp>1631448499517</serviceUpTimestamp>\n" + 87 "</leaseInfo>\n" + 88 "<metadata class=\"java.util.Collections$EmptyMap\"/>\n" + 89 "<homePageUrl>http://xxl-job-executor-b7c89dcf4-wrk7s:8081/</homePageUrl>\n" + 90 "<statusPageUrl>http://xxl-job-executor-b7c89dcf4-wrk7s:8081/info</statusPageUrl>\n" + 91 "<healthCheckUrl>http://xxl-job-executor-b7c89dcf4-wrk7s:8081/health</healthCheckUrl>\n" + 92 "<vipAddress>xxl-job-executor-paas</vipAddress>\n" + 93 "<secureVipAddress>xxl-job-executor-paas</secureVipAddress>\n" + 94 "<isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>\n" + 95 "<lastUpdatedTimestamp>1631448499517</lastUpdatedTimestamp>\n" + 96 "<lastDirtyTimestamp>1631443454988</lastDirtyTimestamp>\n" + 97 "<actionType>ADDED</actionType>\n" + 98 "</instance>\n" + 99 "</application>" 100 ) 101 102 // TestInstanceInfo_UnmarshalXML 测试反序列化XML 103 func TestInstanceInfo_UnmarshalXML(t *testing.T) { 104 app := &Application{} 105 err := xml.NewDecoder(strings.NewReader(instanceNoMetaXMLStr)).Decode(app) 106 if err != nil { 107 t.Fatal(err) 108 } 109 fmt.Printf("app is %+v\n", app.Instance[0].Metadata.Meta) 110 111 builder := &strings.Builder{} 112 err = xml.NewEncoder(builder).Encode(app) 113 if err != nil { 114 t.Fatal(err) 115 } 116 fmt.Printf("xml values is %s\n", builder.String()) 117 }