From b94b48f2cad1b161a0ece13a4100ccf4b7d0c503 Mon Sep 17 00:00:00 2001 From: ambrishest Date: Mon, 8 Jan 2024 12:13:16 +0000 Subject: [PATCH] Change in URI Versioning Issue-ID: NONRTRIC-944 Change-Id: I669f230a0fbc0d735f84ebfe2f016341524ee560 Signed-off-by: ambrishest --- test/servicestub/README.md | 6 +- test/servicestub/pom.xml | 142 ++++++++++----------- test/servicestub/service-stub-build-start.sh | 11 +- .../oran/helloworld/rest/HelloWorldController.java | 38 ------ .../nonrtric/sample/ServiceStubApplication.java} | 8 +- .../nonrtric/sample/rest/HelloWorldController.java | 69 ++++++++++ .../helloworld/rest/HelloWorldControllerTest.java | 59 --------- .../sample/rest/HelloWorldControllerTest.java | 85 ++++++++++++ 8 files changed, 235 insertions(+), 183 deletions(-) delete mode 100644 test/servicestub/src/main/java/org/oran/helloworld/rest/HelloWorldController.java rename test/servicestub/src/main/java/org/{oran/helloworld/HelloWorldApplication.java => oransc/nonrtric/sample/ServiceStubApplication.java} (79%) create mode 100644 test/servicestub/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldController.java delete mode 100644 test/servicestub/src/test/java/org/oran/helloworld/rest/HelloWorldControllerTest.java create mode 100644 test/servicestub/src/test/java/org/oransc/nonrtric/sample/rest/HelloWorldControllerTest.java diff --git a/test/servicestub/README.md b/test/servicestub/README.md index 601e52a5..b4aa791b 100644 --- a/test/servicestub/README.md +++ b/test/servicestub/README.md @@ -19,8 +19,8 @@ wait for a few seconds to ensure the Spring Boot application is fully initialize Hello World endpoint and display the response: ```bash - response=$(curl -s http://localhost:8080/helloworld/sme) - echo "Response from the Hello World endpoint:" + response=$(curl -s http://localhost:8080/helloworld/v1/sme) + echo "Response from the Hello World SME endpoint:" echo "$response" ``` @@ -33,5 +33,5 @@ To stop and remove the Docker container: ## Additional Information -- The Hello World endpoint is available at http://localhost:8080/helloworld/sme. +- The Hello World SME endpoint is available at http://localhost:8080/helloworld/v1/sme. diff --git a/test/servicestub/pom.xml b/test/servicestub/pom.xml index be7a7911..e46ae58d 100644 --- a/test/servicestub/pom.xml +++ b/test/servicestub/pom.xml @@ -1,72 +1,72 @@ - - - - - - 4.0.0 - - org.o-ran-sc.nonrtric.plt - hello-world - 0.1.0 - - - 17 - 17 - UTF-8 - - - - org.springframework.boot - spring-boot-starter-parent - 3.1.0 - - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.mockito - mockito-core - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - + + + + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.2.1 + + + org.o-ran-sc.nonrtric.plt + hello-world + 0.1.0 + + + 17 + 17 + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.mockito + mockito-core + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/test/servicestub/service-stub-build-start.sh b/test/servicestub/service-stub-build-start.sh index 885bd772..de641674 100644 --- a/test/servicestub/service-stub-build-start.sh +++ b/test/servicestub/service-stub-build-start.sh @@ -4,7 +4,7 @@ # ========================LICENSE_START================================= # O-RAN-SC # %% -# Copyright (C) 2023: OpenInfra Foundation Europe. +# Copyright (C) 2023-2024: OpenInfra Foundation Europe. # %% # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -30,11 +30,6 @@ docker run --rm -d -p 8080:8080 --name $NAME $IMAGE_NAME sleep 10 echo "Make an HTTP request to the Hello World endpoint and display the response" -response=$(curl -s http://localhost:8080/v1/helloworld/sme) - -echo "Response from the /v1/helloworld/sme endpoint: " +response=$(curl -s http://localhost:8080/helloworld/v1) +echo "Response from the /helloworld/v1 endpoint: " echo "$response" - -response2=$(curl -s http://localhost:8080/v1/helloworld) -echo "Response from the /v1/helloworld endpoint: " -echo "$response2" diff --git a/test/servicestub/src/main/java/org/oran/helloworld/rest/HelloWorldController.java b/test/servicestub/src/main/java/org/oran/helloworld/rest/HelloWorldController.java deleted file mode 100644 index 057191ec..00000000 --- a/test/servicestub/src/main/java/org/oran/helloworld/rest/HelloWorldController.java +++ /dev/null @@ -1,38 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * O-RAN-SC - * %% - * Copyright (C) 2023 OpenInfra Foundation Europe. - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ========================LICENSE_END=================================== - */ - -package org.oran.helloworld.rest; - -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/v1/helloworld") -public class HelloWorldController { - - @RequestMapping("") - public String helloWorld() { - return "Hello World from service stub\n"; - } - @RequestMapping("/sme") - public String helloWorldSme() { - return "Hello World from SME\n"; - } -} diff --git a/test/servicestub/src/main/java/org/oran/helloworld/HelloWorldApplication.java b/test/servicestub/src/main/java/org/oransc/nonrtric/sample/ServiceStubApplication.java similarity index 79% rename from test/servicestub/src/main/java/org/oran/helloworld/HelloWorldApplication.java rename to test/servicestub/src/main/java/org/oransc/nonrtric/sample/ServiceStubApplication.java index c1fe5e95..a28bec2f 100644 --- a/test/servicestub/src/main/java/org/oran/helloworld/HelloWorldApplication.java +++ b/test/servicestub/src/main/java/org/oransc/nonrtric/sample/ServiceStubApplication.java @@ -18,14 +18,14 @@ * ========================LICENSE_END=================================== */ -package org.oran.helloworld; +package org.oransc.nonrtric.sample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -@SpringBootApplication(scanBasePackages = "org.oran.helloworld") -public class HelloWorldApplication { +@SpringBootApplication(scanBasePackages = "org.oransc.nonrtric.sample") +public class ServiceStubApplication { public static void main(String[] args) { - SpringApplication.run(HelloWorldApplication.class, args); + SpringApplication.run(ServiceStubApplication.class, args); } } diff --git a/test/servicestub/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldController.java b/test/servicestub/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldController.java new file mode 100644 index 00000000..8ea37c0d --- /dev/null +++ b/test/servicestub/src/main/java/org/oransc/nonrtric/sample/rest/HelloWorldController.java @@ -0,0 +1,69 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2023-2024 OpenInfra Foundation Europe. + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.oransc.nonrtric.sample.rest; + +import jakarta.servlet.http.HttpServletRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloWorldController { + + private static final Logger logger = LoggerFactory.getLogger(HelloWorldController.class); + + @RequestMapping("/helloworld/v1") + public String helloWorld(HttpServletRequest request) { + String path = logRequestPath(request); + return "Hello from "+path; + } + + @RequestMapping("/helloworld/v1/subpath1") + public String helloWorldSubpath1(HttpServletRequest request) { + String path = logRequestPath(request); + return "Hello from "+path; + } + + @RequestMapping("/helloworld2/v1") + public String helloWorld2(HttpServletRequest request) { + String path = logRequestPath(request); + return "Hello from "+path; + } + + @RequestMapping("/helloworld/v2") + public String helloWorldV2(HttpServletRequest request) { + String path = logRequestPath(request); + return "Hello from "+path; + } + + @RequestMapping("/helloworld/v2/subpath1") + public String helloWorldSubpath1V2(HttpServletRequest request) { + String path = logRequestPath(request); + return "Hello from "+path; + } + + private String logRequestPath(HttpServletRequest request) { + String path = request.getRequestURI(); + logger.info("Received request for path: {}", path); + return path; + } +} diff --git a/test/servicestub/src/test/java/org/oran/helloworld/rest/HelloWorldControllerTest.java b/test/servicestub/src/test/java/org/oran/helloworld/rest/HelloWorldControllerTest.java deleted file mode 100644 index 36f335f4..00000000 --- a/test/servicestub/src/test/java/org/oran/helloworld/rest/HelloWorldControllerTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * O-RAN-SC - * %% - * Copyright (C) 2023 OpenInfra Foundation Europe. - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ========================LICENSE_END=================================== - */ - -package org.oran.helloworld.rest; - -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.web.servlet.MockMvc; - -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -@WebMvcTest(HelloWorldController.class) -public class HelloWorldControllerTest { - - @Autowired - private MockMvc mockMvc; - - @MockBean - private HelloWorldController helloWorldController; - - @Test - public void testHelloWorldEndpoint() throws Exception { - when(helloWorldController.helloWorld()).thenReturn("Hello World from service stub\n"); - - mockMvc.perform(get("/v1/helloworld")) - .andExpect(status().isOk()) - .andExpect(content().string("Hello World from service stub\n")); - } - - @Test - public void testHelloWorldSmeEndpoint() throws Exception { - when(helloWorldController.helloWorldSme()).thenReturn("Hello World from SME\n"); - - mockMvc.perform(get("/v1/helloworld/sme")) - .andExpect(status().isOk()) - .andExpect(content().string("Hello World from SME\n")); - } -} diff --git a/test/servicestub/src/test/java/org/oransc/nonrtric/sample/rest/HelloWorldControllerTest.java b/test/servicestub/src/test/java/org/oransc/nonrtric/sample/rest/HelloWorldControllerTest.java new file mode 100644 index 00000000..7b7abb07 --- /dev/null +++ b/test/servicestub/src/test/java/org/oransc/nonrtric/sample/rest/HelloWorldControllerTest.java @@ -0,0 +1,85 @@ +/*- + * ========================LICENSE_START================================= + * O-RAN-SC + * %% + * Copyright (C) 2023-2024 OpenInfra Foundation Europe. + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.oransc.nonrtric.sample.rest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.mock.web.MockHttpServletRequest; + +@WebMvcTest(HelloWorldController.class) +public class HelloWorldControllerTest { + + @InjectMocks + private HelloWorldController helloWorldController; + + @Test + public void testHelloWorldEndpoint() { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI("/helloworld/v1"); + + String result = helloWorldController.helloWorld(request); + + assertEquals("Hello from /helloworld/v1", result); + } + + @Test + public void testHelloWorldSubpathEndpoint() { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI("/helloworld/v1/subpath1"); + + String result = helloWorldController.helloWorldSubpath1(request); + + assertEquals("Hello from /helloworld/v1/subpath1", result); + } + + @Test + public void testHelloWorld2Endpoint() { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI("/helloworld2/v1"); + + String result = helloWorldController.helloWorld2(request); + + assertEquals("Hello from /helloworld2/v1", result); + } + + @Test + public void testHelloWorldEndpointV2() { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI("/helloworld/v2"); + + String result = helloWorldController.helloWorldV2(request); + + assertEquals("Hello from /helloworld/v2", result); + } + + @Test + public void testHelloWorldSubpathEndpointV2() { + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI("/helloworld/v2/subpath1"); + + String result = helloWorldController.helloWorldSubpath1V2(request); + + assertEquals("Hello from /helloworld/v2/subpath1", result); + } +} -- 2.16.6