Fixed Concurrency test 98/3498/1
authorPatrikBuhr <patrik.buhr@est.tech>
Tue, 28 Apr 2020 09:33:32 +0000 (11:33 +0200)
committerPatrikBuhr <patrik.buhr@est.tech>
Tue, 28 Apr 2020 09:36:14 +0000 (11:36 +0200)
Fixed so that controller adapter SdncOscA1Client extracts the create_schema
in the same was os the OscA1Client.

Change-Id: Ia930aca22242743631ccc26534ede2eed345cbdd
Issue-ID: NONRTRIC-195
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
policy-agent/src/main/java/org/oransc/policyagent/clients/OscA1Client.java
policy-agent/src/main/java/org/oransc/policyagent/clients/SdncJsonHelper.java
policy-agent/src/main/java/org/oransc/policyagent/clients/SdncOscA1Client.java
policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java
policy-agent/src/test/java/org/oransc/policyagent/ConcurrencyTestRunnable.java

index aaa9519..5a0bdc9 100644 (file)
@@ -23,7 +23,6 @@ package org.oransc.policyagent.clients;
 import java.lang.invoke.MethodHandles;
 import java.util.List;
 
-import org.json.JSONObject;
 import org.oransc.policyagent.configuration.RicConfig;
 import org.oransc.policyagent.repository.Policy;
 import org.slf4j.Logger;
@@ -143,7 +142,7 @@ public class OscA1Client implements A1Client {
     public Mono<String> getPolicyTypeSchema(String policyTypeId) {
         String schemaUri = uri.createGetSchemaUri(policyTypeId);
         return restClient.get(schemaUri) //
-            .flatMap(response -> getCreateSchema(response, policyTypeId));
+            .flatMap(response -> SdncJsonHelper.getCreateSchema(response, policyTypeId));
     }
 
     @Override
@@ -186,19 +185,6 @@ public class OscA1Client implements A1Client {
             .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
     }
 
-    private Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
-        try {
-            JSONObject obj = new JSONObject(policyTypeResponse);
-            JSONObject schemaObj = obj.getJSONObject("create_schema");
-            schemaObj.put(TITLE, policyTypeId);
-            return Mono.just(schemaObj.toString());
-        } catch (Exception e) {
-            String exceptionString = e.toString();
-            logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
-            return Mono.error(e);
-        }
-    }
-
     private Mono<String> deletePolicyById(String typeId, String policyId) {
         String policyUri = uri.createDeleteUri(typeId, policyId);
         return restClient.delete(policyUri);
index 4d3cbf8..d65caf1 100644 (file)
@@ -69,6 +69,19 @@ class SdncJsonHelper {
         }
     }
 
+    public static Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
+        try {
+            JSONObject obj = new JSONObject(policyTypeResponse);
+            JSONObject schemaObj = obj.getJSONObject("create_schema");
+            schemaObj.put("title", policyTypeId);
+            return Mono.just(schemaObj.toString());
+        } catch (Exception e) {
+            String exceptionString = e.toString();
+            logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, exceptionString);
+            return Mono.error(e);
+        }
+    }
+
     public static <T> String createInputJsonString(T params) {
         JsonElement paramsJson = gson.toJsonTree(params);
         JsonObject jsonObj = new JsonObject();
index c29d70e..d9536a5 100644 (file)
@@ -132,7 +132,8 @@ public class SdncOscA1Client implements A1Client {
         } else if (this.protocolType == A1ProtocolType.SDNC_OSC_OSC_V1) {
             OscA1Client.UriBuilder uri = new OscA1Client.UriBuilder(ricConfig);
             final String ricUrl = uri.createGetSchemaUri(policyTypeId);
-            return post(GET_POLICY_RPC, ricUrl, Optional.empty());
+            return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
+                .flatMap(response -> SdncJsonHelper.getCreateSchema(response, policyTypeId));
         } else {
             return Mono.error(createIllegalProtocolException());
         }
index 43cb96d..d274d5a 100644 (file)
@@ -72,7 +72,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
 import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
 import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.http.HttpStatus;
@@ -164,6 +166,12 @@ public class ApplicationTest {
             Duration checkInterval = Duration.ofMillis(1);
             return new ServiceSupervision(this.services, this.policies, this.getA1ClientFactory(), checkInterval);
         }
+
+        @Bean
+        public ServletWebServerFactory servletContainer() {
+            return new TomcatServletWebServerFactory();
+        }
+
     }
 
     @LocalServerPort
@@ -670,7 +678,7 @@ public class ApplicationTest {
         return "{\"servingCellNrcgi\":\"1\"}";
     }
 
-    // @Test TODO temporary disabled
+    @Test
     public void testConcurrency() throws Exception {
         final Instant startTime = Instant.now();
         List<Thread> threads = new ArrayList<>();
index 7f80a8e..f8f7ca3 100644 (file)
@@ -22,6 +22,7 @@ package org.oransc.policyagent;
 
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.oransc.policyagent.clients.AsyncRestClient;
 import org.oransc.policyagent.repository.ImmutablePolicy;
 import org.oransc.policyagent.repository.Policy;
 import org.oransc.policyagent.repository.PolicyType;
@@ -33,10 +34,6 @@ import org.oransc.policyagent.utils.MockA1Client;
 import org.oransc.policyagent.utils.MockA1ClientFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
-import org.springframework.web.client.RestTemplate;
 
 /**
  * Invoke operations over the NBI and start synchronizations in a separate
@@ -44,8 +41,7 @@ import org.springframework.web.client.RestTemplate;
  */
 class ConcurrencyTestRunnable implements Runnable {
     private static final Logger logger = LoggerFactory.getLogger(ConcurrencyTestRunnable.class);
-    private final RestTemplate restTemplate = new RestTemplate();
-    private final String baseUrl;
+    private final AsyncRestClient webClient;
     static AtomicInteger nextCount = new AtomicInteger(0);
     private final int count;
     private final RicSupervision supervision;
@@ -55,12 +51,12 @@ class ConcurrencyTestRunnable implements Runnable {
 
     ConcurrencyTestRunnable(String baseUrl, RicSupervision supervision, MockA1ClientFactory a1ClientFactory, Rics rics,
         PolicyTypes types) {
-        this.baseUrl = baseUrl;
         this.count = nextCount.incrementAndGet();
         this.supervision = supervision;
         this.a1ClientFactory = a1ClientFactory;
         this.rics = rics;
         this.types = types;
+        this.webClient = new AsyncRestClient(baseUrl);
     }
 
     @Override
@@ -105,29 +101,22 @@ class ConcurrencyTestRunnable implements Runnable {
     }
 
     private void listPolicies() {
-        String uri = baseUrl + "/policies";
-        restTemplate.getForObject(uri, String.class);
+        String uri = "/policies";
+        webClient.getForEntity(uri).block();
     }
 
     private void listTypes() {
-        String uri = baseUrl + "/policy_types";
-        restTemplate.getForObject(uri, String.class);
+        String uri = "/policy_types";
+        webClient.getForEntity(uri).block();
     }
 
     private void putPolicy(String name) {
-        String putUrl = baseUrl + "/policy?type=type1&id=" + name + "&ric=ric&service=service1";
-        restTemplate.put(putUrl, createJsonHttpEntity("{}"));
+        String putUrl = "/policy?type=type1&id=" + name + "&ric=ric&service=service1";
+        webClient.putForEntity(putUrl, "{}").block();
     }
 
     private void deletePolicy(String name) {
-        String deleteUrl = baseUrl + "/policy?id=" + name;
-        restTemplate.delete(deleteUrl);
+        String deleteUrl = "/policy?id=" + name;
+        webClient.delete(deleteUrl).block();
     }
-
-    private static HttpEntity<String> createJsonHttpEntity(String content) {
-        HttpHeaders headers = new HttpHeaders();
-        headers.setContentType(MediaType.APPLICATION_JSON);
-        return new HttpEntity<String>(content, headers);
-    }
-
 }