Package com.mockey.model

Examples of com.mockey.model.Scenario


    JSONObject jsonObject = new JSONObject();
    try {
      if ("clear_last_visit".equalsIgnoreCase(action)) {
        if (serviceId != null && scenarioId != null) {
          Service service = store.getServiceById(new Long(serviceId));
          Scenario scenario = service
              .getScenario(new Long(scenarioId));
          scenario.setLastVisit(null);
          service.saveOrUpdateScenario(scenario);
          store.saveOrUpdateService(service);

        } else if (serviceId != null) {
          Service service = store.getServiceById(new Long(serviceId));
View Full Code Here


    try {
      Long serviceId = new Long(req.getParameter("serviceId"));
      Long scenarioId = null;
      scenarioId = new Long(req.getParameter("scenarioId"));
      Service service = store.getServiceById(serviceId);
      Scenario scenario = service.getScenario(scenarioId);
      req.setAttribute("service", service);
      req.setAttribute("scenario", scenario);
    } catch (Exception e) {
      logger.debug("Unable to retrieve a Service of ID: "
          + req.getParameter("serviceId"));
View Full Code Here

      out.flush();
      out.close();
      return;
    }

    Scenario scenario = null;

    // ************************************************
    // HACK A: if renaming an existing Scenario Name, then
    // we need to update this Service's Scenario Name in a
    // Service Plan. NOTE:
    // * we can't ask the Service to update the Plan because it doesn't
    // know about it.
    // * we can't ask the Store to update the Plan because it doesn't
    // know about the Scenario's 'old' name, only this Servlet does!
    // Hence, we do it here via 'newName' and 'oldName'.
    // ************************************************
    String oldName = null;
    String newName = null;
    try {
      scenario = service.getScenario(new Long(req
          .getParameter("scenarioId")));
      // ***************** HACK A ****************
      oldName = scenario.getScenarioName();
      // *****************************************
    } catch (Exception e) {
      //
    }

    // CREATE OR UPDATE OF SCENARIO
    // If scenario is null, that means we're creating,
    // not updating
    if (scenario == null) {
      scenario = new Scenario();
    }

    String scenarioName = req.getParameter("scenarioName");
    if (scenarioName == null || scenarioName.trim().length() == 0) {
      // Let's be nice and make up a name.
      scenarioName = "Scenario for " + service.getServiceName()
          + "(name auto-generated)";
    }
    // ***************** HACK A ****************
    newName = scenarioName;
    // *****************************************

    scenario.setScenarioName(scenarioName);

    if (req.getParameter("tag") != null) {
      scenario.setTag(req.getParameter("tag"));
    }

    if (req.getParameter("httpResponseStatusCode") != null) {
      try {
        String v = req.getParameter("httpResponseStatusCode");
        int statusCodeVal = Integer.parseInt(v);
        scenario.setHttpResponseStatusCode(statusCodeVal);
      } catch (Exception e) {

      }
    }
   
    if (req.getParameter("httpMethodType") != null) {
      try {
        String v = req.getParameter("httpMethodType");
        scenario.setHttpMethodType(v);
      } catch (Exception e) {

      }
    }

    if (req.getParameter("responseHeader") != null) {
      String responseHeader = req.getParameter("responseHeader");
      if (responseHeader != null) {
        scenario.setResponseHeader(responseHeader);
      }

    }

    if (req.getParameter("responseMessage") != null) {
      scenario.setResponseMessage(req.getParameter("responseMessage"));
    }
    if (req.getParameter("matchStringArg") != null) {
      scenario.setMatchStringArg(req.getParameter("matchStringArg"));
    }

    String matchArgAsRegexBoolVal = req
        .getParameter("matchStringArgEvaluationRulesFlag");
    if (matchArgAsRegexBoolVal != null) {
      try {
        scenario.setMatchStringArgEvaluationRulesFlag(Boolean
            .parseBoolean(matchArgAsRegexBoolVal));
      } catch (Exception t) {
        logger.error(
            "Unable to parse the Scenario match-to-be-used-as-a-regex flag, which should be 'true' or 'false' but was  "
                + matchArgAsRegexBoolVal, t);
      }
    }

    // VALIDATION
    Map<String, String> errorMap = ScenarioValidator.validate(scenario);

    if ((errorMap != null) && (errorMap.size() == 0)) {

      // If creating a Scenario, then the returned scenario
      // will now have an id. If updating scenario, then
      // scenario ID remains the same.
      scenario = service.saveOrUpdateScenario(scenario);

      // Make this the default 'error response' scenario
      // for the service
      String v = req.getParameter("errorScenario");
      if (v != null && "true".equalsIgnoreCase(v.trim())) {
        service.setErrorScenarioId(scenario.getId());
      } else if (service.getErrorScenarioId() == scenario.getId()) {
        service.setErrorScenarioId(null);
      }

      // Make this the default universal 'error response',
      // for all services defined in Mockey.
      v = req.getParameter("universalErrorScenario");
      if (v != null && "true".equalsIgnoreCase(v.trim())) {
        ScenarioRef scenarioRef = new ScenarioRef(scenario.getId(),
            scenario.getServiceId());
        store.setUniversalErrorScenarioRef(scenarioRef);

      } else if (store.getUniversalErrorScenario() != null) {
        store.setUniversalErrorScenarioRef(null);
      }

      store.saveOrUpdateService(service);

      // ***************** HACK A ****************
      if (newName != null && oldName != null
          && !oldName.trim().equals(newName.trim())) {
        // OK, we had an existing Service Scenario with a name change.
        // Let's update the appropriate Service Plan.
        store.updateServicePlansWithNewScenarioName(serviceId, oldName, newName);
       
      }
      // *****************************************
      resp.setContentType("application/json");
      PrintWriter out = resp.getWriter();

      JSONObject object = new JSONObject();
      JSONObject resultObject = new JSONObject();
      try {

        object.put("success", "Scenario updated");
        object.put("scenarioId", scenario.getId().toString());
        object.put("serviceId", service.getId().toString());
        resultObject.put("result", object);
      } catch (JSONException e1) {
        e1.printStackTrace();
      }
View Full Code Here

        jsonObject.put("success", "Deleted tag from Service Plan.");

      } else if ("delete_tag_from_scenario".equals(action)) {
        Service service = store.getServiceById(new Long(serviceId));

        Scenario scenario = service.getScenario(new Long(scenarioId));
        scenario.removeTagFromList(tag);
        service.saveOrUpdateScenario(scenario);
        store.saveOrUpdateService(service);
        jsonObject.put("success", "Deleted tag from Scenario.");

      } else if ("update_service_tag".equals(action)) {
        Service service = store.getServiceById(new Long(serviceId));

        service.clearTagList();
        service.addTagToList(tag);
        store.saveOrUpdateService(service);
        jsonObject.put("success", "Updated tag(s) for this Service.");

      } else if ("update_scenario_tag".equals(action)) {
        Service service = store.getServiceById(new Long(serviceId));

        Scenario scenario = service.getScenario(new Long(scenarioId));
        scenario.clearTagList();
        scenario.addTagToList(tag);
        service.saveOrUpdateScenario(scenario);
        store.saveOrUpdateService(service);
        jsonObject.put("success", "Updated tag(s) for this Scenario.");

      }

      // PRESENT STATE
      //
      // OK, now that things are up to date (if any action occurred),
      // let's present the state in the JSON
      // Why get the Service again? Because, we could have removed/edited
      // the tag information from one of the steps above.
      if (serviceId != null) {
        Service service = store.getServiceById(new Long(serviceId));
        jsonObject.put("serviceId", "" + serviceId);

        if (scenarioId != null) {
          Scenario scenario = service
              .getScenario(new Long(scenarioId));
          jsonObject.put("scenarioId", "" + scenario.getId());
          jsonObject.put("tag", "" + scenario.getTag());
        } else {
          jsonObject.put("tag", "" + service.getTag());
        }
      }
View Full Code Here

  public Document getStoreAsDocument(IMockeyStorage store,
      boolean nonRefFullDefinition) {

    Document document = this.getDocument();
    Element rootElement = document.createElement("mockservice");
    Scenario mssb = store.getUniversalErrorScenario();
    this.setAttribute(rootElement, "xml:lang", "en-US");
    this.setAttribute(rootElement, "version", "1.0");
    // Universal Service settings
    if (mssb != null) {
      this.setAttribute(rootElement, "universal_error_service_id", ""
          + mssb.getServiceId());
      this.setAttribute(rootElement, "universal_error_scenario_id", ""
          + mssb.getId());
    }
    if (store.getUniversalTwistInfoId() != null) {
      this.setAttribute(rootElement, "universal_twist_info_id", ""
          + store.getUniversalTwistInfoId());
    }
View Full Code Here

   
    MockeyXmlFileManager.createInstance(TESTCONFIG_DIR);
    Service feelingService = new Service();
    feelingService.setServiceName("feeling");
    feelingService.setUrl("/feeling");
    Scenario happyScenario = new Scenario();
    happyScenario.setScenarioName("happy");
    happyScenario.setResponseMessage("HAPPY");
    happyScenario = feelingService.saveOrUpdateScenario(happyScenario);
    File scenarioFile = MockeyXmlFileManager.getInstance().getServiceScenarioFileAbsolutePath(feelingService, happyScenario);
    assert(scenarioFile!=null) : "Scenario is null. It should not be."
    String correctPath  = TESTCONFIG_DIR + File.separator + "mockey_def_depot" +File.separator +"feeling" + File.separator +"scenarios" + File.separator +"happy.xml";
    assert(scenarioFile.getAbsolutePath().equals(correctPath)) : "Invalid scenario file path.  \n    WAS:" + scenarioFile.getAbsolutePath() +"\nCORRECT:" + correctPath;
View Full Code Here

    // ***************************
    // New Store
    // ***************************
    Service service = new Service();
    service.setServiceName("Service 1");
    Scenario scenario = new Scenario();
    scenario.setScenarioName("ABC");
    scenario.setTag("abc");
    service.saveOrUpdateScenario(scenario);
    store.saveOrUpdateService(service);

    // ***************************
    // Get the store as XML
    // ***************************
    String storeAsXml = getStoreAsXml();

    // ***************************
    // Rebuild the store with:
    // - Same Service, same scenario
    // - Service scenario has different tag
    // ***************************
    store.deleteEverything();
    service = new Service();
    service.setServiceName("Service 1");
    scenario = new Scenario();
    scenario.setScenarioName("ABC");
    scenario.setTag("def");
    service.saveOrUpdateScenario(scenario);
    store.saveOrUpdateService(service);

    // ***************************
    // Upload/Merge the store again.
    // Result should be 1 Service in the store with 2 Real URLS (merged)
    // ***************************
    getMergeResults(storeAsXml);

    List<Service> storeServices = store.getServices();
    assert (storeServices.size() == 1) : "Number of Services in the Store should have been 1 but was "
        + storeServices.size();
    Service serviceToTest = storeServices.get(0);
    List<Scenario> scenarioList = serviceToTest.getScenarios();
    assert (scenarioList.size() == 1) : "Number of Service scenarios in the Store should have been 1 but was "
        + scenarioList.size()
        + " with value: \n"
        + getScenarioListAsString(scenarioList);

    Scenario scenarioTest = scenarioList.get(0);
    assert (scenarioTest.getTagList().size() == 2) : "Number of Tags in the Service Scenario should have been size 2, with value 'abc def' but was size "
        + scenarioTest.getTagList().size()
        + " with value '"
        + scenarioTest.getTag() + "'";

  }
View Full Code Here

  public void validateSearchResultByName() {

    IMockeyStorage store = new InMemoryMockeyStorage();
    Service a = new Service();
    a.setServiceName("Service A");
    Scenario aScenario = new Scenario();
    aScenario.setScenarioName("Service Scenario A");
    a.saveOrUpdateScenario(aScenario);
    store.saveOrUpdateService(a);

    String term = "scenario";
    SearchResultBuilder resultBuilder = new SearchResultBuilder();
View Full Code Here

  public void validateSearchResultScenarioContent() {

    IMockeyStorage store = new InMemoryMockeyStorage();
    Service a = new Service();
    a.setServiceName("Service A");
    Scenario aScenario = new Scenario();
    aScenario.setScenarioName("Scenario A");
    aScenario.setResponseMessage("lorem ipsum");
    a.saveOrUpdateScenario(aScenario);
    store.saveOrUpdateService(a);

    String term = "cats";
    SearchResultBuilder resultBuilder = new SearchResultBuilder();
    List<SearchResult> resultList = resultBuilder.buildSearchResults(term,
        store);

    assert (resultList.size() == 0) : "Length should be 0 but was "
        + resultList.size();

    term = "  lorem   ";
    resultBuilder = new SearchResultBuilder();
    resultList = resultBuilder.buildSearchResults(term, store);
    assert (resultList.size() == 1) : "Length should be: 1 " + " but was '"
        + resultList.size() + "'";
    assert (resultList.get(0).getType() == SearchResultType.SERVICE_SCENARIO) : "Search type result should be 'scenario' but was '"
        + resultList.get(0).getType().toString() + "'";

    aScenario = new Scenario();
    aScenario.setScenarioName("Scenario B");
    aScenario.setResponseMessage("lorem ipsula");
    a.saveOrUpdateScenario(aScenario);
    store.saveOrUpdateService(a);

    term = "  lorem   ";
    resultBuilder = new SearchResultBuilder();
View Full Code Here

  public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    Long serviceId = new Long(req.getParameter("serviceId"));
    String scenarioIdAsString = req.getParameter("scenarioId");
    Service service = store.getServiceById(serviceId);
    Scenario scenario = null;
    resp.setContentType("application/json");
    PrintWriter out = resp.getWriter();
    try {
      scenario = service.getScenario(new Long(scenarioIdAsString));
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("serviceId", "" + serviceId);
      jsonObject.put("serviceName", "" + service.getServiceName());
      jsonObject.put("scenarioId", "" + scenario.getId());
      jsonObject.put("tag", "" + scenario.getTag());
      jsonObject.put("httpResponseStatusCode", "" + scenario.getHttpResponseStatusCode());
      jsonObject.put("httpMethodType", "" + scenario.getHttpMethodType());
      jsonObject.put("name", scenario.getScenarioName());
      jsonObject.put("match", scenario.getMatchStringArg());
      jsonObject.put("matchRegexFlag", scenario.isMatchStringArgEvaluationRulesFlag());
      jsonObject.put("response", scenario.getResponseMessage());
      jsonObject.put("responseHeader", scenario.getResponseHeader());

      // Error handling flags
      String scenarioErrorId = (service.getErrorScenario() != null) ? "" + service.getErrorScenario().getId()
          : "-1";
      if (scenarioErrorId.equals(scenarioIdAsString)) {
        jsonObject.put("scenarioErrorFlag", true);
      } else {
        jsonObject.put("scenarioErrorFlag", false);
      }

      // For universal, both SERVICE ID and SCENARIO ID have to match.
      Scenario universalError = store.getUniversalErrorScenario();
      boolean universalErrorFlag = false;
      if (universalError != null) {
        try {
          if (serviceId.equals(universalError.getServiceId())
              && universalError.getId().equals(new Long(scenarioIdAsString))) {
            universalErrorFlag = true;
          }
        } catch (Exception ae) {
          // Ignore
          logger.debug("Unable to set universal error.", ae);
View Full Code Here

TOP

Related Classes of com.mockey.model.Scenario

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.