Best Testsigma code snippet using com.testsigma.agent.http.ServerURLBuilder
Source:CloudAppBridge.java
1package com.testsigma.agent.tasks;2import com.testsigma.agent.config.AgentConfig;3import com.testsigma.agent.http.HttpClient;4import com.testsigma.agent.http.ServerURLBuilder;5import com.testsigma.agent.http.WebAppHttpClient;6import com.fasterxml.jackson.core.type.TypeReference;7import com.testsigma.automator.AppBridge;8import com.testsigma.automator.constants.AutomatorMessages;9import com.testsigma.automator.entity.*;10import com.testsigma.automator.exceptions.AutomatorException;11import com.testsigma.automator.http.HttpResponse;12import com.testsigma.automator.suggestion.entity.SuggestionEntity;13import lombok.RequiredArgsConstructor;14import lombok.extern.log4j.Log4j2;15import org.apache.commons.lang3.StringUtils;16import org.springframework.beans.factory.annotation.Autowired;17import org.springframework.http.HttpStatus;18import org.springframework.stereotype.Service;19import org.springframework.util.LinkedMultiValueMap;20import org.springframework.util.MultiValueMap;21import java.util.List;22@Service23@Log4j224@RequiredArgsConstructor(onConstructor = @__(@Autowired))25public class CloudAppBridge implements AppBridge {26 private final WebAppHttpClient webAppHttpClient;27 private final AgentConfig agentConfig;28 @Override29 public void postEnvironmentResult(EnvironmentRunResult environmentRunResult) throws AutomatorException {30 try {31 String endpointUrl = ServerURLBuilder.environmentResultURL(environmentRunResult.getId());32 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();33 log.info("Sending environment run results to - " + endpointUrl);34 HttpResponse<String> response = webAppHttpClient.put(endpointUrl, environmentRunResult, null, authHeader);35 log.debug("Sent environment run results to cloud servers successfully - "36 + response.getStatusCode() + " - " + response.getResponseEntity());37 } catch (Exception e) {38 log.error(e.getMessage(), e);39 throw new AutomatorException(e.getMessage(), e);40 }41 }42 @Override43 public void postTestSuiteResult(TestSuiteResult testSuiteResult) throws AutomatorException {44 try {45 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();46 webAppHttpClient.put(ServerURLBuilder.testSuiteResultURL(testSuiteResult.getId()), testSuiteResult,47 null, authHeader);48 } catch (Exception e) {49 log.error(e.getMessage(), e);50 throw new AutomatorException(e.getMessage(), e);51 }52 }53 @Override54 public void postTestCaseResult(TestCaseResult testCaseResult) throws AutomatorException {55 try {56 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();57 webAppHttpClient.put(ServerURLBuilder.testCaseResultURL(testCaseResult.getId()), testCaseResult, null,58 authHeader);59 } catch (Exception e) {60 log.error(e.getMessage(), e);61 throw new AutomatorException(e.getMessage(), e);62 }63 }64 @Override65 public void updateEnvironmentResultData(TestDeviceResultRequest testDeviceResultRequest) throws AutomatorException {66 try {67 String url = ServerURLBuilder.environmentResultUpdateURL(testDeviceResultRequest.getId());68 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();69 HttpResponse<String> response = webAppHttpClient.put(url, testDeviceResultRequest, new TypeReference<>() {70 }, authHeader);71 log.info(response.getStatusCode() + " - " + response.getResponseText());72 } catch (Exception e) {73 log.error(e.getMessage(), e);74 throw new AutomatorException(e.getMessage(), e);75 }76 }77 @Override78 public void updateTestSuiteResultData(TestSuiteResultRequest testSuiteResultRequest) throws AutomatorException {79 try {80 String url = ServerURLBuilder.testSuiteResultUpdateURL(testSuiteResultRequest.getId());81 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();82 HttpResponse<String> response = webAppHttpClient.put(url, testSuiteResultRequest, new TypeReference<>() {83 }, authHeader);84 log.error(response.getStatusCode() + " - " + response.getResponseText());85 } catch (Exception e) {86 log.error(e.getMessage(), e);87 throw new AutomatorException(e.getMessage(), e);88 }89 }90 @Override91 public void updateTestCaseResultData(TestCaseResultRequest testCaseResultRequest) throws AutomatorException {92 try {93 String url = ServerURLBuilder.testCaseResultUpdateURL(testCaseResultRequest.getId());94 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();95 HttpResponse<String> response = webAppHttpClient.put(url, testCaseResultRequest, new TypeReference<>() {96 }, authHeader);97 log.error(response.getStatusCode() + " - " + response.getResponseText());98 } catch (Exception e) {99 log.error(e.getMessage(), e);100 throw new AutomatorException(e.getMessage(), e);101 }102 }103 @Override104 public TestCaseEntity getTestCase(Long environmentResultId, TestCaseEntity testCaseEntity) throws AutomatorException {105 try {106 MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();107 if (StringUtils.isNotBlank(testCaseEntity.getTestDataSetName())) {108 queryParams.add("testDataSetName", testCaseEntity.getTestDataSetName());109 }110 queryParams.add("testCaseResultId", testCaseEntity.getTestCaseResultId().toString());111 queryParams.add("environmentResultId", environmentResultId.toString());112 String url = ServerURLBuilder.testCaseDetailsURL(testCaseEntity.getId(), queryParams);113 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();114 HttpResponse<TestCaseEntity> response = webAppHttpClient.get(url, new TypeReference<>() {115 }, authHeader);116 if (response.getStatusCode() > 200) {117 log.error("---------------- Error while fetching test case - " + response.getStatusCode());118 }119 return response.getResponseEntity();120 } catch (Exception e) {121 log.error(e.getMessage(), e);122 throw new AutomatorException(e.getMessage(), e);123 }124 }125 @Override126 public void updateElement(String name, ElementRequestEntity elementRequestEntity) throws AutomatorException {127 try {128 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();129 HttpResponse<ElementEntity> response =130 webAppHttpClient.put(ServerURLBuilder.elementURL(name), elementRequestEntity, new TypeReference<>() {131 }, authHeader);132 log.info("Element update response - " + response);133 } catch (Exception e) {134 log.error(e.getMessage(), e);135 throw new AutomatorException(e.getMessage(), e);136 }137 }138 @Override139 public String getRunTimeData(String variableName, Long environmentResultId, String sessionId) throws AutomatorException {140 try {141 MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();142 queryParams.add("environmentResultId", environmentResultId.toString());143 queryParams.add("sessionId", sessionId);144 String url = ServerURLBuilder.runTimeDataURL(variableName, queryParams);145 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();146 HttpResponse<String> response = webAppHttpClient.get(url, new TypeReference<>() {147 }, authHeader);148 if (response.getStatusCode() == HttpStatus.NOT_FOUND.value()) {149 throw new AutomatorException(AutomatorMessages.getMessage(AutomatorMessages.EXCEPTION_INVALID_TESTDATA,150 variableName));151 }152 return response.getResponseEntity();153 } catch (Exception e) {154 log.error(e.getMessage(), e);155 throw new AutomatorException(e.getMessage(), e);156 }157 }158 @Override159 public void updateRunTimeData(Long environmentResultId, RuntimeEntity runtimeEntity) throws AutomatorException {160 try {161 MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();162 queryParams.add("environmentResultId", environmentResultId.toString());163 String url = ServerURLBuilder.runTimeNewDataURL(queryParams);164 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();165 webAppHttpClient.put(url, runtimeEntity, null, authHeader);166 } catch (Exception e) {167 log.error(e.getMessage(), e);168 throw new AutomatorException(e.getMessage(), e);169 }170 }171 @Override172 public WebDriverSettingsDTO getWebDriverSettings(Long environmentResultId) throws AutomatorException {173 try {174 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();175 String url = ServerURLBuilder.capabilitiesURL(environmentResultId);176 HttpResponse<WebDriverSettingsDTO> response = webAppHttpClient.get(url, new TypeReference<>() {177 }, authHeader);178 if (response.getStatusCode() != HttpStatus.OK.value()) {179 throw new AutomatorException(response.getStatusMessage());180 }181 return response.getResponseEntity();182 } catch (Exception e) {183 log.error(e.getMessage(), e);184 throw new AutomatorException(e.getMessage(), e);185 }186 }187 @Override188 public String getDriverExecutablePath(String browserName, String browserVersion)189 throws AutomatorException {190 try {191 MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();192 queryParams.add("browserName", browserName);193 queryParams.add("browserVersion", browserVersion);194 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();195 String url = ServerURLBuilder.driverExecutableURL(queryParams, agentConfig.getUUID());196 HttpResponse<String> response = webAppHttpClient.get(url, new TypeReference<>() {197 }, authHeader);198 if (response.getStatusCode() != HttpStatus.OK.value()) {199 throw new AutomatorException(response.getStatusMessage());200 }201 return response.getResponseEntity();202 } catch (Exception e) {203 log.error(e.getMessage(), e);204 throw new AutomatorException(e.getMessage(), e);205 }206 }207 @Override208 public List<SuggestionEntity> getSuggestions(Integer naturalTextActionId) throws AutomatorException {209 try {210 String authHeader = HttpClient.BEARER + " " + agentConfig.getJwtApiKey();211 String url = ServerURLBuilder.suggestionsURL(naturalTextActionId);212 HttpResponse<List<SuggestionEntity>> response = webAppHttpClient.get(url, new TypeReference<>() {213 }, authHeader);214 if (response.getStatusCode() != HttpStatus.OK.value()) {215 throw new AutomatorException(response.getStatusMessage());216 }217 return response.getResponseEntity();218 } catch (Exception e) {219 log.error(e.getMessage(), e);220 throw new AutomatorException(e.getMessage(), e);221 }222 }223}...
Source:RootController.java
...6 */7package com.testsigma.agent.controllers;8import com.testsigma.agent.config.AgentConfig;9import com.testsigma.agent.dto.AgentDTO;10import com.testsigma.agent.http.ServerURLBuilder;11import com.testsigma.agent.http.WebAppHttpClient;12import com.testsigma.agent.services.AgentService;13import com.testsigma.agent.utils.NetworkUtil;14import com.fasterxml.jackson.core.type.TypeReference;15import com.testsigma.automator.http.HttpResponse;16import lombok.RequiredArgsConstructor;17import lombok.extern.log4j.Log4j2;18import org.springframework.beans.factory.annotation.Autowired;19import org.springframework.http.HttpStatus;20import org.springframework.stereotype.Controller;21import org.springframework.ui.Model;22import org.springframework.util.LinkedMultiValueMap;23import org.springframework.util.MultiValueMap;24import org.springframework.web.bind.annotation.GetMapping;25import org.springframework.web.bind.annotation.RequestMapping;26import org.springframework.web.bind.annotation.RequestMethod;27import org.springframework.web.bind.annotation.ResponseStatus;28import javax.servlet.http.HttpServletResponse;29@Controller30@Log4j231@RequiredArgsConstructor(onConstructor = @__(@Autowired))32public class RootController {33 private final AgentConfig agentConfig;34 private final WebAppHttpClient httpClient;35 @RequestMapping(value = {"/"}, method = RequestMethod.GET)36 public String welcomePage(Model model) throws Exception {37 try {38 String uuid = agentConfig.getUUID();39 log.debug("Fetching agent information with UUID - " + uuid);40 String authHeader = WebAppHttpClient.BEARER + " " + this.agentConfig.getJwtApiKey();41 HttpResponse<AgentDTO> response = httpClient.get(ServerURLBuilder.agentURL(uuid), new TypeReference<>() {42 }, authHeader);43 if (response.getStatusCode() == HttpStatus.OK.value()) {44 AgentDTO agentDTO = response.getResponseEntity();45 model.addAttribute("registered", this.agentConfig.getRegistered());46 model.addAttribute("agentName", agentDTO.getTitle());47 model.addAttribute("hostName", agentDTO.getHostName());48 model.addAttribute("osType", agentDTO.getOsType().getName());49 model.addAttribute("ipAddress", agentDTO.getIpAddress());50 model.addAttribute("agentVersion", agentDTO.getAgentVersion());51 } else {52 model.addAttribute("registered", false);53 }54 } catch (Exception e) {55 log.error(e.getMessage(), e);56 throw e;57 }58 return "dashboard"; //View name59 }60 @ResponseStatus(value = HttpStatus.MOVED_PERMANENTLY)61 @GetMapping(value = "/register")62 public void redirectToRegister(HttpServletResponse httpServletResponse) {63 MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();64 queryParams.add("hostName", AgentService.getComputerName());65 queryParams.add("ip", NetworkUtil.getCurrentIpAddress());66 String registerAgentLocation = ServerURLBuilder.registerAgentURL(queryParams);67 registerAgentLocation = registerAgentLocation.replace("/#", "/ui");68 httpServletResponse.setHeader("Location", registerAgentLocation);69 }70}...
Source:AgentWebServerService.java
1package com.testsigma.agent.services;2import com.testsigma.agent.config.AgentConfig;3import com.testsigma.agent.dto.AgentWebServerConfigDTO;4import com.testsigma.agent.http.ServerURLBuilder;5import com.testsigma.agent.http.WebAppHttpClient;6import com.fasterxml.jackson.core.type.TypeReference;7import com.testsigma.automator.http.HttpResponse;8import lombok.RequiredArgsConstructor;9import lombok.extern.log4j.Log4j2;10import org.springframework.beans.factory.annotation.Autowired;11import org.springframework.http.HttpStatus;12import org.springframework.stereotype.Service;13import java.io.IOException;14import java.util.UUID;15@Log4j216@Service17@RequiredArgsConstructor(onConstructor = @__(@Autowired))18public class AgentWebServerService {19 private final WebAppHttpClient httpClient;20 private final AgentConfig agentConfig;21 public AgentWebServerConfigDTO getWebServerCertificate() throws IOException {22 HttpResponse<AgentWebServerConfigDTO> response = httpClient.get(ServerURLBuilder.webServerCertificateFetchURL(),23 new TypeReference<>() {24 });25 if (response.getStatusCode() == HttpStatus.OK.value()) {26 return response.getResponseEntity();27 } else {28 log.info("Could not fetch agent web server config from testsigma servers. Response code - "29 + response.getStatusCode() + " , message - " + response.getResponseText());30 }31 return null;32 }33 public void registerLocalAgent() {34 try {35 if (!agentConfig.getRegistered() && agentConfig.getLocalAgentRegister()) {36 log.info("Triggering local agent registration since agent is not registered");37 agentConfig.setUUID(UUID.randomUUID().toString());38 agentConfig.saveConfig();39 HttpResponse<String> response = httpClient.get(ServerURLBuilder.registerLocalAgentURL(40 AgentService.getComputerName(), agentConfig.getLocalServerUrl()), new TypeReference<>() {41 });42 if (response.getStatusCode() == HttpStatus.OK.value()) {43 log.info("Agent register triggered successfully");44 } else {45 log.info("Failed to trigger local agent registration. May be server is not running in localhost. " +46 "Response code - " + response.getStatusCode() + " , message - " + response.getResponseText());47 }48 } else {49 log.info("Agent already registered...skipping local agent registration");50 }51 } catch (Exception e) {52 log.error("Failed to auto register local agent - " + e.getMessage(), e);53 }...
ServerURLBuilder
Using AI Code Generation
1import com.testsigma.agent.http.ServerURLBuilder;2import com.testsigma.agent.http.ServerURLBuilder.ServerURLBuilderException;3public class ServerURLBuilderTest {4 public static void main(String[] args) throws ServerURLBuilderException {5 System.out.println("Server URL: " + serverURLBuilder.build());6 }7}8serverURLBuilder.addPath("api");9System.out.println("Server URL: " + serverURLBuilder.build());10serverURLBuilder.addPath("api");11serverURLBuilder.addPath("v1");12serverURLBuilder.addPath("users");13System.out.println("Server URL: " + serverURLBuilder.build());14serverURLBuilder.addQueryParam("name", "John");15System.out.println("Server URL: " + serverURLBuilder.build());
ServerURLBuilder
Using AI Code Generation
1package com.testsigma.agent.http;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.Map;5public class ServerURLBuilder {6 private static final String DEFAULT_SERVER_HOST = "localhost";7 private static final int DEFAULT_SERVER_PORT = 8080;8 private static final String DEFAULT_SERVER_PROTOCOL = "http";9 private static final String DEFAULT_SERVER_PATH = "/testsigma";10 private static final String DEFAULT_SERVER_ENDPOINT = "run";11 private static final String DEFAULT_SERVER_API_VERSION = "v1";12 private String host;13 private int port;14 private String protocol;15 private String path;16 private String endpoint;17 private String apiVersion;18 public ServerURLBuilder() {19 this.host = DEFAULT_SERVER_HOST;20 this.port = DEFAULT_SERVER_PORT;21 this.protocol = DEFAULT_SERVER_PROTOCOL;22 this.path = DEFAULT_SERVER_PATH;23 this.endpoint = DEFAULT_SERVER_ENDPOINT;24 this.apiVersion = DEFAULT_SERVER_API_VERSION;25 }26 public ServerURLBuilder host(String host) {27 this.host = host;28 return this;29 }30 public ServerURLBuilder port(int port) {31 this.port = port;32 return this;33 }34 public ServerURLBuilder protocol(String protocol) {35 this.protocol = protocol;36 return this;37 }38 public ServerURLBuilder path(String path) {39 this.path = path;40 return this;41 }42 public ServerURLBuilder endpoint(String endpoint) {43 this.endpoint = endpoint;44 return this;45 }46 public ServerURLBuilder apiVersion(String apiVersion) {47 this.apiVersion = apiVersion;48 return this;49 }50 public URL build() throws MalformedURLException {51 return new URL(protocol, host, port, path + "/" + endpoint + "/" + apiVersion);52 }53}54package com.testsigma.agent.http;55import java.net.MalformedURLException;56import java.net.URL;57import java.util.Map;58public class ServerURLBuilder {59 private static final String DEFAULT_SERVER_HOST = "localhost";60 private static final int DEFAULT_SERVER_PORT = 8080;61 private static final String DEFAULT_SERVER_PROTOCOL = "http";62 private static final String DEFAULT_SERVER_PATH = "/testsigma";63 private static final String DEFAULT_SERVER_ENDPOINT = "run";64 private static final String DEFAULT_SERVER_API_VERSION = "v1";
ServerURLBuilder
Using AI Code Generation
1import com.testsigma.agent.http.ServerURLBuilder;2public class TestClass {3public static void main(String[] args) {4 String serverURL = new ServerURLBuilder()5 .withHost("localhost")6 .withPort(8080)7 .withProtocol("http")8 .withPath("/agent")9 .build();10 System.out.println("Server URL: " + serverURL);11 }12}13import com.testsigma.agent.http.ServerURLBuilder;14public class TestClass {15public static void main(String[] args) {16 String serverURL = new ServerURLBuilder()17 .withHost("localhost")18 .withPort(8080)19 .withProtocol("http")20 .withPath("/agent")21 .withQueryParams("key1", "value1", "key2", "value2")22 .build();23 System.out.println("Server URL: " + serverURL);24 }25}26import com.testsigma.agent.http.ServerURLBuilder;27public class TestClass {28public static void main(String[] args) {29 String serverURL = new ServerURLBuilder()30 .withHost("localhost")31 .withPort(8080)32 .withProtocol("http")33 .withPath("/agent")34 .withQueryParams("key1", "value1", "key2", "value2")35 .withFragment("fragment")36 .build();37 System.out.println("Server URL: " + serverURL);38 }39}40import com.testsigma.agent.http.ServerURLBuilder;41public class TestClass {42public static void main(String[] args) {43 String serverURL = new ServerURLBuilder()44 .withHost("localhost")45 .withPort(8080)46 .withProtocol("http")47 .withPath("/agent")48 .withQueryParams("
ServerURLBuilder
Using AI Code Generation
1package com.testsigma.agent.http;2import java.net.MalformedURLException;3import java.net.URL;4public class ServerURLBuilder {5private String host;6private Integer port;7private String path;8private String protocol;9public ServerURLBuilder(String host, Integer port, String path, String protocol) {10this.host = host;11this.port = port;12this.path = path;13this.protocol = protocol;14}15public URL build() throws MalformedURLException {16return new URL(url);17}18}19package com.testsigma.agent.http;20import java.net.MalformedURLException;21import java.net.URL;22public class ServerURLBuilder {23private String host;24private Integer port;25private String path;26private String protocol;27public ServerURLBuilder(String host, Integer port, String path, String protocol) {28this.host = host;29this.port = port;30this.path = path;31this.protocol = protocol;32}33public URL build() throws MalformedURLException {34return new URL(url);35}36}37package com.testsigma.agent.http;38import java.net.MalformedURLException;39import java.net.URL;40public class ServerURLBuilder {41private String host;42private Integer port;43private String path;44private String protocol;45public ServerURLBuilder(String host, Integer port, String path, String protocol) {46this.host = host;47this.port = port;48this.path = path;49this.protocol = protocol;50}51public URL build() throws MalformedURLException {52return new URL(url);53}54}
ServerURLBuilder
Using AI Code Generation
1ServerURLBuilder urlBuilder = new ServerURLBuilder();2urlBuilder.setHost("localhost");3urlBuilder.setPort(8080);4urlBuilder.setContext("testsigma");5urlBuilder.setResource("agent");6urlBuilder.setProtocol("http");7urlBuilder.setMethod("get");8String url = urlBuilder.build();9System.out.println(url);10ServerURLBuilder urlBuilder = new ServerURLBuilder();11urlBuilder.setHost("localhost");12urlBuilder.setPort(8080);13urlBuilder.setContext("testsigma");14urlBuilder.setResource("agent");15urlBuilder.setProtocol("http");16urlBuilder.setMethod("get");17urlBuilder.setParameters("id=1&name=abc");18String url = urlBuilder.build();19System.out.println(url);20ServerURLBuilder urlBuilder = new ServerURLBuilder();21urlBuilder.setHost("localhost");22urlBuilder.setPort(8080);23urlBuilder.setContext("testsigma");24urlBuilder.setResource("agent");25urlBuilder.setProtocol("http");26urlBuilder.setMethod("get");27urlBuilder.setParameters("id=1&name=abc");
ServerURLBuilder
Using AI Code Generation
1import com.testsigma.agent.http.ServerURLBuilder;2public class 2 {3 public static void main(String[] args) {4 String url = ServerURLBuilder.build("localhost", "8080", "testsigma", "2");5 System.out.println("URL: " + url);6 }7}8import com.testsigma.agent.http.ServerURLBuilder;9public class 2 {10 public static void main(String[] args) {11 String url = ServerURLBuilder.build("localhost", "8080", "testsigma", "2");12 System.out.println("URL: " + url);13 }14}15import com.testsigma.agent.http.ServerURLBuilder;16public class 2 {17 public static void main(String[] args) {18 String url = ServerURLBuilder.build("localhost", "8080", "testsigma", "2");19 System.out.println("URL: " + url);20 }21}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!