2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  20 package org.onap.policy.pap.xacml.rest.controller;
 
  22 import static org.junit.Assert.*;
 
  23 import static org.mockito.Mockito.mock;
 
  24 import static org.mockito.Mockito.when;
 
  26 import java.io.BufferedReader;
 
  27 import java.io.StringReader;
 
  28 import java.io.UnsupportedEncodingException;
 
  30 import javax.servlet.http.HttpServletRequest;
 
  32 import org.junit.After;
 
  33 import org.junit.Before;
 
  34 import org.junit.Test;
 
  35 import org.mockito.Mockito;
 
  36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 
  37 import org.onap.policy.common.logging.flexlogger.Logger;
 
  38 import org.onap.policy.rest.dao.CommonClassDao;
 
  39 import org.springframework.mock.web.MockHttpServletResponse;
 
  41 public class DecisionPolicyDictionaryControllerTest {
 
  42         private static Logger logger = FlexLogger.getLogger(DecisionPolicyDictionaryControllerTest.class);
 
  43         private static CommonClassDao commonClassDao;
 
  44         private String jsonString = null;
 
  45         private HttpServletRequest request = null;
 
  46         private DecisionPolicyDictionaryController controller = null;
 
  49         public void setUp() throws Exception {
 
  50                 logger.info("setUp: Entering");
 
  51         commonClassDao = Mockito.mock(CommonClassDao.class);
 
  52             HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
 
  54                 jsonString = "{\"attributeDictionaryDatas\": {\"error\": \"\",  \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
 
  55                                 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
 
  56                                 + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
 
  57                                 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
 
  58                                 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
 
  59                                 + "     \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
 
  60                                 + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
 
  61                                 + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
 
  63         BufferedReader br = new BufferedReader(new StringReader(jsonString));
 
  65         //--- mock the getReader() call
 
  66         when(request.getReader()).thenReturn(br);   
 
  68         controller = new DecisionPolicyDictionaryController(commonClassDao);
 
  70         logger.info("setUp: exit");
 
  74         public void tearDown() throws Exception {
 
  78         public void testGetSettingsDictionaryByNameEntityData() {
 
  79                 logger.info("testGetSettingsDictionaryByNameEntityData: Entering");
 
  81                 MockHttpServletResponse response =  new MockHttpServletResponse();
 
  83                 controller.getSettingsDictionaryByNameEntityData(request, response);
 
  86                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
 
  87                         logger.info("response.getContentAsString(): " + response.getContentAsString());
 
  88                 } catch (UnsupportedEncodingException e) {
 
  89                         logger.error("Exception Occured"+e);
 
  90                         fail("Exception: " + e);
 
  93                 logger.info("testGetSettingsDictionaryByNameEntityData: exit"); 
 
  98         public void testGetSettingsDictionaryEntityData() {
 
  99                 logger.info("testGetSettingsDictionaryEntityData: Entering");
 
 101                 MockHttpServletResponse response =  new MockHttpServletResponse();
 
 103                 controller.getSettingsDictionaryEntityData(response);
 
 106                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
 
 107                         logger.info("response.getContentAsString(): " + response.getContentAsString());
 
 108                 } catch (UnsupportedEncodingException e) {
 
 109                         logger.error("Exception Occured"+e);
 
 110                         fail("Exception: " + e);
 
 113                 logger.info("testGetSettingsDictionaryEntityData: exit");
 
 118         public void testSaveSettingsDictionary() {
 
 119                 logger.info("testSaveSettingsDictionary: Entering");
 
 121                 MockHttpServletResponse response =  new MockHttpServletResponse();
 
 122             request = mock(HttpServletRequest.class);  
 
 125                     // mock the getReader() call
 
 126                         jsonString = "{\"settingsDictionaryData\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"},\"description\":\"test\",\"priority\":\"High\"}, \"userid\":\"test\"}";
 
 128                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
 
 129                         when(request.getReader()).thenReturn(br);                   
 
 130                         controller.saveSettingsDictionary(request, response);
 
 131                         logger.info("response.getContentAsString(): " + response.getContentAsString());
 
 132                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
 
 134                 } catch (Exception e) {
 
 135                         logger.error("Exception Occured"+e);
 
 136                         fail("Exception: " + e);
 
 139                 logger.info("testSaveSettingsDictionary: exit");        
 
 144         public void testRemoveSettingsDictionary() {
 
 145                 logger.info("testRemoveSettingsDictionary: Entering");
 
 147                 MockHttpServletResponse response =  new MockHttpServletResponse();
 
 148             request = mock(HttpServletRequest.class);   
 
 151                     // mock the getReader() call
 
 152                         jsonString = "{\"data\": {\"modelName\": \"test\",      \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
 
 153                                         + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
 
 154                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
 
 155                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
 
 156                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
 
 157                                         + "     \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
 
 158                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
 
 159                                         + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
 
 160                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
 
 161                         when(request.getReader()).thenReturn(br);                   
 
 162                         controller.removeSettingsDictionary(request, response);
 
 163                         logger.info("response.getContentAsString(): " + response.getContentAsString());
 
 164                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
 
 166                 } catch (Exception e) {
 
 167                         logger.error("Exception Occured"+e);
 
 168                         fail("Exception: " + e);
 
 171                 logger.info("testRemoveSettingsDictionary: exit");      
 
 176         public void testGetRainyDayDictionaryByNameEntityData() {
 
 177                 logger.info("testGetRainyDayDictionaryByNameEntityData: Entering");
 
 179                 MockHttpServletResponse response =  new MockHttpServletResponse();
 
 181                 controller.getRainyDayDictionaryByNameEntityData(request, response);
 
 184                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
 
 185                         logger.info("response.getContentAsString(): " + response.getContentAsString());
 
 186                 } catch (UnsupportedEncodingException e) {
 
 187                         logger.error("Exception Occured"+e);
 
 188                         fail("Exception: " + e);
 
 191                 logger.info("testGetRainyDayDictionaryByNameEntityData: exit");         
 
 196         public void testGetRainyDayDictionaryEntityData() {
 
 197                 logger.info("testGetRainyDayDictionaryEntityData: Entering");
 
 199                 MockHttpServletResponse response =  new MockHttpServletResponse();
 
 201                 controller.getRainyDayDictionaryEntityData(response);
 
 204                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
 
 205                         logger.info("response.getContentAsString(): " + response.getContentAsString());
 
 206                 } catch (UnsupportedEncodingException e) {
 
 207                         logger.error("Exception Occured"+e);
 
 208                         fail("Exception: " + e);
 
 211                 logger.info("testGetRainyDayDictionaryEntityData: exit");       
 
 216         public void testSaveRainyDayDictionary() {
 
 217                 logger.info("testSaveRainyDayDictionary: Entering");
 
 219                 MockHttpServletResponse response =  new MockHttpServletResponse();
 
 220             request = mock(HttpServletRequest.class);  
 
 223                     // mock the getReader() call
 
 224                         jsonString = "{\"rainyDayDictionaryData\":{\"bbid\":\"BB2\",\"workstep\":\"1\",\"userDataTypeValues\":[{\"$$hashKey\":\"object:233\",\"treatment\":\"test1\"},{\"$$hashKey\":\"object:239\",\"treatment\":\"test2\"}]},\"userid\":\"mm117s\"}";
 
 226                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
 
 227                         when(request.getReader()).thenReturn(br);                   
 
 228                         controller.saveRainyDayDictionary(request, response);
 
 229                         logger.info("response.getContentAsString(): " + response.getContentAsString());
 
 230                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
 
 232                 } catch (Exception e) {
 
 233                         logger.error("Exception Occured"+e);
 
 234                         fail("Exception: " + e);
 
 237                 logger.info("testSaveRainyDayDictionary: exit");                
 
 242         public void testRemoveRainyDayDictionary() {
 
 243                 logger.info("testRemoveRainyDayDictionary: Entering");
 
 245                 MockHttpServletResponse response =  new MockHttpServletResponse();
 
 246             request = mock(HttpServletRequest.class);   
 
 249                     // mock the getReader() call
 
 250                         jsonString = "{\"data\": {\"modelName\": \"test\",      \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
 
 251                                         + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
 
 252                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
 
 253                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
 
 254                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
 
 255                                         + "     \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
 
 256                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
 
 257                                         + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
 
 258                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
 
 259                         when(request.getReader()).thenReturn(br);                   
 
 260                         controller.removeRainyDayDictionary(request, response);
 
 261                         logger.info("response.getContentAsString(): " + response.getContentAsString());
 
 262                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
 
 264                 } catch (Exception e) {
 
 265                         logger.error("Exception Occured"+e);
 
 266                         fail("Exception: " + e);
 
 269                 logger.info("testRemoveRainyDayDictionary: exit");