1/**2 * http://www.163.com3 * Copyright (c) 1997-2018 All Rights Reserved.4 */5package com.netease.cloudqa.nlb.api.test.utils;6import static org.testng.Assert.assertEquals;7import java.lang.reflect.InvocationTargetException;8import java.lang.reflect.Method;9import java.util.HashMap;10import java.util.Map;11import java.util.concurrent.TimeUnit;12import com.alibaba.fastjson.JSONObject;13import com.netease.cloudqa.nlb.api.test.framework.common.runtime.ApiRuntimeContext;14import com.netease.cloudqa.nlb.api.test.framework.driver.FrameWorkDriver;15import com.netease.cloudqa.nlb.api.test.framework.model.DataUnit;16import com.netease.cloudqa.nlb.api.test.framework.model.Response;17import com.netease.cloudqa.nlb.api.test.framework.utils.HttpClientUtils;18import com.netease.cloudqa.nlb.api.test.model.Listener;19import com.netease.cloudqa.nlb.api.test.model.LoadBalancer;20import com.netease.cloudqa.nlb.api.test.model.NlbModel;21import com.netease.cloudqa.nlb.api.test.model.Order;22/**23 *24 * @author hzzhangyan25 * @version $Id: PrepareMethodInvokerUtils.java, v 0.1 2018-5-16 ä¸å1:04:43 hzzhangyan Exp $26 */27public class PrepareMethodInvokerUtils extends FrameWorkDriver {28 private static Map<String, Method> methodMap = new HashMap<String, Method>();29 final private static String NLB_MODEL = "NLB_MODEL";30 static {31 try {32 methodMap.put("PrepareMethodInvokerUtils.prepareListener",33 PrepareMethodInvokerUtils.class.getMethod("prepareListener", Map.class,34 HttpClientUtils.class, String.class));35 methodMap.put("PrepareMethodInvokerUtils.prepareListenerIng",36 PrepareMethodInvokerUtils.class.getMethod("prepareListenerIng", Map.class,37 HttpClientUtils.class, String.class));38 methodMap.put("PrepareMethodInvokerUtils.prepareIngress",39 PrepareMethodInvokerUtils.class.getMethod("prepareIngress", Map.class,40 HttpClientUtils.class, String.class));41 methodMap.put("PrepareMethodInvokerUtils.prepareIngressAndOrder",42 PrepareMethodInvokerUtils.class.getMethod("prepareIngressAndOrder", Map.class,43 HttpClientUtils.class, String.class, String.class));44 methodMap.put("PrepareMethodInvokerUtils.prepareIngressAndUpdateOrder",45 PrepareMethodInvokerUtils.class.getMethod("prepareIngressAndUpdateOrder",46 Map.class, HttpClientUtils.class, String.class, String.class));47 methodMap.put("PrepareMethodInvokerUtils.prepareIngressWithoutWaiting",48 PrepareMethodInvokerUtils.class.getMethod("prepareIngressWithoutWaiting",49 Map.class, HttpClientUtils.class, String.class));50 methodMap.put("PrepareMethodInvokerUtils.prepareCreateOrder",51 PrepareMethodInvokerUtils.class.getMethod("prepareCreateOrder", Map.class,52 HttpClientUtils.class, String.class, String.class));53 methodMap.put("PrepareMethodInvokerUtils.prepareIngressAndListener",54 PrepareMethodInvokerUtils.class.getMethod("prepareIngressAndListener", Map.class,55 HttpClientUtils.class, String.class));56 } catch (NoSuchMethodException e) {57 e.printStackTrace();58 } catch (SecurityException e) {59 e.printStackTrace();60 }61 }62 public static void invokeMethod(String methodName, Object... args) {63 try {64 NlbModel nlbModel = (NlbModel) methodMap.get(methodName).invoke(65 new PrepareMethodInvokerUtils(), args);66 DataUnit unit = new DataUnit(NLB_MODEL, nlbModel);67 ApiRuntimeContext.CaseContext.addPrameter(unit);68 } catch (IllegalAccessException e) {69 e.printStackTrace();70 } catch (IllegalArgumentException e) {71 e.printStackTrace();72 } catch (InvocationTargetException e) {73 e.printStackTrace();74 }75 }76 public NlbModel prepareListener(Map<String, String> headers, HttpClientUtils httpClient,77 String prepareBody) {78 JSONObject jsonBody = JSONObject.parseObject(prepareBody);79 Response res = CommonApi.createLn(headers, jsonBody, httpClient);80 assertEquals(res.getCode(), 200, "Prepare Lb listener failed!");81 logger.info("Prepare data successfully!");82 Response res1 = CommonApi83 .getLbDetail(headers, httpClient, jsonBody.getString("InstanceId"));84 NlbModel nlbModel = new NlbModel();85 JSONObject listener = CommonApi.findListenerByName(JSONObject.parseObject(res1.getHtml()),86 "testln1");87 nlbModel.setListener(new Listener(listener.getString("ListenerId")));88 return nlbModel;89 }90 public NlbModel prepareListenerIng(Map<String, String> headers, HttpClientUtils httpClient,91 String prepareBody) {92 JSONObject jsonBody = JSONObject.parseObject(prepareBody);93 Response res = CommonApi.createLnIng(headers, jsonBody, httpClient);94 assertEquals(res.getCode(), 204, "Prepare ing listener failed!");95 logger.info("Prepare data successfully!");96 NlbModel nlbModel = new NlbModel();97 nlbModel.setListener(new Listener(jsonBody.getString("Name")));98 try {99 TimeUnit.SECONDS.sleep(10);100 } catch (InterruptedException e) {101 System.out.println("error");102 }103 return nlbModel;104 }105 public NlbModel prepareIngressAndListener(Map<String, String> headers,106 HttpClientUtils httpClient, String prepareBody) {107 JSONObject jsonBody = JSONObject.parseObject(prepareBody);108 JSONObject createBody = jsonBody.getJSONObject("CreateBody");109 JSONObject listenerBody = jsonBody.getJSONObject("ListenerBody");110 Response res = CommonApi.createLbIng(headers, createBody, httpClient);111 JSONObject resJson = JSONObject.parseObject(res.getHtml());112 assertEquals(res.getCode(), 200, "Prepare ingress failed!");113 String instanceId = resJson.getString("InstanceId");114 listenerBody.put("InstanceId", instanceId);115 String status = CommonApi.waitIng(headers, httpClient, instanceId);116 assertEquals(status, "WORKING", "create ingress failed!");117 res = CommonApi.createLnIng(headers, listenerBody, httpClient);118 assertEquals(res.getCode(), 204, "Prepare listener failed!");119 NlbModel nlbModel = new NlbModel();120 nlbModel.setLoadBalancer(new LoadBalancer(instanceId));121 return nlbModel;122 }123 public NlbModel prepareIngress(Map<String, String> headers, HttpClientUtils httpClient,124 String prepareBody) {125 JSONObject jsonBody = JSONObject.parseObject(prepareBody);126 Response res = CommonApi.createLbIng(headers, jsonBody, httpClient);127 JSONObject resJson = JSONObject.parseObject(res.getHtml());128 assertEquals(res.getCode(), 200, "Prepare ingress failed!");129 String instanceId = resJson.getString("InstanceId");130 String status = CommonApi.waitIng(headers, httpClient, instanceId);131 assertEquals(status, "WORKING", "create ingress failed!");132 NlbModel nlbModel = new NlbModel();133 nlbModel.setLoadBalancer(new LoadBalancer(instanceId));134 return nlbModel;135 }136 public NlbModel prepareIngressWithoutWaiting(Map<String, String> headers,137 HttpClientUtils httpClient, String prepareBody) {138 JSONObject jsonBody = JSONObject.parseObject(prepareBody);139 Response res = CommonApi.createLbIng(headers, jsonBody, httpClient);140 JSONObject resJson = JSONObject.parseObject(res.getHtml());141 assertEquals(res.getCode(), 200, "Prepare ingress failed!");142 String instanceId = resJson.getString("InstanceId");143 NlbModel nlbModel = new NlbModel();144 nlbModel.setLoadBalancer(new LoadBalancer(instanceId));145 return nlbModel;146 }147 public NlbModel prepareCreateOrder(Map<String, String> headers, HttpClientUtils httpClient,148 String prepareBody, String tenantId) {149 headers.put("X-Account-Type", "primary");150 headers.put("X-ORIGIN-GW", "G0");151 headers.put("X-Bill-TenantId", tenantId);152 JSONObject orderBodyJson = JSONObject.parseObject(prepareBody);153 String orderId = CommonApi.createOrderAndPay(headers, orderBodyJson, httpClient);154 NlbModel nlbModel = new NlbModel();155 nlbModel.setOrder(new Order(orderId));156 return nlbModel;157 }158 public NlbModel prepareUpdateOrder(Map<String, String> headers, HttpClientUtils httpClient,159 String prepareBody, String tenantId) {160 headers.put("X-Account-Type", "primary");161 headers.put("X-ORIGIN-GW", "G0");162 headers.put("X-Bill-TenantId", tenantId);163 JSONObject orderBodyJson = JSONObject.parseObject(prepareBody);164 String orderId = CommonApi.createUpOrderAndPay(headers, orderBodyJson, httpClient);165 NlbModel nlbModel = new NlbModel();166 nlbModel.setOrder(new Order(orderId));167 return nlbModel;168 }169 public NlbModel prepareIngressAndOrder(Map<String, String> headers, HttpClientUtils httpClient,170 String prepareBody, String tenantId) {171 JSONObject jsonBody = JSONObject.parseObject(prepareBody);172 JSONObject createBody = jsonBody.getJSONObject("CreateBody");173 NlbModel nlbModel = new NlbModel();174 if (jsonBody.containsKey("OrderBody")) {175 JSONObject orderBody = jsonBody.getJSONObject("OrderBody");176 nlbModel = prepareCreateOrder(headers, httpClient, orderBody.toJSONString(), tenantId);177 }178 Response res = CommonApi.createLbIng(headers, createBody, httpClient);179 JSONObject resJson = JSONObject.parseObject(res.getHtml());180 assertEquals(res.getCode(), 200, "Prepare ingress failed!");181 String instanceId = resJson.getString("InstanceId");182 String status = CommonApi.waitIng(headers, httpClient, instanceId);183 assertEquals(status, "WORKING", "create ingress failed!");184 nlbModel.setLoadBalancer(new LoadBalancer(instanceId));185 return nlbModel;186 }187 public NlbModel prepareIngressAndUpdateOrder(Map<String, String> headers,188 HttpClientUtils httpClient, String prepareBody,189 String tenantId) {190 JSONObject jsonBody = JSONObject.parseObject(prepareBody);191 JSONObject createBody = jsonBody.getJSONObject("CreateBody");192 Response res = CommonApi.createLbIng(headers, createBody, httpClient);193 JSONObject resJson = JSONObject.parseObject(res.getHtml());194 assertEquals(res.getCode(), 200, "Prepare ingress failed!");195 String instanceId = resJson.getString("InstanceId");196 String status = CommonApi.waitIng(headers, httpClient, instanceId);197 assertEquals(status, "WORKING", "create ingress failed!");198 NlbModel nlbModel = new NlbModel();199 if (jsonBody.containsKey("OrderBody")) {200 JSONObject orderBody = jsonBody.getJSONObject("OrderBody");201 orderBody.put("InstanceId", instanceId);202 nlbModel = prepareUpdateOrder(headers, httpClient, orderBody.toJSONString(), tenantId);203 }204 nlbModel.setLoadBalancer(new LoadBalancer(instanceId));205 return nlbModel;206 }207}...