UpgradeCatalog221 extends AbstractUpgradeCatalog { protected String addCheckCommandTimeoutParam(String source) { JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject(); JsonArray parametersJson = sourceJson.getAsJsonArray("parameters"); boolean parameterExists = parametersJson != null && !parametersJson.isJsonNull(); if (parameterExists) { Iterator<JsonElement> jsonElementIterator = parametersJson.iterator(); while(jsonElementIterator.hasNext()) { JsonElement element = jsonElementIterator.next(); JsonElement name = element.getAsJsonObject().get("name"); if (name != null && !name.isJsonNull() && name.getAsString().equals("check.command.timeout")) { return sourceJson.toString(); } } } JsonObject checkCommandTimeoutParamJson = new JsonObject(); checkCommandTimeoutParamJson.add("name", new JsonPrimitive("check.command.timeout")); checkCommandTimeoutParamJson.add("display_name", new JsonPrimitive("Check command timeout")); checkCommandTimeoutParamJson.add("value", new JsonPrimitive(60.0)); checkCommandTimeoutParamJson.add("type", new JsonPrimitive("NUMERIC")); checkCommandTimeoutParamJson.add("description", new JsonPrimitive("The maximum time before check command will be killed by timeout")); checkCommandTimeoutParamJson.add("units", new JsonPrimitive("seconds")); if (!parameterExists) { parametersJson = new JsonArray(); parametersJson.add(checkCommandTimeoutParamJson); sourceJson.add("parameters", parametersJson); } else { parametersJson.add(checkCommandTimeoutParamJson); sourceJson.remove("parameters"); sourceJson.add("parameters", parametersJson); } return sourceJson.toString(); } @Inject  UpgradeCatalog221(Injector injector); @Override String getTargetVersion(); @Override String getSourceVersion();  }
@Test public void test_AddCheckCommandTimeoutParam_ParamsNotAvailable() { UpgradeCatalog221 upgradeCatalog221 = new UpgradeCatalog221(injector); String inputSource = "{ \"path\" : \"test_path\", \"type\" : \"SCRIPT\"}"; String expectedSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"name\":\"check.command.timeout\",\"display_name\":\"Check command timeout\",\"value\":60.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before check command will be killed by timeout\",\"units\":\"seconds\"}]}"; String result = upgradeCatalog221.addCheckCommandTimeoutParam(inputSource); Assert.assertEquals(result, expectedSource); }
@Test public void test_AddCheckCommandTimeoutParam_ParamsAvailable() { UpgradeCatalog221 upgradeCatalog221 = new UpgradeCatalog221(injector); String inputSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"name\":\"test\",\"display_name\":\"Test\",\"value\":10.0,\"type\":\"test\",\"description\":\"test\",\"units\":\"test\"}]}"; String expectedSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"name\":\"test\",\"display_name\":\"Test\",\"value\":10.0,\"type\":\"test\",\"description\":\"test\",\"units\":\"test\"},{\"name\":\"check.command.timeout\",\"display_name\":\"Check command timeout\",\"value\":60.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before check command will be killed by timeout\",\"units\":\"seconds\"}]}"; String result = upgradeCatalog221.addCheckCommandTimeoutParam(inputSource); Assert.assertEquals(result, expectedSource); }
@Test public void test_AddCheckCommandTimeoutParam_NeededParamAlreadyAdded() { UpgradeCatalog221 upgradeCatalog221 = new UpgradeCatalog221(injector); String inputSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"display_name\":\"Test\",\"value\":10.0,\"type\":\"test\",\"description\":\"test\",\"units\":\"test\"},{\"name\":\"check.command.timeout\",\"display_name\":\"Check command timeout\",\"value\":60.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before check command will be killed by timeout\",\"units\":\"seconds\"}]}"; String expectedSource = "{\"path\":\"test_path\",\"type\":\"SCRIPT\",\"parameters\":[{\"display_name\":\"Test\",\"value\":10.0,\"type\":\"test\",\"description\":\"test\",\"units\":\"test\"},{\"name\":\"check.command.timeout\",\"display_name\":\"Check command timeout\",\"value\":60.0,\"type\":\"NUMERIC\",\"description\":\"The maximum time before check command will be killed by timeout\",\"units\":\"seconds\"}]}"; String result = upgradeCatalog221.addCheckCommandTimeoutParam(inputSource); Assert.assertEquals(result, expectedSource); }