@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 shouldNotUpdatePackageRepositoryIfTheSpecifiedPluginTypeIsInvalid() throws Exception {
   when(packageRepositoryService.validatePluginId(newPackageRepo)).thenReturn(false);
   when(packageRepositoryService.validateRepositoryConfiguration(newPackageRepo)).thenReturn(true);
   UpdatePackageRepositoryCommand command =
       new UpdatePackageRepositoryCommand(
           goConfigService,
           packageRepositoryService,
           newPackageRepo,
           currentUser,
           "md5",
           entityHashingService,
           result,
           repoId);
   command.update(cruiseConfig);
   assertFalse(command.isValid(cruiseConfig));
 }
  @Test
  public void shouldNotUpdatePackageRepositoryWhenRepositoryHasInvalidName() throws Exception {
    newPackageRepo.setName("~!@#$%^&*(");
    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            repoId);
    command.update(cruiseConfig);

    assertFalse(command.isValid(cruiseConfig));
    assertThat(
        newPackageRepo.errors().firstError(),
        is(
            "Invalid PackageRepository name '~!@#$%^&*('. This must be alphanumeric and can contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters."));
  }
  @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 shouldNotUpdatePackageRepositoryWhenRepositoryHasDuplicateConfigurationProperties()
      throws Exception {
    ConfigurationProperty property =
        new ConfigurationProperty(new ConfigurationKey("foo"), new ConfigurationValue("bar"));
    Configuration configuration = new Configuration(property, property);
    newPackageRepo.setConfiguration(configuration);

    UpdatePackageRepositoryCommand command =
        new UpdatePackageRepositoryCommand(
            goConfigService,
            packageRepositoryService,
            newPackageRepo,
            currentUser,
            "md5",
            entityHashingService,
            result,
            repoId);
    command.update(cruiseConfig);

    assertFalse(command.isValid(cruiseConfig));
    assertThat(
        property.errors().firstError(), is("Duplicate key 'foo' found for Repository 'npmOrg'"));
  }