@Test
  public void shouldCopyPackagesFromOldRepositoryToTheUpdatedRepository() throws Exception {
    PackageDefinition nodePackage = new PackageDefinition("foo", "bar", new Configuration());
    oldPackageRepo.setPackages(new Packages(nodePackage));
    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            repoId);

    assertThat(cruiseConfig.getPackageRepositories().find(repoId), is(oldPackageRepo));
    assertThat(cruiseConfig.getPackageRepositories().find(repoId).getPackages().size(), is(1));
    assertThat(newPackageRepo.getPackages().size(), is(0));

    command.update(cruiseConfig);
    HttpLocalizedOperationResult expectedResult = new HttpLocalizedOperationResult();
    assertThat(result, is(expectedResult));
    assertThat(cruiseConfig.getPackageRepositories().find(repoId), is(newPackageRepo));
    assertThat(cruiseConfig.getPackageRepositories().find(repoId).getPackages().size(), is(1));
    assertThat(
        cruiseConfig.getPackageRepositories().find(repoId).getPackages().first(), is(nodePackage));
  }
  @Test
  public void shouldCreatePackageRepository() throws Exception {
    PackageRepository repository =
        new PackageRepository("id", "name", new PluginConfiguration(), new Configuration());
    CreatePackageRepositoryCommand command =
        new CreatePackageRepositoryCommand(
            goConfigService, packageRepositoryService, repository, currentUser, result);

    assertNull(cruiseConfig.getPackageRepositories().find("id"));
    command.update(cruiseConfig);
    HttpLocalizedOperationResult expectedResult = new HttpLocalizedOperationResult();
    assertThat(result, is(expectedResult));
    assertThat(cruiseConfig.getPackageRepositories().find("id"), is(repository));
  }
 @Before
 public void setup() throws Exception {
   initMocks(this);
   currentUser = new Username(new CaseInsensitiveString("user"));
   cruiseConfig = GoConfigMother.defaultCruiseConfig();
   repoId = "npmOrg";
   packageRepository =
       new PackageRepository(
           repoId, repoId, new PluginConfiguration("npm", "1"), new Configuration());
   result = new HttpLocalizedOperationResult();
   cruiseConfig.getPackageRepositories().add(packageRepository);
 }
  @Test
  public void shouldUpdatePackageRepository() throws Exception {
    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            repoId);

    assertThat(cruiseConfig.getPackageRepositories().size(), is(1));
    assertThat(cruiseConfig.getPackageRepositories().find(repoId), is(oldPackageRepo));

    command.update(cruiseConfig);
    HttpLocalizedOperationResult expectedResult = new HttpLocalizedOperationResult();
    assertThat(result, is(expectedResult));
    assertThat(cruiseConfig.getPackageRepositories().size(), is(1));
    assertThat(cruiseConfig.getPackageRepositories().find(repoId), is(newPackageRepo));
  }
  @Test
  public void shouldNotUpdatePackageRepositoryWhenRepositoryWithSpecifiedNameAlreadyExists()
      throws Exception {
    cruiseConfig.getPackageRepositories().add(newPackageRepo);
    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            repoId);

    assertFalse(command.isValid(cruiseConfig));
    assertThat(
        newPackageRepo.errors().firstError(),
        is(
            "You have defined multiple repositories called 'npmOrg'. Repository names are case-insensitive and must be unique."));
  }