@Test
public void testValidateRepositoryVersion() throws Exception {

  StackDAO stackDAO = injector.getInstance(StackDAO.class);
  StackEntity stackEntity = stackDAO.find("HDP", "1.1");
  Assert.assertNotNull(stackEntity);
  final RepositoryVersionEntity entity = new RepositoryVersionEntity();
  entity.setDisplayName("name");
  entity.setStack(stackEntity);
  entity.setVersion("1.1");
  entity.setOperatingSystems("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"http://example.com/repo1\",\"Repositories/unique\":\"true\"}]}]");
  final RepositoryVersionDAO repositoryVersionDAO = injector.getInstance(RepositoryVersionDAO.class);
  AmbariMetaInfo info = injector.getInstance(AmbariMetaInfo.class);
  // test valid usecases
  RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity);
  entity.setVersion("1.1-17");
  RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity);
  entity.setVersion("1.1.1.1");
  RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity);
  entity.setVersion("1.1.343432.2");
  RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity);
  entity.setVersion("1.1.343432.2-234234324");
  RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity);
  // test invalid usecases
  entity.setOperatingSystems(jsonStringRedhat7);
  try {
    RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity);
    Assert.fail("Should throw exception");
  } catch (Exception ex) {
  }
  entity.setOperatingSystems("");
  try {
    RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity);
    Assert.fail("Should throw exception");
  } catch (Exception ex) {
  }
  StackEntity bigtop = new StackEntity();
  bigtop.setStackName("BIGTOP");
  entity.setStack(bigtop);
  try {
    RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity);
    Assert.fail("Should throw exception");
  } catch (Exception ex) {
  }
  entity.setDisplayName("name");
  entity.setStack(stackEntity);
  entity.setVersion("1.1");
  entity.setOperatingSystems("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"http://example.com/repo1\",\"Repositories/unique\":\"true\"}]}]");
  repositoryVersionDAO.create(entity);
  final RepositoryVersionEntity entity2 = new RepositoryVersionEntity();
  entity2.setId(2l);
  entity2.setDisplayName("name2");
  entity2.setStack(stackEntity);
  entity2.setVersion("1.2");
  entity2.setOperatingSystems("[{\"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"http://example.com/repo1\",\"Repositories/unique\":\"true\"}]}]");
  try {
    RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity2);
    Assert.fail("Should throw exception: Base url http://example.com/repo1 is already defined for another repository version");
  } catch (Exception ex) {
  }
  final RepositoryVersionEntity entity3 = new RepositoryVersionEntity();
  entity3.setId(3l);
  entity3.setDisplayName("name2");
  entity3.setStack(stackEntity);
  entity3.setVersion("1.1");
  entity3.setOperatingSystems("[{\"OperatingSystems/ambari_managed_repositories\": true, \"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"http://example.com/repo1\",\"Repositories/unique\":\"true\"}]}]");
  try {
    RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity3);
    Assert.fail("Expected exception");
  } catch (AmbariException e) {
    // expected
  }
  entity3.setOperatingSystems("[{\"OperatingSystems/ambari_managed_repositories\": false, \"OperatingSystems/os_type\":\"redhat6\",\"repositories\":[{\"Repositories/repo_id\":\"1\",\"Repositories/repo_name\":\"1\",\"Repositories/base_url\":\"http://example.com/repo1\",\"Repositories/unique\":\"true\"}]}]");
  RepositoryVersionResourceProvider.validateRepositoryVersion(repositoryVersionDAO, info, entity3);
}