summaryrefslogtreecommitdiffstats
path: root/controlloop/common/model-impl/so/src/test
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@ericsson.com>2018-03-20 15:30:17 +0000
committerliamfallon <liam.fallon@ericsson.com>2018-03-25 13:40:28 +0100
commit7150a5f7027725b5eed9c723c6224c8b3d5307dd (patch)
treee89e9be3d6155b1f47fd5c6d76c5b6c78ae4ef5b /controlloop/common/model-impl/so/src/test
parentc9f47efdffe00fc5f823b1c982e6b12d92f90276 (diff)
Add timeout to SO to wait for success
The timeout implementation assumes that a finished request is a HTTP-200 together with either a request state of COMPLETE or FAILED in the embedded request in the response in the JSON. This is the same as what is done earlier in this class and in, for example, VFC. Unit test for timeout has also been added. Change-Id: I637dbecb8d230b8060f7ced76d92d11ec482503e Issue-ID: POLICY-448 Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'controlloop/common/model-impl/so/src/test')
-rw-r--r--controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/DummyWorkingMemory.java314
-rw-r--r--controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSOManager.java455
-rw-r--r--controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoDummyServer.java272
3 files changed, 821 insertions, 220 deletions
diff --git a/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/DummyWorkingMemory.java b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/DummyWorkingMemory.java
new file mode 100644
index 000000000..af7ddc426
--- /dev/null
+++ b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/DummyWorkingMemory.java
@@ -0,0 +1,314 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * so
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * 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.onap.policy.so;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.drools.core.WorkingMemory;
+import org.drools.core.WorkingMemoryEntryPoint;
+import org.drools.core.common.InternalFactHandle;
+import org.drools.core.impl.InternalKnowledgeBase;
+import org.drools.core.spi.AsyncExceptionHandler;
+import org.drools.core.spi.GlobalResolver;
+import org.kie.api.event.kiebase.KieBaseEventListener;
+import org.kie.api.event.rule.AgendaEventListener;
+import org.kie.api.event.rule.RuleRuntimeEventListener;
+import org.kie.api.runtime.Environment;
+import org.kie.api.runtime.ObjectFilter;
+import org.kie.api.runtime.process.ProcessInstance;
+import org.kie.api.runtime.process.WorkItemManager;
+import org.kie.api.runtime.rule.Agenda;
+import org.kie.api.runtime.rule.AgendaFilter;
+import org.kie.api.runtime.rule.FactHandle;
+import org.kie.api.runtime.rule.FactHandle.State;
+import org.kie.api.runtime.rule.QueryResults;
+import org.kie.api.time.SessionClock;
+
+public class DummyWorkingMemory implements WorkingMemory {
+
+ @Override
+ public void addEventListener(RuleRuntimeEventListener listener) {
+ }
+
+ @Override
+ public void removeEventListener(RuleRuntimeEventListener listener) {
+ }
+
+ @Override
+ public Collection<RuleRuntimeEventListener> getRuleRuntimeEventListeners() {
+ return null;
+ }
+
+ @Override
+ public void addEventListener(AgendaEventListener listener) {
+ }
+
+ @Override
+ public void removeEventListener(AgendaEventListener listener) {
+ }
+
+ @Override
+ public Collection<AgendaEventListener> getAgendaEventListeners() {
+ return null;
+ }
+
+ @Override
+ public void addEventListener(KieBaseEventListener listener) {
+ }
+
+ @Override
+ public void removeEventListener(KieBaseEventListener listener) {
+ }
+
+ @Override
+ public Collection<KieBaseEventListener> getKieBaseEventListeners() {
+ return null;
+ }
+
+ @Override
+ public FactHandle insert(Object object, boolean dynamic) {
+ return null;
+ }
+
+ @Override
+ public void dispose() {
+ }
+
+ @Override
+ public String getEntryPointId() {
+ return null;
+ }
+
+ @Override
+ public FactHandle insert(Object object) {
+ return null;
+ }
+
+ @Override
+ public void retract(FactHandle handle) {
+ }
+
+ @Override
+ public void delete(FactHandle handle) {
+ }
+
+ @Override
+ public void delete(FactHandle handle, State fhState) {
+
+
+ }
+
+ @Override
+ public void update(FactHandle handle, Object object) {
+ }
+
+ @Override
+ public void update(FactHandle handle, Object object, String... modifiedProperties) {
+ }
+
+ @Override
+ public Collection<? extends Object> getObjects() {
+ return null;
+ }
+
+ @Override
+ public Collection<? extends Object> getObjects(ObjectFilter filter) {
+ return null;
+ }
+
+ @Override
+ public <T extends FactHandle> Collection<T> getFactHandles() {
+ return null;
+ }
+
+ @Override
+ public <T extends FactHandle> Collection<T> getFactHandles(ObjectFilter filter) {
+ return null;
+ }
+
+ @Override
+ public long getFactCount() {
+ return 0;
+ }
+
+ @Override
+ public Agenda getAgenda() {
+ return null;
+ }
+
+ @Override
+ public void setGlobal(String identifier, Object value) {
+ }
+
+ @Override
+ public Object getGlobal(String identifier) {
+ return null;
+ }
+
+ @Override
+ public Environment getEnvironment() {
+ return null;
+ }
+
+ @Override
+ public void setGlobalResolver(GlobalResolver globalResolver) {
+ }
+
+ @Override
+ public GlobalResolver getGlobalResolver() {
+ return null;
+ }
+
+ @Override
+ public InternalKnowledgeBase getKnowledgeBase() {
+ return null;
+ }
+
+ @Override
+ public int fireAllRules() {
+ return 0;
+ }
+
+ @Override
+ public int fireAllRules(AgendaFilter agendaFilter) {
+ return 0;
+ }
+
+ @Override
+ public int fireAllRules(int fireLimit) {
+ return 0;
+ }
+
+ @Override
+ public int fireAllRules(AgendaFilter agendaFilter, int fireLimit) {
+ return 0;
+ }
+
+ @Override
+ public Object getObject(FactHandle handle) {
+ return null;
+ }
+
+ @Override
+ public FactHandle getFactHandle(Object object) {
+ return null;
+ }
+
+ @Override
+ public FactHandle getFactHandleByIdentity(Object object) {
+ return null;
+ }
+
+ @Override
+ public Iterator<?> iterateObjects() {
+ return null;
+ }
+
+ @Override
+ public Iterator<?> iterateObjects(ObjectFilter filter) {
+ return null;
+ }
+
+ @Override
+ public Iterator<InternalFactHandle> iterateFactHandles() {
+ return null;
+ }
+
+ @Override
+ public Iterator<InternalFactHandle> iterateFactHandles(ObjectFilter filter) {
+ return null;
+ }
+
+ @Override
+ public void setFocus(String focus) {
+ }
+
+ @Override
+ public QueryResults getQueryResults(String query, Object... arguments) {
+ return null;
+ }
+
+ @Override
+ public void setAsyncExceptionHandler(AsyncExceptionHandler handler) {
+ }
+
+ @Override
+ public void clearAgenda() {
+ }
+
+ @Override
+ public void clearAgendaGroup(String group) {
+ }
+
+ @Override
+ public void clearActivationGroup(String group) {
+ }
+
+ @Override
+ public void clearRuleFlowGroup(String group) {
+ }
+
+ @Override
+ public ProcessInstance startProcess(String processId) {
+ return null;
+ }
+
+ @Override
+ public ProcessInstance startProcess(String processId, Map<String, Object> parameters) {
+ return null;
+ }
+
+ @Override
+ public Collection<ProcessInstance> getProcessInstances() {
+ return null;
+ }
+
+ @Override
+ public ProcessInstance getProcessInstance(long id) {
+ return null;
+ }
+
+ @Override
+ public ProcessInstance getProcessInstance(long id, boolean readOnly) {
+ return null;
+ }
+
+ @Override
+ public WorkItemManager getWorkItemManager() {
+ return null;
+ }
+
+ @Override
+ public void halt() {
+ }
+
+ @Override
+ public WorkingMemoryEntryPoint getWorkingMemoryEntryPoint(String id) {
+ return null;
+ }
+
+ @Override
+ public SessionClock getSessionClock() {
+ return null;
+ }
+
+}
diff --git a/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSOManager.java b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSOManager.java
index a2beb57b5..864362649 100644
--- a/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSOManager.java
+++ b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSOManager.java
@@ -19,234 +19,249 @@
*/
package org.onap.policy.so;
-import static org.junit.Assert.*;
-import static org.mockito.ArgumentMatchers.anyMap;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.ArgumentMatchers.startsWith;
-import static org.mockito.Mockito.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import java.io.IOException;
+import java.net.URI;
import java.util.UUID;
import java.util.concurrent.Future;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
import org.drools.core.WorkingMemory;
-import org.junit.Before;
+import org.glassfish.grizzly.http.server.HttpServer;
+import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.drools.system.PolicyEngine;
-import org.onap.policy.rest.RESTManager;
-import org.onap.policy.rest.RESTManager.Pair;
-import org.onap.policy.so.util.Serialization;
public class TestSOManager {
- private static WorkingMemory mockedWorkingMemory;
-
- private RESTManager mockedRESTManager;
-
- private Pair<Integer, String> httpResponsePutOK;
- private Pair<Integer, String> httpResponseGetOK;
- private Pair<Integer, String> httpResponsePostOK;
- private Pair<Integer, String> httpResponseErr;
-
- private SORequest request;
- private SOResponse response;
-
- @BeforeClass
- public static void beforeTestSOManager() {
- mockedWorkingMemory = mock(WorkingMemory.class);
- }
-
- @Before
- public void setupMockedRest() {
- mockedRESTManager = mock(RESTManager.class);
-
- httpResponsePutOK = mockedRESTManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
- httpResponseGetOK = mockedRESTManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
- httpResponsePostOK = mockedRESTManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
- httpResponseErr = mockedRESTManager.new Pair<>(200, "{");
- }
-
- @Before
- public void createRequestAndResponse() {
- request = new SORequest();
- SORequestStatus requestStatus = new SORequestStatus();
- requestStatus.setRequestState("COMPLETE");
- request.setRequestStatus(requestStatus);
- request.setRequestId(UUID.randomUUID());
-
- response = new SOResponse();
-
- SORequestReferences requestReferences = new SORequestReferences();
- String requestId = UUID.randomUUID().toString();
- requestReferences.setRequestId(requestId);
- response.setRequestReferences(requestReferences);
-
- response.setRequest(request);
- }
-
- @Test
- public void testSOInitiation() {
- assertNotNull(new SOManager());
- }
-
- @Test
- public void testCreateModuleInstance() throws InterruptedException {
- SOManager manager = new SOManager();
- manager.setRestManager(mockedRESTManager);
-
- assertNull(manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "OK", request));
-
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
- .thenReturn(null);
- assertNull(manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "Null", request));
-
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Not202"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponseErr);
- assertNull(manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "Not202", request));
-
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("PutOKGetNull"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePutOK);
- when(mockedRESTManager.get(startsWith("http://somewhere.over.the.rainbow/InOz"), eq("Dorothy"), eq("PutOKGetNull"), anyMap()))
- .thenReturn(null);
- assertNull(manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "PutOKGetNull", request));
-
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("PutOKGetOK"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePutOK);
- when(mockedRESTManager.get(startsWith("http://somewhere.over.the.rainbow/InOz"), eq("Dorothy"), eq("PutOKGetOK"), anyMap()))
- .thenReturn(httpResponseGetOK);
- request.getRequestStatus().setRequestState("COMPLETE");
- SOResponse response = manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "PutOKGetOK", request);
- assertNotNull(response);
- assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
-
- response.getRequest().getRequestStatus().setRequestState("FAILED");
- Pair<Integer, String> httpResponseGetOKRequestFailed = mockedRESTManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("PutOKGetOKReqFailed"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePutOK);
- when(mockedRESTManager.get(startsWith("http://somewhere.over.the.rainbow/InOz"), eq("Dorothy"), eq("PutOKGetOKReqFailed"), anyMap()))
- .thenReturn(httpResponseGetOKRequestFailed);
- response = manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "PutOKGetOKReqFailed", request);
- assertNotNull(response);
- assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
-
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("PutOKGetBadJSON"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePutOK);
- when(mockedRESTManager.get(startsWith("http://somewhere.over.the.rainbow/InOz"), eq("Dorothy"), eq("PutOKGetBadJSON"), anyMap()))
- .thenReturn(httpResponseErr);
- assertNull(manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "PutOKGetBadJSON", request));
-
- response.getRequest().getRequestStatus().setRequestState("IN-PROGRESS");
- Pair<Integer, String> httpResponseGetOKRequestTimeout = mockedRESTManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("PutOKGetOKReqTimeout"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePutOK);
- when(mockedRESTManager.get(startsWith("http://somewhere.over.the.rainbow/InOz"), eq("Dorothy"), eq("PutOKGetOKReqTimeout"), anyMap()))
- .thenReturn(httpResponseGetOKRequestTimeout);
-
- manager.setRestGetTimeout(10);
- response = manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "PutOKGetOKReqTimeout", request);
- assertNotNull(response);
- assertEquals("IN-PROGRESS", response.getRequest().getRequestStatus().getRequestState());
- }
-
- @Test
- public void testAsyncSORestCall() throws InterruptedException {
- PolicyEngine.manager.getEnvironment().put("so.url", "http://somewhere.over.the.rainbow.null");
- PolicyEngine.manager.getEnvironment().put("so.username", "Dorothy");
- PolicyEngine.manager.getEnvironment().put("so.password", "OK");
-
- SOManager manager = new SOManager();
- manager.setRestManager(mockedRESTManager);
-
- String serviceInstanceId = UUID.randomUUID().toString();
- String vnfInstanceId = UUID.randomUUID().toString();
-
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow.null"), eq("policy"), eq("policy"), anyMap(), anyString(), anyString()))
- .thenReturn(null);
-
- Future<?> asyncRestCallFuture = manager.asyncSORestCall(request.getRequestId().toString(), mockedWorkingMemory, serviceInstanceId, vnfInstanceId, request);
- try {
- assertNull(asyncRestCallFuture.get());
- }
- catch (Exception e) {
- fail("test should not throw an exception");
- }
-
- PolicyEngine.manager.getEnvironment().put("so.url", "http://somewhere.over.the.rainbow.err");
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow.err"), eq("policy"), eq("policy"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponseErr);
-
- asyncRestCallFuture = manager.asyncSORestCall(request.getRequestId().toString(), mockedWorkingMemory, serviceInstanceId, vnfInstanceId, request);
- try {
- assertNull(asyncRestCallFuture.get());
- }
- catch (Exception e) {
- System.err.println(e);
- fail("test should not throw an exception");
- }
-
- PolicyEngine.manager.getEnvironment().put("so.url", "http://somewhere.over.the.rainbow.ok");
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow.ok"), eq("policy"), eq("policy"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePostOK);
-
- asyncRestCallFuture = manager.asyncSORestCall(request.getRequestId().toString(), mockedWorkingMemory, serviceInstanceId, vnfInstanceId, request);
- try {
- assertNull(asyncRestCallFuture.get());
- }
- catch (Exception e) {
- System.err.println(e);
- fail("test should not throw an exception");
- }
-/*
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
- .thenReturn(null);
- assertNull(manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "Null", request));
-
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Not202"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponseErr);
- assertNull(manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "Not202", request));
-
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("PutOKGetNull"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePutOK);
- when(mockedRESTManager.get(startsWith("http://somewhere.over.the.rainbow/InOz"), eq("Dorothy"), eq("PutOKGetNull"), anyMap()))
- .thenReturn(null);
- assertNull(manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "PutOKGetNull", request));
-
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("PutOKGetOK"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePutOK);
- when(mockedRESTManager.get(startsWith("http://somewhere.over.the.rainbow/InOz"), eq("Dorothy"), eq("PutOKGetOK"), anyMap()))
- .thenReturn(httpResponseGetOK);
- request.getRequestStatus().setRequestState("COMPLETE");
- SOResponse response = manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "PutOKGetOK", request);
- assertNotNull(response);
- assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
-
- response.getRequest().getRequestStatus().setRequestState("FAILED");
- Pair<Integer, String> httpResponseGetOKRequestFailed = mockedRESTManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("PutOKGetOKReqFailed"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePutOK);
- when(mockedRESTManager.get(startsWith("http://somewhere.over.the.rainbow/InOz"), eq("Dorothy"), eq("PutOKGetOKReqFailed"), anyMap()))
- .thenReturn(httpResponseGetOKRequestFailed);
- response = manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "PutOKGetOKReqFailed", request);
- assertNotNull(response);
- assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
-
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("PutOKGetBadJSON"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePutOK);
- when(mockedRESTManager.get(startsWith("http://somewhere.over.the.rainbow/InOz"), eq("Dorothy"), eq("PutOKGetBadJSON"), anyMap()))
- .thenReturn(httpResponseErr);
- assertNull(manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "PutOKGetBadJSON", request));
-
- response.getRequest().getRequestStatus().setRequestState("IN-PROGRESS");
- Pair<Integer, String> httpResponseGetOKRequestTimeout = mockedRESTManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
- when(mockedRESTManager.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("PutOKGetOKReqTimeout"), anyMap(), anyString(), anyString()))
- .thenReturn(httpResponsePutOK);
- when(mockedRESTManager.get(startsWith("http://somewhere.over.the.rainbow/InOz"), eq("Dorothy"), eq("PutOKGetOKReqTimeout"), anyMap()))
- .thenReturn(httpResponseGetOKRequestTimeout);
-
- manager.setRestGetTimeout(10);
- response = manager.createModuleInstance("http://somewhere.over.the.rainbow", "http://somewhere.over.the.rainbow/InOz", "Dorothy", "PutOKGetOKReqTimeout", request);
- assertNotNull(response);
- assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
- */
- }
+ private static final String BASE_URI = "http://localhost:46553/TestSOManager";
+ private static final String BASE_SO_URI = BASE_URI + "/SO";
+ private static HttpServer server;
+
+ @BeforeClass
+ public static void setUp() {
+ final ResourceConfig rc = new ResourceConfig(TestSoDummyServer.class);
+ server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
+ }
+
+ @AfterClass
+ public static void tearDown() throws Exception {
+ server.shutdown();
+ }
+
+ @Test
+ public void testGrizzlyServer() throws ClientProtocolException, IOException {
+ CloseableHttpClient httpclient = HttpClients.createDefault();
+ HttpGet httpGet = new HttpGet("http://localhost:46553/TestSOManager/SO/Stats");
+ CloseableHttpResponse response = httpclient.execute(httpGet);
+
+ String returnBody = EntityUtils.toString(response.getEntity(), "UTF-8");
+ assertTrue(returnBody.matches("^\\{\"GET\": [0-9]*,\"STAT\": [0-9]*,\"POST\": [0-9]*,\"PUT\": [0-9]*\\}$"));
+ }
+
+ @Test
+ public void testServiceInstantiation() throws IOException {
+ SOManager manager = new SOManager();
+ assertNotNull(manager);
+ manager.setRestGetTimeout(100);
+
+ SOResponse response = manager.createModuleInstance("http:/localhost:99999999", BASE_SO_URI, "sean", "citizen", null);
+ assertNull(response);
+
+ response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstances/v5", BASE_SO_URI, "sean", "citizen", null);
+ assertNull(response);
+
+ response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstances/v5", BASE_SO_URI, "sean", "citizen", new SORequest());
+ assertNull(response);
+
+ SORequest request = new SORequest();
+ request.setRequestId(UUID.randomUUID());
+ request.setRequestScope("Test");
+ request.setRequestType("ReturnBadJson");
+ request.setStartTime("2018-03-23 16:31");
+ request.setRequestStatus(new SORequestStatus());
+ request.getRequestStatus().setRequestState("ONGOING");
+
+ response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstances/v5", BASE_SO_URI, "sean", "citizen", request);
+ assertNull(response);
+
+ request.setRequestType("ReturnCompleted");
+ response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstances/v5", BASE_SO_URI, "sean", "citizen", request);
+ assertNotNull(response);
+ assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
+
+ request.setRequestType("ReturnFailed");
+ response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstances/v5", BASE_SO_URI, "sean", "citizen", request);
+ assertNotNull(response);
+ assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
+
+ // Use scope to set the number of iterations we'll wait for
+
+ request.setRequestType("ReturnOnging200");
+ request.setRequestScope(new Integer(10).toString());
+ response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstances/v5", BASE_SO_URI, "sean", "citizen", request);
+ assertNotNull(response);
+ assertNotNull(response.getRequest());
+ assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
+
+ request.setRequestType("ReturnOnging202");
+ request.setRequestScope(new Integer(20).toString());
+ response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstances/v5", BASE_SO_URI, "sean", "citizen", request);
+ assertNotNull(response);
+ assertNotNull(response.getRequest());
+ assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
+
+ // Test timeout after 20 attempts for a response
+ request.setRequestType("ReturnOnging202");
+ request.setRequestScope(new Integer(21).toString());
+ response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstances/v5", BASE_SO_URI, "sean", "citizen", request);
+ assertNull(response);
+
+ // Test bad response after 3 attempts for a response
+ request.setRequestType("ReturnBadAfterWait");
+ request.setRequestScope(new Integer(3).toString());
+ response = manager.createModuleInstance(BASE_SO_URI + "/serviceInstances/v5", BASE_SO_URI, "sean", "citizen", request);
+ assertNull(response);
+ }
+
+ @Test
+ public void testVfModuleCreation() throws IOException {
+ SOManager manager = new SOManager();
+ assertNotNull(manager);
+ manager.setRestGetTimeout(100);
+
+ PolicyEngine.manager.setEnvironmentProperty("so.username", "sean");
+ PolicyEngine.manager.setEnvironmentProperty("so.password", "citizen");
+
+ WorkingMemory wm = new DummyWorkingMemory();
+
+ PolicyEngine.manager.setEnvironmentProperty("so.url", "http:/localhost:99999999");
+ Future<SOResponse> asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), UUID.randomUUID().toString(), null);
+ try {
+ SOResponse response = asyncRestCallFuture.get();
+ assertEquals(999, response.getHttpResponseCode());
+ }
+ catch (Exception e) {
+ fail("test should not throw an exception");
+ }
+
+ PolicyEngine.manager.setEnvironmentProperty("so.url", BASE_SO_URI);
+ asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), UUID.randomUUID().toString(), null);
+ try {
+ SOResponse response = asyncRestCallFuture.get();
+ assertEquals(999, response.getHttpResponseCode());
+ }
+ catch (Exception e) {
+ fail("test should not throw an exception");
+ }
+
+ asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), UUID.randomUUID().toString(), new SORequest());
+ try {
+ SOResponse response = asyncRestCallFuture.get();
+ assertEquals(999, response.getHttpResponseCode());
+ }
+ catch (Exception e) {
+ fail("test should not throw an exception");
+ }
+
+ SORequest request = new SORequest();
+ request.setRequestId(UUID.randomUUID());
+ request.setRequestScope("Test");
+ request.setRequestType("ReturnBadJson");
+ request.setStartTime("2018-03-23 16:31");
+ request.setRequestStatus(new SORequestStatus());
+ request.getRequestStatus().setRequestState("ONGOING");
+
+ asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
+ try {
+ SOResponse response = asyncRestCallFuture.get();
+ assertEquals(999, response.getHttpResponseCode());
+ }
+ catch (Exception e) {
+ fail("test should not throw an exception");
+ }
+
+ request.setRequestType("ReturnCompleted");
+
+ asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
+ try {
+ SOResponse response = asyncRestCallFuture.get();
+ assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
+ }
+ catch (Exception e) {
+ fail("test should not throw an exception");
+ }
+
+ request.setRequestType("ReturnFailed");
+ asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
+ try {
+ SOResponse response = asyncRestCallFuture.get();
+ assertEquals("FAILED", response.getRequest().getRequestStatus().getRequestState());
+ }
+ catch (Exception e) {
+ fail("test should not throw an exception");
+ }
+
+ // Use scope to set the number of iterations we'll wait for
+
+ request.setRequestType("ReturnOnging200");
+ request.setRequestScope(new Integer(10).toString());
+ asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
+ try {
+ SOResponse response = asyncRestCallFuture.get();
+ assertNotNull(response.getRequest());
+ assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
+ }
+ catch (Exception e) {
+ fail("test should not throw an exception");
+ }
+
+ request.setRequestType("ReturnOnging202");
+ request.setRequestScope(new Integer(20).toString());
+ asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
+ try {
+ SOResponse response = asyncRestCallFuture.get();
+ assertNotNull(response.getRequest());
+ assertEquals("COMPLETE", response.getRequest().getRequestStatus().getRequestState());
+ }
+ catch (Exception e) {
+ fail("test should not throw an exception");
+ }
+
+ // Test timeout after 20 attempts for a response
+ request.setRequestType("ReturnOnging202");
+ request.setRequestScope(new Integer(21).toString());
+ asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
+ try {
+ SOResponse response = asyncRestCallFuture.get();
+ assertEquals(999, response.getHttpResponseCode());
+ }
+ catch (Exception e) {
+ fail("test should not throw an exception");
+ }
+
+ // Test bad response after 3 attempts for a response
+ request.setRequestType("ReturnBadAfterWait");
+ request.setRequestScope(new Integer(3).toString());
+ asyncRestCallFuture = manager.asyncSORestCall(UUID.randomUUID().toString(), wm, UUID.randomUUID().toString(), UUID.randomUUID().toString(), request);
+ try {
+ SOResponse response = asyncRestCallFuture.get();
+ assertEquals(999, response.getHttpResponseCode());
+ }
+ catch (Exception e) {
+ fail("test should not throw an exception");
+ }
+ }
}
diff --git a/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoDummyServer.java b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoDummyServer.java
new file mode 100644
index 000000000..907ca69bc
--- /dev/null
+++ b/controlloop/common/model-impl/so/src/test/java/org/onap/policy/so/TestSoDummyServer.java
@@ -0,0 +1,272 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * so
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * 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.onap.policy.so;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.core.Response;
+
+import com.google.gson.Gson;
+
+@Path("/SO")
+public class TestSoDummyServer {
+
+ private static int postMessagesReceived = 0;
+ private static int putMessagesReceived = 0;
+ private static int statMessagesReceived = 0;
+ private static int getMessagesReceived = 0;
+
+ private static Map<String, SOResponse> ongoingRequestMap = new ConcurrentHashMap<>();
+
+ @GET
+ @Path("/Stats")
+ public Response serviceGetStats() {
+ statMessagesReceived++;
+ return Response.status(200).entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
+ }
+
+ @GET
+ @Path("/OneStat/{statType}")
+ public Response serviceGetStat(@PathParam("statType") final String statType) {
+ statMessagesReceived++;
+ return Response.status(200).entity("{\"TYPE\": " + statType + "}").build();
+ }
+
+ @POST
+ @Path("/serviceInstances/v5")
+ public Response servicePostRequest(final String jsonString) {
+ postMessagesReceived++;
+
+ if (jsonString == null) {
+ return Response.status(400).build();
+ }
+
+ SORequest request = null;
+ try {
+ request = new Gson().fromJson(jsonString, SORequest.class);
+ }
+ catch (Exception e) {
+ return Response.status(400).build();
+ }
+
+ if (request == null) {
+ return Response.status(400).build();
+ }
+
+ if (request.getRequestType() == null) {
+ return Response.status(400).build();
+ }
+
+ if ("ReturnBadJson".equals(request.getRequestType())) {
+ return Response.status(200)
+ .entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}")
+ .build();
+ }
+
+ SOResponse response = new SOResponse();
+ response.setRequest(request);
+ response.setRequestReferences(new SORequestReferences());
+ response.getRequestReferences().setRequestId(request.getRequestId().toString());
+
+ if ("ReturnCompleted".equals(request.getRequestType())) {
+ response.getRequest().getRequestStatus().setRequestState("COMPLETE");
+ response.setHttpResponseCode(200);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+ if ("ReturnFailed".equals(request.getRequestType())) {
+ response.getRequest().getRequestStatus().setRequestState("FAILED");
+ response.setHttpResponseCode(200);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+ if ("ReturnOnging202".equals(request.getRequestType())) {
+ ongoingRequestMap.put(request.getRequestId().toString(), response);
+
+ response.getRequest().getRequestStatus().setRequestState("ONGOING");
+ response.setHttpResponseCode(202);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+ if ("ReturnOnging200".equals(request.getRequestType())) {
+ ongoingRequestMap.put(request.getRequestId().toString(), response);
+
+ response.getRequest().getRequestStatus().setRequestState("ONGOING");
+ response.setHttpResponseCode(200);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+
+ if ("ReturnBadAfterWait".equals(request.getRequestType())) {
+ ongoingRequestMap.put(request.getRequestId().toString(), response);
+
+ response.getRequest().getRequestStatus().setRequestState("ONGOING");
+ response.setHttpResponseCode(200);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+ return null;
+ }
+
+ @POST
+ @Path("/serviceInstances/v5/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules")
+ public Response servicePostRequestVfModules(
+ @PathParam("serviceInstanceId") final String serviceInstanceId,
+ @PathParam("vnfInstanceId") final String vnfInstanceId,
+ final String jsonString) {
+ postMessagesReceived++;
+
+ if (jsonString == null) {
+ return Response.status(400).build();
+ }
+
+ SORequest request = null;
+ try {
+ request = new Gson().fromJson(jsonString, SORequest.class);
+ }
+ catch (Exception e) {
+ return Response.status(400).build();
+ }
+
+ if (request == null) {
+ return Response.status(400).build();
+ }
+
+ if (request.getRequestType() == null) {
+ return Response.status(400).build();
+ }
+
+ if ("ReturnBadJson".equals(request.getRequestType())) {
+ return Response.status(200)
+ .entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}")
+ .build();
+ }
+
+ SOResponse response = new SOResponse();
+ response.setRequest(request);
+ response.setRequestReferences(new SORequestReferences());
+ response.getRequestReferences().setRequestId(request.getRequestId().toString());
+
+ if ("ReturnCompleted".equals(request.getRequestType())) {
+ response.getRequest().getRequestStatus().setRequestState("COMPLETE");
+ response.setHttpResponseCode(200);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+ if ("ReturnFailed".equals(request.getRequestType())) {
+ response.getRequest().getRequestStatus().setRequestState("FAILED");
+ response.setHttpResponseCode(200);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+ if ("ReturnOnging202".equals(request.getRequestType())) {
+ ongoingRequestMap.put(request.getRequestId().toString(), response);
+
+ response.getRequest().getRequestStatus().setRequestState("ONGOING");
+ response.setHttpResponseCode(202);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+ if ("ReturnOnging200".equals(request.getRequestType())) {
+ ongoingRequestMap.put(request.getRequestId().toString(), response);
+
+ response.getRequest().getRequestStatus().setRequestState("ONGOING");
+ response.setHttpResponseCode(200);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+
+ if ("ReturnBadAfterWait".equals(request.getRequestType())) {
+ ongoingRequestMap.put(request.getRequestId().toString(), response);
+
+ response.getRequest().getRequestStatus().setRequestState("ONGOING");
+ response.setHttpResponseCode(200);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+ return null;
+ }
+
+ @GET
+ @Path("/orchestrationRequests/v5/{nsInstanceId}")
+ public Response soRequestStatus(@PathParam("nsInstanceId") final String nsInstanceId) {
+
+ SOResponse response = ongoingRequestMap.get(nsInstanceId);
+
+ int iterationsLeft = Integer.valueOf(response.getRequest().getRequestScope());
+ if (--iterationsLeft > 0) {
+ response.getRequest().setRequestScope(new Integer(iterationsLeft).toString());
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+
+ ongoingRequestMap.remove(nsInstanceId);
+
+ if ("ReturnBadAfterWait".equals(response.getRequest().getRequestType())) {
+ return Response.status(400).build();
+ }
+
+ response.getRequest().getRequestStatus().setRequestState("COMPLETE");
+ response.getRequest().setRequestScope("0");
+ response.setHttpResponseCode(200);
+ String responseString = new Gson().toJson(response, SOResponse.class);
+ return Response.status(response.getHttpResponseCode())
+ .entity(responseString)
+ .build();
+ }
+}