@Test(timeout = 90000)
public void testResourceValidation() throws Exception {

  assertEquals(RegistryConstants.MAX_FQDN_LABEL_LENGTH + 1, LEN_64_STR
      .length());
  SliderFileSystem sfs = ServiceTestUtils.initMockFs();
  Service app = new Service();
  // no name
  try {
    ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DNS_ENABLED);
    Assert.fail(EXCEPTION_PREFIX + "service with no name");
  } catch (IllegalArgumentException e) {
    assertEquals(ERROR_APPLICATION_NAME_INVALID, e.getMessage());
  }
  app.setName("test");
  // no version
  try {
    ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DNS_ENABLED);
    Assert.fail(EXCEPTION_PREFIX + " service with no version");
  } catch (IllegalArgumentException e) {
    assertEquals(String.format(ERROR_APPLICATION_VERSION_INVALID,
        app.getName()), e.getMessage());
  }
  app.setVersion("v1");
  // bad format name
  String[] badNames = {"4finance", "Finance", "finance@home", LEN_64_STR};
  for (String badName : badNames) {
    app.setName(badName);
    try {
      ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DNS_ENABLED);
      Assert.fail(EXCEPTION_PREFIX + "service with bad name " + badName);
    } catch (IllegalArgumentException e) {
    }
  }
  // launch command not specified
  app.setName(LEN_64_STR);
  Component comp = new Component().name("comp1");
  app.addComponent(comp);
  try {
    ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DEFAULT_DNS);
    Assert.fail(EXCEPTION_PREFIX + "service with no launch command");
  } catch (IllegalArgumentException e) {
    assertEquals(RestApiErrorMessages.ERROR_ABSENT_LAUNCH_COMMAND,
        e.getMessage());
  }
  // launch command not specified
  app.setName(LEN_64_STR.substring(0, RegistryConstants
      .MAX_FQDN_LABEL_LENGTH));
  try {
    ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DNS_ENABLED);
    Assert.fail(EXCEPTION_PREFIX + "service with no launch command");
  } catch (IllegalArgumentException e) {
    assertEquals(RestApiErrorMessages.ERROR_ABSENT_LAUNCH_COMMAND,
        e.getMessage());
  }
  // memory not specified
  comp.setLaunchCommand("sleep 1");
  Resource res = new Resource();
  app.setResource(res);
  try {
    ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DNS_ENABLED);
    Assert.fail(EXCEPTION_PREFIX + "service with no memory");
  } catch (IllegalArgumentException e) {
    assertEquals(String.format(
        RestApiErrorMessages.ERROR_RESOURCE_MEMORY_FOR_COMP_INVALID,
        comp.getName()), e.getMessage());
  }
  // invalid no of cpus
  res.setMemory("100mb");
  res.setCpus(-2);
  try {
    ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DNS_ENABLED);
    Assert.fail(
        EXCEPTION_PREFIX + "service with invalid no of cpus");
  } catch (IllegalArgumentException e) {
    assertEquals(String.format(
        RestApiErrorMessages.ERROR_RESOURCE_CPUS_FOR_COMP_INVALID_RANGE,
        comp.getName()), e.getMessage());
  }
  // number of containers not specified
  res.setCpus(2);
  try {
    ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DNS_ENABLED);
    Assert.fail(EXCEPTION_PREFIX + "service with no container count");
  } catch (IllegalArgumentException e) {
    Assert.assertTrue(e.getMessage()
        .contains(ERROR_CONTAINERS_COUNT_INVALID));
  }
  // specifying profile along with cpus/memory raises exception
  res.setProfile("hbase_finance_large");
  try {
    ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DNS_ENABLED);
    Assert.fail(EXCEPTION_PREFIX
        + "service with resource profile along with cpus/memory");
  } catch (IllegalArgumentException e) {
    assertEquals(String.format(RestApiErrorMessages
            .ERROR_RESOURCE_PROFILE_MULTIPLE_VALUES_FOR_COMP_NOT_SUPPORTED,
        comp.getName()),
        e.getMessage());
  }
  // currently resource profile alone is not supported.
  // TODO: remove the next test once resource profile alone is supported.
  res.setCpus(null);
  res.setMemory(null);
  try {
    ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DNS_ENABLED);
    Assert.fail(EXCEPTION_PREFIX + "service with resource profile only");
  } catch (IllegalArgumentException e) {
    assertEquals(ERROR_RESOURCE_PROFILE_NOT_SUPPORTED_YET,
        e.getMessage());
  }
  // unset profile here and add cpus/memory back
  res.setProfile(null);
  res.setCpus(2);
  res.setMemory("2gb");
  // null number of containers
  try {
    ServiceApiUtil.validateAndResolveService(app, sfs, CONF_DNS_ENABLED);
    Assert.fail(EXCEPTION_PREFIX + "null number of containers");
  } catch (IllegalArgumentException e) {
    Assert.assertTrue(e.getMessage()
        .startsWith(ERROR_CONTAINERS_COUNT_INVALID));
  }
}