diff --git a/testing-modules/jmeter-2/src/main/java/com/baeldung/sharedthreadvars/SharedThreadVarsApplication.java b/testing-modules/jmeter-2/src/main/java/com/baeldung/sharedthreadvars/SharedThreadVarsApplication.java new file mode 100644 index 000000000000..a2cf19550ae9 --- /dev/null +++ b/testing-modules/jmeter-2/src/main/java/com/baeldung/sharedthreadvars/SharedThreadVarsApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.sharedthreadvars; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SharedThreadVarsApplication { + + public static void main(String[] args) { + SpringApplication.run(SharedThreadVarsApplication.class, args); + } +} diff --git a/testing-modules/jmeter-2/src/main/java/com/baeldung/sharedthreadvars/config/SecurityConfiguration.java b/testing-modules/jmeter-2/src/main/java/com/baeldung/sharedthreadvars/config/SecurityConfiguration.java new file mode 100644 index 000000000000..ae8fac15c10e --- /dev/null +++ b/testing-modules/jmeter-2/src/main/java/com/baeldung/sharedthreadvars/config/SecurityConfiguration.java @@ -0,0 +1,19 @@ +package com.baeldung.sharedthreadvars.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer; +import org.springframework.security.web.SecurityFilterChain; + +@Configuration +public class SecurityConfiguration { + + @Bean + public SecurityFilterChain configure(HttpSecurity http) throws Exception { + http.csrf(CsrfConfigurer::disable) + .authorizeHttpRequests(auth -> auth.anyRequest() + .permitAll()); + return http.build(); + } +} diff --git a/testing-modules/jmeter-2/src/main/java/com/baeldung/sharedthreadvars/controller/RestApi.java b/testing-modules/jmeter-2/src/main/java/com/baeldung/sharedthreadvars/controller/RestApi.java new file mode 100644 index 000000000000..3a01302b5c20 --- /dev/null +++ b/testing-modules/jmeter-2/src/main/java/com/baeldung/sharedthreadvars/controller/RestApi.java @@ -0,0 +1,45 @@ +package com.baeldung.sharedthreadvars.controller; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +@RestController +@RequestMapping("/api") +public class RestApi { + + private final Logger log = LoggerFactory.getLogger(RestApi.class); + + private Map items = new HashMap<>(); + + @PostMapping + public String post(@RequestBody String name) { + UUID uuid = UUID.randomUUID(); + items.put(uuid, name); + + log.debug("put {}={}", uuid, name); + return uuid.toString(); + } + + @GetMapping + public String get(@RequestParam("uuid") String uuid) { + String name = items.remove(UUID.fromString(uuid)); + + if (name == null) + throw new ResponseStatusException(HttpStatus.EXPECTATION_FAILED); + + log.debug("get {}={}", uuid, name); + return name; + } +} diff --git a/testing-modules/jmeter-2/src/main/resources/com/baeldung/sharedthreadvars/config/bsh-shared.jmx b/testing-modules/jmeter-2/src/main/resources/com/baeldung/sharedthreadvars/config/bsh-shared.jmx new file mode 100644 index 000000000000..b08a17ca28f4 --- /dev/null +++ b/testing-modules/jmeter-2/src/main/resources/com/baeldung/sharedthreadvars/config/bsh-shared.jmx @@ -0,0 +1,136 @@ + + + + + + + + false + false + + + + 3 + 1 + true + continue + + 1 + false + + + + + localhost + 8080 + /api + true + POST + true + true + + + + false + ${__RandomString(5)} + = + + + + + + + true + + + String response = prev.getResponseDataAsString(); + +if (bsh.shared.uuidQueue == void) { + bsh.shared.uuidQueue= new LinkedList(); + log.info("--> setup: created queue"); +} +bsh.shared.uuidQueue.add(response); + +log.info("--> setup: "+response); + + java + + + + + 3 + 1 + true + continue + + 1 + false + + + + + true + + + String uuid = bsh.shared.uuidQueue.poll(); +vars.put("uuid", uuid); + +log.info("<-- worker - next: "+uuid); + + java + + + + localhost + 8080 + /api?uuid=${uuid} + true + GET + true + false + + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + + diff --git a/testing-modules/jmeter-2/src/main/resources/com/baeldung/sharedthreadvars/config/shared-file.jmx b/testing-modules/jmeter-2/src/main/resources/com/baeldung/sharedthreadvars/config/shared-file.jmx new file mode 100644 index 000000000000..0e0352a771bb --- /dev/null +++ b/testing-modules/jmeter-2/src/main/resources/com/baeldung/sharedthreadvars/config/shared-file.jmx @@ -0,0 +1,147 @@ + + + + + + + + false + false + + + + 3 + 1 + true + continue + + 1 + false + + + + + localhost + 8080 + /api + true + POST + true + true + + + + false + ${__RandomString(5)} + = + + + + + + + true + + + String response = prev.getResponseDataAsString(); +File file = new File("/tmp/shared-file"+ctx.getThreadNum()+".txt"); + +try { + FileWriter writer = new FileWriter(file); + writer.write(response); + writer.close(); + log.info("--> setup: "+response+" - "+file); +} catch (IOException e) { + log.error("Failed to write response to file", e); +} + + java + + + + + 3 + 1 + true + continue + + 1 + false + + + + + true + + + File file = new File("/tmp/shared-file"+ctx.getThreadNum()+".txt"); + +try { + Scanner reader = new Scanner(file); + String uuid = reader.hasNextLine() ? reader.nextLine().trim() : ""; + reader.close(); + + vars.put("uuid", uuid); + + log.info("<-- worker - next: "+uuid+" - "+file); +} catch (IOException e) { + log.error("Failed to read response file", e); +} + + java + + + + localhost + 8080 + /api?uuid=${uuid} + true + GET + true + false + + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + + diff --git a/testing-modules/jmeter-2/src/main/resources/com/baeldung/sharedthreadvars/config/shared-vars.jmx b/testing-modules/jmeter-2/src/main/resources/com/baeldung/sharedthreadvars/config/shared-vars.jmx new file mode 100644 index 000000000000..2f7feb47b68e --- /dev/null +++ b/testing-modules/jmeter-2/src/main/resources/com/baeldung/sharedthreadvars/config/shared-vars.jmx @@ -0,0 +1,131 @@ + + + + + + + + false + false + + + + 1 + 1 + true + continue + + 1 + false + + + + + localhost + 8080 + /api + true + POST + true + true + + + + false + ${__RandomString(5)} + = + + + + + + + true + + + String response = prev.getResponseDataAsString(); +props.put("uuid", response); + +log.info("<-- setup: "+response); + + java + + + + + 1 + 1 + true + continue + + 1 + false + + + + + true + + + String uuid = props.get("uuid"); +log.info("--> worker: "+uuid); + +vars.put("uuid", uuid); + + java + + + + localhost + 8080 + /api?uuid=${uuid} + true + GET + true + false + + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + + diff --git a/testing-modules/jmeter-2/src/test/java/com/baeldung/sharedthreadvars/RestApiIntegrationTest.java b/testing-modules/jmeter-2/src/test/java/com/baeldung/sharedthreadvars/RestApiIntegrationTest.java new file mode 100644 index 000000000000..8d6ad36922a3 --- /dev/null +++ b/testing-modules/jmeter-2/src/test/java/com/baeldung/sharedthreadvars/RestApiIntegrationTest.java @@ -0,0 +1,46 @@ +package com.baeldung.sharedthreadvars; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.Random; +import java.util.UUID; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +class RestApiIntegrationTest { + + @Autowired + MockMvc mvc; + Random random = new Random(); + + @Test + void givenRandomString_whenPost_thenCanRetrieveItViaGet() throws Exception { + String originalName = UUID.randomUUID() + .toString(); + + String uuid = mvc.perform(post("/api").contentType(MediaType.TEXT_PLAIN) + .content(originalName)) + .andExpect(status().isOk()) + .andReturn() + .getResponse() + .getContentAsString(); + + String retrievedName = mvc.perform(get("/api").param("uuid", uuid)) + .andExpect(status().isOk()) + .andReturn() + .getResponse() + .getContentAsString(); + + assertEquals(originalName, retrievedName); + } +}