From d30570e0aef589a471d12762b801f9354700c38c Mon Sep 17 00:00:00 2001 From: ambrishest Date: Thu, 7 Dec 2023 08:33:45 +0000 Subject: [PATCH] Hello World service for NONRTRIC - Unit Tests Added Issue-ID: NONRTRIC-944 Change-Id: I59122a202746587a34d461ef84fd331bf7c5a73e Signed-off-by: ambrishest --- test/cr/cr-build-start.sh | 2 +- test/mrstub/mrstub-build-start.sh | 2 +- test/prodstub/prod-stub-build-start.sh | 2 +- test/servicestub/.gitignore | 38 +++++++++ test/servicestub/Dockerfile | 47 +++++++++++ test/servicestub/README.md | 37 +++++++++ test/servicestub/build.sh | 96 ++++++++++++++++++++++ test/servicestub/container-tag.yaml | 25 ++++++ test/servicestub/docker-compose.yml | 29 +++++++ test/servicestub/hello-world-chart/.helmignore | 23 ++++++ test/servicestub/hello-world-chart/Chart.yaml | 44 ++++++++++ .../hello-world-chart/templates/_helpers.tpl | 62 ++++++++++++++ .../hello-world-chart/templates/deployment.yaml | 43 ++++++++++ .../hello-world-chart/templates/service.yaml | 37 +++++++++ .../templates/tests/test-connection.yaml | 35 ++++++++ test/servicestub/hello-world-chart/values.yaml | 29 +++++++ test/servicestub/pom.xml | 72 ++++++++++++++++ test/servicestub/service-stub-build-start.sh | 40 +++++++++ .../org/oran/helloworld/HelloWorldApplication.java | 31 +++++++ .../oran/helloworld/rest/HelloWorldController.java | 38 +++++++++ .../helloworld/rest/HelloWorldControllerTest.java | 59 +++++++++++++ 21 files changed, 788 insertions(+), 3 deletions(-) create mode 100644 test/servicestub/.gitignore create mode 100644 test/servicestub/Dockerfile create mode 100644 test/servicestub/README.md create mode 100644 test/servicestub/build.sh create mode 100644 test/servicestub/container-tag.yaml create mode 100644 test/servicestub/docker-compose.yml create mode 100644 test/servicestub/hello-world-chart/.helmignore create mode 100644 test/servicestub/hello-world-chart/Chart.yaml create mode 100644 test/servicestub/hello-world-chart/templates/_helpers.tpl create mode 100644 test/servicestub/hello-world-chart/templates/deployment.yaml create mode 100644 test/servicestub/hello-world-chart/templates/service.yaml create mode 100644 test/servicestub/hello-world-chart/templates/tests/test-connection.yaml create mode 100644 test/servicestub/hello-world-chart/values.yaml create mode 100644 test/servicestub/pom.xml create mode 100644 test/servicestub/service-stub-build-start.sh create mode 100644 test/servicestub/src/main/java/org/oran/helloworld/HelloWorldApplication.java create mode 100644 test/servicestub/src/main/java/org/oran/helloworld/rest/HelloWorldController.java create mode 100644 test/servicestub/src/test/java/org/oran/helloworld/rest/HelloWorldControllerTest.java diff --git a/test/cr/cr-build-start.sh b/test/cr/cr-build-start.sh index 03e9ed29..000cd52a 100755 --- a/test/cr/cr-build-start.sh +++ b/test/cr/cr-build-start.sh @@ -19,6 +19,6 @@ #Builds the callback receiver container and starts it in interactive mode -docker build --build-arg NEXUS_PROXY_REPO=nexus3.onap.org:10001/ -t callback-receiver . +docker build -t callback-receiver . docker run --rm -it -p 8090:8090 -p 8091:8091 --name cr callback-receiver diff --git a/test/mrstub/mrstub-build-start.sh b/test/mrstub/mrstub-build-start.sh index 1874f970..c2451721 100755 --- a/test/mrstub/mrstub-build-start.sh +++ b/test/mrstub/mrstub-build-start.sh @@ -19,6 +19,6 @@ #Builds the mrstub container and starts it in interactive mode -docker build --build-arg NEXUS_PROXY_REPO=nexus3.onap.org:10001/ -t mrstub . +docker build -t mrstub . docker run --rm --name=dmaap-mr -it -p 3904:3904 -p 3905:3905 -v "$PWD/cert:/usr/src/app/cert" mrstub diff --git a/test/prodstub/prod-stub-build-start.sh b/test/prodstub/prod-stub-build-start.sh index 89cc967d..d10626ed 100755 --- a/test/prodstub/prod-stub-build-start.sh +++ b/test/prodstub/prod-stub-build-start.sh @@ -22,6 +22,6 @@ NAME="producer-stub-test" IMAGE_NAME="producer-stub-test-image" -docker build --build-arg NEXUS_PROXY_REPO=nexus3.onap.org:10001/ -t $IMAGE_NAME . +docker build -t $IMAGE_NAME . docker run --rm -it -p 8992:8092 -p 8993:8093 --name $NAME $IMAGE_NAME diff --git a/test/servicestub/.gitignore b/test/servicestub/.gitignore new file mode 100644 index 00000000..5ff6309b --- /dev/null +++ b/test/servicestub/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/test/servicestub/Dockerfile b/test/servicestub/Dockerfile new file mode 100644 index 00000000..fb942d8b --- /dev/null +++ b/test/servicestub/Dockerfile @@ -0,0 +1,47 @@ +#================================================================================== +# 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. +# +# This source code is part of the near-RT RIC (RAN Intelligent Controller) +# platform project (RICP). +#================================================================================== + + +# Use Maven image with OpenJDK 17 for the build stage +FROM maven:3.8.5-openjdk-17 AS maven_build + +# Copy Maven project files +COPY pom.xml /tmp/ +COPY src /tmp/src/ + +# Set working directory +WORKDIR /tmp/ + +# Build the Maven project +RUN mvn package + +# Use a separate image with OpenJDK 17 for the runtime stage +FROM openjdk:17-jdk-slim + +# Expose port 8080 +EXPOSE 8080 + +# Set the working directory +WORKDIR /app + +# Copy the JAR file from the maven_build stage to the runtime stage +COPY --from=maven_build /tmp/target/hello-world-0.1.0.jar /app/hello-world-0.1.0.jar + +# Command to run the application +CMD ["java", "-jar", "hello-world-0.1.0.jar"] diff --git a/test/servicestub/README.md b/test/servicestub/README.md new file mode 100644 index 00000000..601e52a5 --- /dev/null +++ b/test/servicestub/README.md @@ -0,0 +1,37 @@ +# Hello World Service Stub + +This repository contains a Spring Boot application serving a Hello World endpoint. The application can be built and +run using the provided script - ``service-stub-build-start.sh``. + +## Prerequisites + +- Docker installed on your machine. + +## Building and Running the Application +Run the script: + +```bash + ./service-stub-build-start.sh +``` + +The script will build a Docker image and run a container with the Hello World service. After the container starts, +wait for a few seconds to ensure the Spring Boot application is fully initialized. Next, it will make an HTTP request to the +Hello World endpoint and display the response: + +```bash + response=$(curl -s http://localhost:8080/helloworld/sme) + echo "Response from the Hello World endpoint:" + echo "$response" +``` + +To stop and remove the Docker container: + +```bash + docker stop service-stub-hello-world-test + docker rm service-stub-hello-world-test +``` + +## Additional Information + +- The Hello World endpoint is available at http://localhost:8080/helloworld/sme. + diff --git a/test/servicestub/build.sh b/test/servicestub/build.sh new file mode 100644 index 00000000..bb0ff80a --- /dev/null +++ b/test/servicestub/build.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# ============LICENSE_START=============================================== +# 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================================================= +# + +# Build image from Dockerfile with/without custom image tag +# Optionally push to external image repo + +print_usage() { + echo "Usage: build.sh no-push| []" + exit 1 +} + +if [ $# -ne 1 ] && [ $# -ne 2 ]; then + print_usage +fi + +IMAGE_NAME="nonrtric-hello-world" +IMAGE_TAG="latest" +REPO="" +if [ $1 == "no-push" ]; then + echo "Only local image build" +else + REPO=$1 + echo "Attempt to push built image to: "$REPO +fi + +shift +while [ $# -ne 0 ]; do + if [ $1 == "--tag" ]; then + shift + if [ -z "$1" ]; then + print_usage + fi + IMAGE_TAG=$1 + echo "Setting image tag to: "$IMAGE_TAG + shift + else + echo "Unknown parameter: $1" + print_usage + fi +done + +IMAGE=$IMAGE_NAME:$IMAGE_TAG + +export DOCKER_DEFAULT_PLATFORM=linux/amd64 +CURRENT_PLATFORM=$(docker system info --format '{{.OSType}}/{{.Architecture}}') +if [ $CURRENT_PLATFORM != $DOCKER_DEFAULT_PLATFORM ]; then + echo "Image may not work on the current platform: $CURRENT_PLATFORM, only platform $DOCKER_DEFAULT_PLATFORM supported" +fi + +echo "Building image $IMAGE" +docker build -t $IMAGE_NAME:$IMAGE_TAG . +if [ $? -ne 0 ]; then + echo "BUILD FAILED" + exit 1 +fi +echo "BUILD OK" + +if [ "$REPO" != "" ]; then + echo "Tagging image" + NEW_IMAGE=$REPO/$IMAGE_NAME:$IMAGE_TAG + docker tag $IMAGE $NEW_IMAGE + if [ $? -ne 0 ]; then + echo "RE-TAGGING FAILED" + exit 1 + fi + echo "RE-TAG OK" + + echo "Pushing image $NEW_IMAGE" + docker push $NEW_IMAGE + if [ $? -ne 0 ]; then + echo "PUSHED FAILED" + echo " Perhaps not logged into docker-hub repo $REPO?" + exit 1 + fi + IMAGE=$NEW_IMAGE + echo "PUSH OK" +fi + +echo "IMAGE OK: $IMAGE" +echo "DONE" diff --git a/test/servicestub/container-tag.yaml b/test/servicestub/container-tag.yaml new file mode 100644 index 00000000..d9113881 --- /dev/null +++ b/test/servicestub/container-tag.yaml @@ -0,0 +1,25 @@ +# - +# ========================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=================================== +# + +# The Jenkins job requires a tag to build the Docker image. +# By default this file is in the docker build directory, +# but the location can configured in the JJB template. +--- +tag: 0.1.0 diff --git a/test/servicestub/docker-compose.yml b/test/servicestub/docker-compose.yml new file mode 100644 index 00000000..b137ec16 --- /dev/null +++ b/test/servicestub/docker-compose.yml @@ -0,0 +1,29 @@ +# - +# ========================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=================================== +# + +version: "3.0" + +services: + hello-world: + build: + context: ./ + dockerfile: Dockerfile + ports: + - 8080:8080 \ No newline at end of file diff --git a/test/servicestub/hello-world-chart/.helmignore b/test/servicestub/hello-world-chart/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/test/servicestub/hello-world-chart/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/test/servicestub/hello-world-chart/Chart.yaml b/test/servicestub/hello-world-chart/Chart.yaml new file mode 100644 index 00000000..3f5d2006 --- /dev/null +++ b/test/servicestub/hello-world-chart/Chart.yaml @@ -0,0 +1,44 @@ +# - +# ========================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=================================== +# + +apiVersion: v2 +name: hello-world-chart +description: A Helm chart for NONRTRIC Hello World + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "0.1.0" diff --git a/test/servicestub/hello-world-chart/templates/_helpers.tpl b/test/servicestub/hello-world-chart/templates/_helpers.tpl new file mode 100644 index 00000000..5759827d --- /dev/null +++ b/test/servicestub/hello-world-chart/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "hello-world-chart.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "hello-world-chart.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "hello-world-chart.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "hello-world-chart.labels" -}} +helm.sh/chart: {{ include "hello-world-chart.chart" . }} +{{ include "hello-world-chart.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "hello-world-chart.selectorLabels" -}} +app.kubernetes.io/name: {{ include "hello-world-chart.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "hello-world-chart.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "hello-world-chart.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/test/servicestub/hello-world-chart/templates/deployment.yaml b/test/servicestub/hello-world-chart/templates/deployment.yaml new file mode 100644 index 00000000..57f53579 --- /dev/null +++ b/test/servicestub/hello-world-chart/templates/deployment.yaml @@ -0,0 +1,43 @@ +# - +# ========================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=================================== +# + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "hello-world-chart.fullname" . }} + labels: + {{- include "hello-world-chart.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "hello-world-chart.selectorLabels" . | nindent 8 }} + template: + metadata: + labels: + {{- include "hello-world-chart.selectorLabels" . | nindent 12 }} + spec: + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + ports: + - name: http + containerPort: {{ .Values.service.port }} + diff --git a/test/servicestub/hello-world-chart/templates/service.yaml b/test/servicestub/hello-world-chart/templates/service.yaml new file mode 100644 index 00000000..fdd91440 --- /dev/null +++ b/test/servicestub/hello-world-chart/templates/service.yaml @@ -0,0 +1,37 @@ +# - +# ========================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=================================== +# + +apiVersion: v1 +kind: Service +metadata: + name: {{ include "hello-world-chart.fullname" . }} + labels: + {{- include "hello-world-chart.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + nodePort: 30951 + selector: + {{- include "hello-world-chart.selectorLabels" . | nindent 4 }} + diff --git a/test/servicestub/hello-world-chart/templates/tests/test-connection.yaml b/test/servicestub/hello-world-chart/templates/tests/test-connection.yaml new file mode 100644 index 00000000..a8b1698f --- /dev/null +++ b/test/servicestub/hello-world-chart/templates/tests/test-connection.yaml @@ -0,0 +1,35 @@ +# - +# ========================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=================================== +# + +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "hello-world-chart.fullname" . }}-test-connection" + labels: + {{- include "hello-world-chart.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "hello-world-chart.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/test/servicestub/hello-world-chart/values.yaml b/test/servicestub/hello-world-chart/values.yaml new file mode 100644 index 00000000..8856b1cb --- /dev/null +++ b/test/servicestub/hello-world-chart/values.yaml @@ -0,0 +1,29 @@ +# - +# ========================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=================================== +# + +image: + repository: nonrtric-hello-world + tag: latest + +service: + name: hello-world + type: NodePort + port: 8080 + diff --git a/test/servicestub/pom.xml b/test/servicestub/pom.xml new file mode 100644 index 00000000..be7a7911 --- /dev/null +++ b/test/servicestub/pom.xml @@ -0,0 +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 + + + + + \ No newline at end of file diff --git a/test/servicestub/service-stub-build-start.sh b/test/servicestub/service-stub-build-start.sh new file mode 100644 index 00000000..b798e867 --- /dev/null +++ b/test/servicestub/service-stub-build-start.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# - +# ========================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=================================== +# + +NAME="service-stub-hello-world-test" +IMAGE_NAME="nonrtric-hello-world" + +docker build -t $IMAGE_NAME:latest . + +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/helloworld/sme) + +echo "Response from the /helloworld/sme endpoint: " +echo "$response" + +response2=$(curl -s http://localhost:8080/helloworld) +echo "Response from the /helloworld endpoint: " +echo "$response2" diff --git a/test/servicestub/src/main/java/org/oran/helloworld/HelloWorldApplication.java b/test/servicestub/src/main/java/org/oran/helloworld/HelloWorldApplication.java new file mode 100644 index 00000000..c1fe5e95 --- /dev/null +++ b/test/servicestub/src/main/java/org/oran/helloworld/HelloWorldApplication.java @@ -0,0 +1,31 @@ +/*- + * ========================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; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication(scanBasePackages = "org.oran.helloworld") +public class HelloWorldApplication { + public static void main(String[] args) { + SpringApplication.run(HelloWorldApplication.class, args); + } +} 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 new file mode 100644 index 00000000..994e1513 --- /dev/null +++ b/test/servicestub/src/main/java/org/oran/helloworld/rest/HelloWorldController.java @@ -0,0 +1,38 @@ +/*- + * ========================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("/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/test/java/org/oran/helloworld/rest/HelloWorldControllerTest.java b/test/servicestub/src/test/java/org/oran/helloworld/rest/HelloWorldControllerTest.java new file mode 100644 index 00000000..0d29c8bd --- /dev/null +++ b/test/servicestub/src/test/java/org/oran/helloworld/rest/HelloWorldControllerTest.java @@ -0,0 +1,59 @@ +/*- + * ========================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("/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("/helloworld/sme")) + .andExpect(status().isOk()) + .andExpect(content().string("Hello World from SME\n")); + } +} -- 2.16.6