2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Modifications Copyright (C) 2018 IBM.
 
   8  * ================================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  20  * ============LICENSE_END=========================================================
 
  23 package org.onap.ccsdk.apps.ms.neng.service.extinf.impl;
 
  25 import static org.junit.Assert.assertNotNull;
 
  26 import static org.junit.Assert.assertNull;
 
  27 import static org.mockito.Mockito.doReturn;
 
  28 import static org.mockito.Mockito.when;
 
  30 import com.fasterxml.jackson.databind.ObjectMapper;
 
  31 import java.nio.charset.StandardCharsets;
 
  32 import java.util.ArrayList;
 
  33 import java.util.Arrays;
 
  34 import java.util.HashMap;
 
  35 import java.util.List;
 
  37 import org.junit.Test;
 
  38 import org.junit.runner.RunWith;
 
  39 import org.mockito.InjectMocks;
 
  40 import org.mockito.Matchers;
 
  41 import org.mockito.Mock;
 
  42 import org.mockito.Mockito;
 
  43 import org.mockito.Spy;
 
  44 import org.mockito.runners.MockitoJUnitRunner;
 
  45 import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException;
 
  46 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigRequest;
 
  47 import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigResponse;
 
  48 import org.onap.ccsdk.apps.ms.neng.core.rs.interceptors.PolicyManagerAuthorizationInterceptor;
 
  49 import org.onap.ccsdk.apps.ms.neng.extinf.props.PolicyManagerProps;
 
  50 import org.springframework.boot.web.client.RestTemplateBuilder;
 
  51 import org.springframework.http.HttpStatus;
 
  52 import org.springframework.http.RequestEntity;
 
  53 import org.springframework.http.ResponseEntity;
 
  54 import org.springframework.web.client.HttpClientErrorException;
 
  55 import org.springframework.web.client.HttpStatusCodeException;
 
  56 import org.springframework.web.client.RestTemplate;
 
  58 @RunWith(MockitoJUnitRunner.class)
 
  59 public class PolicyFinderServiceImplTest {
 
  62     PolicyFinderServiceImpl policyFinder;
 
  64     PolicyManagerProps policManProps;
 
  66     RestTemplateBuilder policyMgrRestTempBuilder;
 
  68     PolicyManagerAuthorizationInterceptor authInt;
 
  70     RestTemplate restTemplate;
 
  73     public void testConfig() throws Exception {
 
  74         doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall( 
 
  75             Matchers.any(), Matchers.any(), Matchers.any());
 
  76         assertNotNull(policyFinder.getConfig("policy"));
 
  80     public void testFindPolicy() throws Exception {
 
  81         doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall( 
 
  82             Matchers.any(), Matchers.any(), Matchers.any());
 
  83         assertNull(policyFinder.findPolicy("policy"));
 
  86     @Test(expected = NengException.class)
 
  87     public void testHandleError_Not_Found() throws Exception {
 
  88         HttpStatusCodeException e = new HttpClientErrorException(HttpStatus.NOT_FOUND,"",
 
  89                         null,StandardCharsets.US_ASCII);
 
  90         policyFinder.handleError(e);
 
  93     @SuppressWarnings("unchecked")
 
  95     public void testmakeOutboundCall() throws Exception {
 
  96         Map<String, Object> configMap = buildPolicyResponse();
 
  97         Object resp = Arrays.asList(new Object[] {configMap});
 
  98         ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.OK);
 
  99         Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
 
 101         policManProps.setUrl("http://policyManager.onap.org");
 
 103         GetConfigRequest request = new GetConfigRequest();
 
 104         request.setPolicyName("policy");
 
 105         GetConfigResponse configResp = policyFinder.makeOutboundCall("",request, GetConfigResponse.class);
 
 106         assertNotNull(configResp);
 
 109     @SuppressWarnings("unchecked")
 
 110     @Test(expected = NengException.class)
 
 111     public void testmakeOutboundCall_500() throws Exception {
 
 112         Map<String, Object> configMap = buildPolicyResponse();
 
 113         Object resp = Arrays.asList(new Object[] {configMap});
 
 114         ResponseEntity<Object> respEn = new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR);
 
 115         Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenReturn(respEn);
 
 117         policManProps.setUrl("http://policyManager.onap.org");
 
 119         GetConfigRequest request = new GetConfigRequest();
 
 120         request.setPolicyName("policy");
 
 121         policyFinder.makeOutboundCall("",request, GetConfigResponse.class);
 
 125     public void testGetRestTemplate() throws Exception {
 
 126         PolicyFinderServiceImpl service = new PolicyFinderServiceImpl();
 
 127         RestTemplateBuilder policyRestTemplateBuilder = new RestTemplateBuilder();
 
 128         service.setPolicyMgrRestTempBuilder(policyRestTemplateBuilder);
 
 129         service.setAuthInt(new PolicyManagerAuthorizationInterceptor());
 
 131         assertNotNull(service.getPolicyMgrRestTempBuilder());
 
 132         assertNotNull(service.getAuthInt());
 
 133         assertNotNull(service.getRestTemplate(Boolean.FALSE));
 
 137     public void testTransformConfigObject() throws Exception {
 
 138         String config = "{\"riskLevel\":\"4\",\"riskType\":\"test\","
 
 139                         + "\"policyName\":\"1806SriovBigJson\",\"service\":\"SDNC-GenerateName\","
 
 140                         + "\"guard\":\"False\",\"description\":\"1806SriovBigJson\","
 
 141                         + "\"templateVersion\":\"1607\",\"priority\":\"4\",\"version\":\"pannny_nnnn\","
 
 142                         + "\"content\":{\"policy-instance-name\":\"1806NameGenerationPolicyForSRIOV\","
 
 143                         + "\"naming-models\":[{\"naming-properties\":[{\"property-operation\":\"substr(5)\","
 
 144                         + "\"property-name\":\"COMPLEX\"},{\"property-name\":\"SEQUENCE\","
 
 145                         + "\"increment-sequence\":{\"max\":\"zzz\",\"scope\":\"ENTIRETY\","
 
 146                         + "\"start-value\":\"001\",\"length\":\"3\",\"increment\":\"1\","
 
 147                         + "\"sequence-type\":\"alpha-numeric\"}},{\"property-name\":\"NF_NAMING_CODE\"}],"
 
 148                         + "\"naming-type\":\"VNF\",\"nfRole\":\"vPE\","
 
 149                         + "\"naming-recipe\":\"COMPLEX|SEQUENCE|NF_NAMING_CODE\"},"
 
 150                         + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
 
 151                         + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
 
 152                         + "{\"max\":\"999\",\"scope\":\"ENTIRETY\",\"start-value\":\"001\",\"length\":\"3\","
 
 153                         + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}},"
 
 154                         + "{\"property-operation\":\"substr(-3)\",\"property-name\":\"NFC_NAMING_CODE\"}],"
 
 155                         + "\"naming-type\":\"VM\",\"nfRole\":\"vPE\","
 
 156                         + "\"naming-recipe\":\"VNF_NAME|SEQUENCE|NFC_NAMING_CODE\"},"
 
 157                         + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
 
 158                         + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
 
 159                         + "{\"property-name\":\"VF_MODULE_LABEL\"},{\"property-name\":\"VF_MODULE_TYPE\"},"
 
 160                         + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
 
 161                         + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\",\"length\":\"2\","
 
 162                         + "\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
 
 163                         + "\"naming-type\":\"VF-MODULE\",\"nfRole\":\"vPE\","
 
 164                         + "\"naming-recipe\":\"VNF_NAME|DELIMITER|VF_MODULE_LABEL|DELIMITER"
 
 165                         + "|VF_MODULE_TYPE|DELIMITER|SEQUENCE\"},"
 
 166                         + "{\"naming-properties\":[{\"property-name\":\"VF-MODULE_NAME\"},"
 
 167                         + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
 
 168                         + "{\"property-value\":\"volumegroup\",\"property-name\":\"CONSTANT\"}],"
 
 169                         + "\"naming-type\":\"VOLUME_GROUP\",\"nfRole\":\"vPE\","
 
 170                         + "\"naming-recipe\":\"VF-MODULE_NAME|DELIMITER|CONSTANT\"},"
 
 171                         + "{\"naming-properties\":[{\"property-name\":\"VOLUME_GROUP_NAME\"},"
 
 172                         + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
 
 173                         + "{\"property-value\":\"volume\",\"property-name\":\"CONSTANT\"},"
 
 174                         + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
 
 175                         + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
 
 176                         + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
 
 177                         + "\"naming-type\":\"VOLUME\",\"nfRole\":\"vPE\","
 
 178                         + "\"naming-recipe\":\"VOLUME_GROUP_NAME|DELIMITER|CONSTANT|DELIMITER|SEQUENCE\"},"
 
 179                         + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
 
 180                         + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
 
 181                         + "{\"property-value\":\"affinity\",\"property-name\":\"CONSTANT\"}],"
 
 182                         + "\"naming-type\":\"AFFINITY\",\"nfRole\":\"vPE\","
 
 183                         + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT\"},"
 
 184                         + "{\"naming-properties\":[{\"property-name\":\"VNF_NAME\"},"
 
 185                         + "{\"property-value\":\"-\",\"property-name\":\"DELIMITER\"},"
 
 186                         + "{\"property-value\":\"INT\",\"property-name\":\"CONSTANT\"},"
 
 187                         + "{\"property-name\":\"SEQUENCE\",\"increment-sequence\":"
 
 188                         + "{\"max\":\"99\",\"scope\":\"PRECEEDING\",\"start-value\":\"01\","
 
 189                         + "\"length\":\"2\",\"increment\":\"1\",\"sequence-type\":\"numeric\"}}],"
 
 190                         + "\"naming-type\":\"INTERNAL_NETWORK\",\"nfRole\":\"vPE\","
 
 191                         + "\"naming-recipe\":\"VNF_NAME|DELIMITER|CONSTANT|SEQUENCE\"}]}}";
 
 192         Map<Object, Object> configMap = new HashMap<>();
 
 193         configMap.put("config", config);
 
 194         ObjectMapper objectmapper = new ObjectMapper();
 
 195         List<Map<Object, Object>> respList = new ArrayList<>();
 
 196         respList.add(configMap);
 
 197         policyFinder.transformConfigObject(objectmapper, respList);
 
 198         assertNotNull(respList.get(0).get("config"));
 
 201     Map<String, Object> buildPolicyResponse() {
 
 202         Map<String, Object> policyDataMap = new HashMap<>();
 
 203         policyDataMap.put("policy-instance-name", "SDNC_Policy.Config_MS_VNFCNamingPolicy");
 
 204         Map<String, Object> namingModelMap = new HashMap<>();
 
 205         namingModelMap.put("nf-role", "vPE");
 
 206         namingModelMap.put("naming-type", "VNF");
 
 207         namingModelMap.put("naming-recipe", "COMPLEX|NF-NAMING-CODE|Field2|Field3|Field4");
 
 208         Map<String, Object> namingPropertyMap = new HashMap<>();
 
 209         Map<String, Object> propertyMap1 = new HashMap<>();
 
 210         propertyMap1.put("property-name", "COMPLEX");
 
 211         Map<String, Object> propertyMap2 = new HashMap<>();
 
 212         propertyMap2.put("property-name", "NF-NAMING-CODE");
 
 213         namingPropertyMap.put("", Arrays.asList(new Object[] {propertyMap1, propertyMap2}));
 
 214         namingModelMap.put("naming-properties", namingPropertyMap);
 
 215         policyDataMap.put("naming-models", Arrays.asList(new Object[] {namingModelMap}));
 
 216         Map<String, Object> configMap = new HashMap<>();
 
 217         Map<String, Object> contentMap = new HashMap<>();
 
 218         contentMap.put("content", policyDataMap);
 
 219         configMap.put("config", contentMap);
 
 223     @SuppressWarnings("unchecked")
 
 224     @Test(expected = NengException.class)
 
 225     public void testmakeOutboundCall_500_statusExp() throws Exception {
 
 226         HttpClientErrorException exp = new HttpClientErrorException(HttpStatus.METHOD_NOT_ALLOWED, "{error}");
 
 227         Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenThrow(exp);
 
 228         policManProps.setUrl("http://policyManager.onap.org");
 
 229         GetConfigRequest request = new GetConfigRequest();
 
 230         request.setPolicyName("policy");
 
 231         policyFinder.makeOutboundCall("", request, GetConfigResponse.class);
 
 234     @SuppressWarnings("unchecked")
 
 235     @Test(expected = NengException.class)
 
 236     public void testmakeOutboundCall_500_statusExp_notFound() throws Exception {
 
 237         HttpClientErrorException exp = new HttpClientErrorException(HttpStatus.NOT_FOUND, "{error}");
 
 238         Mockito.lenient().when(restTemplate.exchange(Matchers.any(RequestEntity.class), Matchers.any(Class.class))).thenThrow(exp);
 
 239         policManProps.setUrl("http://policyManager.onap.org");
 
 240         GetConfigRequest request = new GetConfigRequest();
 
 241         request.setPolicyName("policy");
 
 242         policyFinder.makeOutboundCall("", request, GetConfigResponse.class);
 
 246     public void testmakeOutboundCall_err_policy() throws Exception {
 
 247         Map<String, Object> configMap = buildPolicyResponse();      
 
 248         Object resp = configMap;
 
 249         GetConfigResponse configResp = new GetConfigResponse();
 
 250         configResp.setResponse(resp);
 
 251         doReturn(configResp).when(policyFinder).getConfig("policy");
 
 252         assertNull(policyFinder.findPolicy("policy"));
 
 256     public void testmakeOutboundCall_err_policy_empty() throws Exception {
 
 257         Object resp = Arrays.asList(new Object[]{});
 
 258         GetConfigResponse configResp = new GetConfigResponse();
 
 259         configResp.setResponse(resp);
 
 260         doReturn(configResp).when(policyFinder).getConfig("policy");
 
 261         assertNull(policyFinder.findPolicy("policy"));