2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018 Samsung Electronics. All rights reserved.
 
   6  * ================================================================================
 
   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.
 
  22  * ============LICENSE_END=========================================================
 
  28  * Class to emulate responses from the Saltstack Server that is compliant with the APP-C Saltstack Server
 
  29  * Interface. Used for jUnit tests to verify code is working. In tests it can be used
 
  30  * as a replacement for methods from ConnectionBuilder class
 
  33 package org.onap.ccsdk.sli.adaptors.saltstack.model;
 
  35 import com.att.eelf.configuration.EELFLogger;
 
  36 import com.att.eelf.configuration.EELFManager;
 
  37 import org.apache.commons.lang.StringUtils;
 
  38 import org.json.JSONObject;
 
  41 import java.util.regex.Matcher;
 
  42 import java.util.regex.Pattern;
 
  44 public class SaltstackServerEmulator {
 
  46     private static final String SALTSTATE_FILE_NAME = "fileName";
 
  47     private final EELFLogger logger = EELFManager.getInstance().getLogger(SaltstackServerEmulator.class);
 
  50      * Method that emulates the response from an Saltstack Server
 
  51      * when presented with a request to execute a saltState
 
  52      * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful)
 
  53      * payload is json string as would be sent back by Saltstack Server
 
  55     public SaltstackResult mockReqExec(Map<String, String> params) {
 
  56         SaltstackResult result = new SaltstackResult();
 
  59             if (params.get("Test") == "fail") {
 
  60                 result = rejectRequest(result, "Mocked: Fail");
 
  62                 String fileName = params.get(SALTSTATE_FILE_NAME);
 
  64                     result = acceptRequest(result, "");
 
  66                     result = acceptRequest(result, fileName);
 
  68         } catch (Exception e) {
 
  69             logger.error("JSONException caught", e);
 
  70             rejectRequest(result, e.getMessage());
 
  75     private SaltstackResult rejectRequest(SaltstackResult result, String Message) {
 
  76         result.setStatusCode(SaltstackResultCodes.REJECTED.getValue());
 
  77         result.setStatusMessage("Rejected");
 
  81     private SaltstackResult acceptRequest(SaltstackResult result, String fileName) {
 
  82         result.setStatusCode(SaltstackResultCodes.SUCCESS.getValue());
 
  83         result.setStatusMessage("Success");
 
  84         result.setOutputFileName(fileName);