--- /dev/null
+logs
+.tmp_*
\ No newline at end of file
--- /dev/null
+#!/usr/bin/env bash
+
+TC_ONELINE_DESCR="Auto test for policy agent with simulator"
+
+. ../common/testcase_common.sh $1 $2
+
+clean_containers
+
+start_simulators
+
+consul_config_app "../simulator-group/consul_cbs/config.json"
+
+start_policy_agent
+
+check_policy_agent_logs
+
+#### TEST COMPLETE ####
+
+store_logs END
+
--- /dev/null
+#!/usr/bin/env bash
+
+# Local image and tag, shall point to locally built image (non-nexus path)
+export POLICY_AGENT_LOCAL_IMAGE=oransc/policy-agent
+
+
+# Common env var for auto-test.
+
+POLICY_AGENT_PORT=8081
+POLICY_AGENT_LOGPATH="/var/log/policy-agent/application.log" #Path the application log in the policy agent container
+DOCKER_SIM_NWNAME="nonrtric-docker-net" #Name of docker private network
+CONSUL_HOST="consul-server" #Host name of consul
+CONSUL_PORT=8500 #Port number of consul
+CONFIG_BINDING_SERVICE="config-binding-service" #Host name of CBS
+PA_APP_BASE="policy-agent"
--- /dev/null
+#!/usr/bin/env bash
+
+. ../common/test_env.sh
+
+echo "Test case started as: ${BASH_SOURCE[$i+1]} "$1 $2
+
+STARTED_POLICY_AGENT="" #Policy agent app names added to this var to keep track of started container in the script
+START_ARG=$1
+IMAGE_TAG="1.0.0-SNAPSHOT"
+
+if [ $# -lt 1 ] || [ $# -gt 2 ]; then
+ echo "Expected arg: local [<image-tag>] ]| remote [<image-tag>] ]| remote-remove [<image-tag>]] | manual-container | manual-app"
+ exit 1
+elif [ $1 == "local" ]; then
+ if [ -z $POLICY_AGENT_LOCAL_IMAGE ]; then
+ echo "POLICY_AGENT_LOCAL_IMAGE not set in test_env"
+ exit 1
+ fi
+ POLICY_AGENT_IMAGE=$POLICY_AGENT_LOCAL_IMAGE":"$IMAGE_TAG
+fi
+
+# Set a description string for the test case
+if [ -z "$TC_ONELINE_DESCR" ]; then
+ TC_ONELINE_DESCR="<no-description>"
+ echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
+fi
+
+ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
+
+
+# Create the logs dir if not already created in the current dir
+if [ ! -d "logs" ]; then
+ mkdir logs
+fi
+
+TESTLOGS=$PWD/logs
+
+mkdir -p $TESTLOGS/$ATC
+
+TCLOG=$TESTLOGS/$ATC/TC.log
+exec &> >(tee ${TCLOG})
+
+#Variables for counting tests as well as passed and failed tests
+RES_TEST=0
+RES_PASS=0
+RES_FAIL=0
+TCTEST_START=$SECONDS
+
+echo "-------------------------------------------------------------------------------------------------"
+echo "----------------------------------- Test case: "$ATC
+echo "----------------------------------- Started: "$(date)
+echo "-------------------------------------------------------------------------------------------------"
+echo "-- Description: "$TC_ONELINE_DESCR
+echo "-------------------------------------------------------------------------------------------------"
+echo "----------------------------------- Test case setup -----------------------------------"
+
+
+if [ -z "$SIM_GROUP" ]; then
+ SIM_GROUP=$PWD/../simulator-group
+ if [ ! -d $SIM_GROUP ]; then
+ echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the integration repo, but failed."
+ echo "Please set the SIM_GROUP manually in the test_env.sh"
+ exit 1
+ else
+ echo "SIM_GROUP auto set to: " $SIM_GROUP
+ fi
+elif [ $SIM_GROUP = *simulator_group ]; then
+ echo "Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the integration repo, check test_env.sh"
+ exit 1
+fi
+
+echo ""
+
+if [ $1 != "manual-container" ] && [ $1 != "manual-app" ]; then
+ echo -e "Policy agent image tag set to: \033[1m" $IMAGE_TAG"\033[0m"
+ echo "Configured image for policy agent app(s) (${1}): "$POLICY_AGENT_LOCAL_IMAGE
+ tmp_im=$(docker images ${POLICY_AGENT_LOCAL_IMAGE} | grep -v REPOSITORY)
+
+ if [ $1 == "local" ]; then
+ if [ -z "$tmp_im" ]; then
+ echo "Local image (non nexus) "$POLICY_AGENT_LOCAL_IMAGE" does not exist in local registry, need to be built"
+ exit 1
+ else
+ echo -e "Policy agent local image: \033[1m"$tmp_im"\033[0m"
+ echo "If the policy agen image seem outdated, rebuild the image and run the test again."
+ fi
+ fi
+fi
+
+
+
+__consul_config() {
+
+ appname=$PA_APP_BASE
+
+ echo "Configuring consul for " $appname " from " $1
+ curl -s http://127.0.0.1:${CONSUL_PORT}/v1/kv/${appname}?dc=dc1 -X PUT -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'X-Requested-With: XMLHttpRequest' --data-binary "@"$1 >/dev/null
+}
+
+
+consul_config_app() {
+
+ __consul_config $1
+
+}
+
+# Start all simulators in the simulator group
+start_simulators() {
+
+ echo "Starting all simulators"
+ curdir=$PWD
+ cd $SIM_GROUP
+ $SIM_GROUP/simulators-start.sh
+ cd $curdir
+ echo ""
+}
+
+clean_containers() {
+ echo "Stopping all containers, policy agent app(s) and simulators with name prefix 'policy_agent'"
+ docker stop $(docker ps -q --filter name=/policy-agent) &> /dev/null
+ echo "Removing all containers, policy agent app and simulators with name prefix 'policy_agent'"
+ docker rm $(docker ps -a -q --filter name=/policy-agent) &> /dev/null
+ echo "Removing unused docker networks with substring 'policy agent' in network name"
+ docker network rm $(docker network ls -q --filter name=nonrtric)
+ echo ""
+}
+
+start_policy_agent() {
+
+ appname=$PA_APP_BASE
+
+ if [ $START_ARG == "local" ] ; then
+ __start_policy_agent_image $appname
+ fi
+}
+
+__start_policy_agent_image() {
+
+ appname=$1
+ localport=$POLICY_AGENT_PORT
+
+ echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
+
+ docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
+
+ echo "Starting policy agent: " $appname " with ports mapped to " $localport " in docker network "$DOCKER_SIM_NWNAME
+ docker run -d -p $localport":8081" --network=$DOCKER_SIM_NWNAME -e CONSUL_HOST=$CONSUL_HOST -e CONSUL_PORT=$CONSUL_PORT -e CONFIG_BINDING_SERVICE=$CONFIG_BINDING_SERVICE -e HOSTNAME=$appname --name $appname $POLICY_AGENT_IMAGE
+ #docker run -d -p 8081:8081 --network=nonrtric-docker-net -e CONSUL_HOST=CONSUL_HOST=$CONSUL_HOST -e CONSUL_PORT=$CONSUL_PORT -e CONFIG_BINDING_SERVICE=$CONFIG_BINDING_SERVICE -e HOSTNAME=policy-agent
+ sleep 3
+ set +x
+ pa_started=false
+ for i in {1..10}; do
+ if [ $(docker inspect --format '{{ .State.Running }}' $appname) ]
+ then
+ echo " Image: $(docker inspect --format '{{ .Config.Image }}' ${appname})"
+ echo "Policy Agent container ${appname} running"
+ pa_started=true
+ break
+ else
+ sleep $i
+ fi
+ done
+ if ! [ $pa_started ]; then
+ echo "Policy Agent container ${appname} could not be started"
+ exit 1
+ fi
+
+ pa_st=false
+ echo "Waiting for Policy Agent ${appname} service status..."
+ for i in {1..10}; do
+ result="$(__do_curl http://127.0.0.1:${localport}/status)"
+ if [ $? -eq 0 ]; then
+ echo "Policy Agent ${appname} responds to service status: " $result
+ pa_st=true
+ break
+ else
+ sleep $i
+ fi
+ done
+
+ if [ "$pa_st" = "false" ]; then
+ echo "Policy Agent ${appname} did not respond to service status"
+ exit 1
+ fi
+}
+
+check_policy_agent_logs() {
+
+ appname=$PA_APP_BASE
+ tmp=$(docker ps | grep $appname)
+ if ! [ -z "$tmp" ]; then #Only check logs for running policy agent apps
+ __check_policy_agent_log $appname
+ fi
+
+}
+
+__check_policy_agent_log() {
+ echo "Checking $1 log $POLICY_AGENT_LOGPATH for WARNINGs and ERRORs"
+ foundentries=$(docker exec -it $1 grep WARN /var/log/policy-agent/application.log | wc -l)
+ if [ $? -ne 0 ];then
+ echo " Problem to search $1 log $POLICY_AGENT_LOGPATH"
+ else
+ if [ $foundentries -eq 0 ]; then
+ echo " No WARN entries found in $1 log $POLICY_AGENT_LOGPATH"
+ else
+ echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $1 log $POLICY_AGENT_LOGPATH"
+ fi
+ fi
+ foundentries=$(docker exec -it $1 grep ERR $POLICY_AGENT_LOGPATH | wc -l)
+ if [ $? -ne 0 ];then
+ echo " Problem to search $1 log $POLICY_AGENT_LOGPATH"
+ else
+ if [ $foundentries -eq 0 ]; then
+ echo " No ERR entries found in $1 log $POLICY_AGENT_LOGPATH"
+ else
+ echo -e " Found \033[1m"$foundentries"\033[0m ERR entries in $1 log $POLICY_AGENT_LOGPATH"
+ fi
+ fi
+}
+
+store_logs() {
+ if [ $# != 1 ]; then
+ __print_err "need one arg, <file-prefix>"
+ exit 1
+ fi
+ echo "Storing all container logs and policy agent app log using prefix: "$1
+
+ docker logs polman_consul > $TESTLOGS/$ATC/$1_consul.log 2>&1
+ docker logs polman_cbs > $TESTLOGS/$ATC/$1_cbs.log 2>&1
+}
+
+__do_curl() {
+ res=$(curl -skw "%{http_code}" $1)
+ http_code="${res:${#res}-3}"
+ if [ ${#res} -eq 3 ]; then
+ echo "<no-response-from-server>"
+ return 1
+ else
+ if [ $http_code -lt 200 ] && [ $http_code -gt 299]; then
+ echo "<not found, resp:${http_code}>"
+ return 1
+ fi
+ if [ $# -eq 2 ]; then
+ echo "${res:0:${#res}-3}" | xargs
+ else
+ echo "${res:0:${#res}-3}"
+ fi
+
+ return 0
+ fi
+}
+
--- /dev/null
+#server = true
+#bootstrap = true
+#client_addr = "0.0.0.0"
+
+service {
+ # Name for CBS in consul, env var CONFIG_BINDING_SERVICE
+ # should be passed to policy agent app with this value
+ Name = "config-binding-service"
+ # Host name where CBS is running
+ Address = "config-binding-service"
+ # Port number where CBS is running
+ Port = 10000
+}
\ No newline at end of file
service {
# Name for CBS in consul, env var CONFIG_BINDING_SERVICE
- # should be passed to dfc app with this value
+ # should be passed to policy agent app with this value
# This is only to be used when contacting cbs via local host
- # (typicall when dfc is executed as an application without a container)
+ # (typicall when policy agent is executed as an application without a container)
Name = "config-binding-service-localhost"
# Host name where CBS is running
Address = "localhost"
ports:
- 8500:8500/tcp
volumes:
- - /Users/yonwu/ORAN/nonrtric/near-rt-ric-simulator/consul_cbs/consul:/consul/config:rw
+ - /Users/yonwu/ORAN/nonrtric/near-rt-ric-simulator/simulator-group/consul_cbs/consul:/consul/config:rw
version: '3.0'
--- /dev/null
+#!/usr/bin/env bash
+
+DOCKER_SIM_NWNAME="nonrtric-docker-net"
+echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
+docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
+
+docker-compose -f consul_cbs/docker-compose-template.yml config > docker-compose.yml
+
+docker-compose up -d
\ No newline at end of file
RUN mkdir -p /var/log/policy-agent
RUN mkdir -p /opt/app/policy-agent/etc/cert/
+EXPOSE 8081
ADD /config/application.yaml /opt/app/policy-agent/config/
ADD /target/policy-agent-1.0.0-SNAPSHOT.jar /opt/app/policy-agent/
RUN chmod -R 777 /opt/app/policy-agent/config/
-ENTRYPOINT ["/usr/bin/java", "-jar", "/opt/app/policy-agent/policy-agent-1.0.0-SNAPSHOT.jar"]
+CMD ["java", "-jar", "/opt/app/policy-agent/policy-agent-1.0.0-SNAPSHOT.jar"]
+
+
<swagger.version>2.0.0</swagger.version>
<json.version>20180130</json.version>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
+ <junit-platform-surefire-provider.version>1.3.2</junit-platform-surefire-provider.version>
<formatter-maven-plugin.version>2.8.1</formatter-maven-plugin.version>
<spotless-maven-plugin.version>1.18.0</spotless-maven-plugin.version>
<dockerfile-maven-plugin.version>1.4.13</dockerfile-maven-plugin.version>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-configuration-processor</artifactId>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>org.onap.dcaegen2.services.sdk.rest.services</groupId>
+ <artifactId>cbs-client</artifactId>
+ <version>${sdk.version}</version>
+ </dependency>
<!--REQUIRED TO GENERATE DOCUMENTATION -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
- <dependency>
- <groupId>org.onap.dcaegen2.services.sdk.rest.services</groupId>
- <artifactId>cbs-client</artifactId>
- <version>${sdk.version}</version>
- </dependency>
+ <!-- TEST -->
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-junit-jupiter</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
</dependency>
</dependencies>
<build>
<configuration>
<skipTests>false</skipTests>
</configuration>
+ <dependencies>
+ <dependency>
+ <groupId>org.junit.platform</groupId>
+ <artifactId>junit-platform-surefire-provider</artifactId>
+ <version> ${junit-platform-surefire-provider.version}</version>
+ </dependency>
+ </dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
-
import java.net.URL;
import java.util.List;
import java.util.Vector;
-
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.oransc.policyagent.configuration.ApplicationConfig;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
-
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
-
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;
import java.util.Vector;
-
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
import org.oransc.policyagent.exceptions.ServiceException;
import org.oransc.policyagent.utils.LoggingUtils;
-
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
+@ExtendWith(MockitoExtension.class)
+@RunWith(MockitoJUnitRunner.class)
public class ApplicationConfigTest {
private ApplicationConfig appConfigUnderTest;
- CbsClient cbsClient = mock(CbsClient.class);
+
+ @Mock
+ CbsClient cbsClient;
public static final ImmutableRicConfig CORRECT_RIC_CONIFG = ImmutableRicConfig.builder() //
.name("ric1") //
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.mock;
+import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import java.util.Vector;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.oransc.policyagent.clients.RicClient;
import org.oransc.policyagent.configuration.ApplicationConfig;
import org.oransc.policyagent.configuration.ImmutableRicConfig;
import org.oransc.policyagent.repository.Ric;
import org.oransc.policyagent.repository.Rics;
+@ExtendWith(MockitoExtension.class)
+@RunWith(MockitoJUnitRunner.class)
public class StartupServiceTest {
private static final String FIRST_RIC_NAME = "first";
private static final String FIRST_RIC_URL = "firstUrl";
private static final String POLICY_TYPE_1_NAME = "type1";
private static final String POLICY_TYPE_2_NAME = "type2";
+ @Mock
ApplicationConfig appConfigMock;
+ @Mock
+ RicClient ricClientMock;
+
@Test
public void startup_allOk() throws ServiceException {
- ApplicationConfig appConfigMock = mock(ApplicationConfig.class);
Vector<RicConfig> ricConfigs = new Vector<>(2);
ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A));
ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C));
secondTypes.add(type1);
PolicyType type2 = ImmutablePolicyType.builder().name(POLICY_TYPE_2_NAME).jsonSchema("{}").build();
secondTypes.add(type2);
- RicClient ricClientMock = mock(RicClient.class);
- when(ricClientMock.getPolicyTypes(FIRST_RIC_URL)).thenReturn(firstTypes);
- when(ricClientMock.getPolicyTypes("secondUrl")).thenReturn(secondTypes);
+ when(ricClientMock.getPolicyTypes(anyString())).thenReturn(firstTypes, secondTypes);
Rics rics = new Rics();
PolicyTypes policyTypes = new PolicyTypes();