Naming micro-service - upgrade to policy decision API.
[ccsdk/apps.git] / ms / neng / src / test / java / org / onap / ccsdk / apps / ms / neng / service / extinf / impl / PolicyFinderServiceImplTest.java
index 77ec7ee..7ce03cc 100644 (file)
 
 package org.onap.ccsdk.apps.ms.neng.service.extinf.impl;
 
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.when;
+
 import com.fasterxml.jackson.databind.ObjectMapper;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InjectMocks;
 import org.mockito.Matchers;
 import org.mockito.Mock;
+import org.mockito.Mockito;
 import org.mockito.Spy;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException;
@@ -43,14 +55,6 @@ import org.springframework.web.client.HttpClientErrorException;
 import org.springframework.web.client.HttpStatusCodeException;
 import org.springframework.web.client.RestTemplate;
 
-import java.nio.charset.StandardCharsets;
-import java.util.*;
-
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.when;
-
 @RunWith(MockitoJUnitRunner.class)
 public class PolicyFinderServiceImplTest {
     @InjectMocks
@@ -67,36 +71,38 @@ public class PolicyFinderServiceImplTest {
 
     @Test
     public void testConfig() throws Exception {
-        doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any());
+        doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall( 
+            Matchers.any(), Matchers.any(), Matchers.any());
         assertNotNull(policyFinder.getConfig("policy"));
     }
     
     @Test
     public void testFindPolicy() throws Exception {
-        doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any());
+        doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall( 
+            Matchers.any(), Matchers.any(), Matchers.any());
         assertNull(policyFinder.findPolicy("policy"));
     }
 
-    @SuppressWarnings("unchecked")
     @Test(expected = NengException.class)
-    public void testHandleError_NOT_FOUND() throws Exception{
-        HttpStatusCodeException e = new HttpClientErrorException(HttpStatus.NOT_FOUND,"",null,StandardCharsets.US_ASCII);
+    public void testHandleError_Not_Found() throws Exception {
+        HttpStatusCodeException e = new HttpClientErrorException(HttpStatus.NOT_FOUND,"",
+                        null,StandardCharsets.US_ASCII);
         policyFinder.handleError(e);
-
     }
+    
     @SuppressWarnings("unchecked")
     @Test
     public void testmakeOutboundCall() throws Exception {
         Map<String, Object> configMap = buildPolicyResponse();
         Object resp = Arrays.asList(new Object[] {configMap});
         ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.OK);
-        when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
+        Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
 
         policManProps.setUrl("http://policyManager.onap.org");
 
         GetConfigRequest request = new GetConfigRequest();
         request.setPolicyName("policy");
-        GetConfigResponse configResp = policyFinder.makeOutboundCall(request, GetConfigResponse.class);
+        GetConfigResponse configResp = policyFinder.makeOutboundCall("",request, GetConfigResponse.class);
         assertNotNull(configResp);
     }
 
@@ -106,13 +112,13 @@ public class PolicyFinderServiceImplTest {
         Map<String, Object> configMap = buildPolicyResponse();
         Object resp = Arrays.asList(new Object[] {configMap});
         ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR);
-        when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
+        Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
 
         policManProps.setUrl("http://policyManager.onap.org");
 
         GetConfigRequest request = new GetConfigRequest();
         request.setPolicyName("policy");
-        policyFinder.makeOutboundCall(request, GetConfigResponse.class);
+        policyFinder.makeOutboundCall("",request, GetConfigResponse.class);
     }
 
     @Test
@@ -213,4 +219,46 @@ public class PolicyFinderServiceImplTest {
         configMap.put("config", contentMap);
         return configMap;
     }
+
+    @SuppressWarnings("unchecked")
+    @Test(expected = NengException.class)
+    public void testmakeOutboundCall_500_statusExp() throws Exception {
+        HttpClientErrorException exp = new HttpClientErrorException(HttpStatus.METHOD_NOT_ALLOWED, "{error}");
+        Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenThrow(exp);
+        policManProps.setUrl("http://policyManager.onap.org");
+        GetConfigRequest request = new GetConfigRequest();
+        request.setPolicyName("policy");
+        policyFinder.makeOutboundCall("", request, GetConfigResponse.class);
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test(expected = NengException.class)
+    public void testmakeOutboundCall_500_statusExp_notFound() throws Exception {
+        HttpClientErrorException exp = new HttpClientErrorException(HttpStatus.NOT_FOUND, "{error}");
+        Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenThrow(exp);
+        policManProps.setUrl("http://policyManager.onap.org");
+        GetConfigRequest request = new GetConfigRequest();
+        request.setPolicyName("policy");
+        policyFinder.makeOutboundCall("", request, GetConfigResponse.class);
+    }
+
+    @Test
+    public void testmakeOutboundCall_err_policy() throws Exception {
+        Map<String, Object> configMap = buildPolicyResponse();      
+        Object resp = configMap;
+        GetConfigResponse configResp = new GetConfigResponse();
+        configResp.setResponse(resp);
+        doReturn(configResp).when(policyFinder).getConfig("policy");
+        assertNull(policyFinder.findPolicy("policy"));
+    }
+    
+    @Test
+    public void testmakeOutboundCall_err_policy_empty() throws Exception {
+        Object resp = Arrays.asList(new Object[]{});
+        GetConfigResponse configResp = new GetConfigResponse();
+        configResp.setResponse(resp);
+        doReturn(configResp).when(policyFinder).getConfig("policy");
+        assertNull(policyFinder.findPolicy("policy"));
+    }
+    
 }