@Test
 public void shouldNotCreatePackageRepositoryIfTheSpecifiedPluginTypeIsInvalid() throws Exception {
   when(packageRepositoryService.validatePluginId(packageRepository)).thenReturn(false);
   when(packageRepositoryService.validateRepositoryConfiguration(packageRepository))
       .thenReturn(true);
   CreatePackageRepositoryCommand command =
       new CreatePackageRepositoryCommand(
           goConfigService, packageRepositoryService, packageRepository, currentUser, result);
   assertFalse(command.isValid(cruiseConfig));
 }
  @Test
  public void shouldContinueWithConfigSaveIfUserIsGroupAdmin() {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(false);
    when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(true);

    CreatePackageRepositoryCommand command =
        new CreatePackageRepositoryCommand(
            goConfigService, packageRepositoryService, packageRepository, currentUser, result);

    assertThat(command.canContinue(cruiseConfig), is(true));
  }
 @Test
 public void shouldNotCreatePackageRepositoryWhenRepositoryWithSpecifiedNameAlreadyExists()
     throws Exception {
   CreatePackageRepositoryCommand command =
       new CreatePackageRepositoryCommand(
           goConfigService, packageRepositoryService, packageRepository, currentUser, result);
   command.update(cruiseConfig);
   assertFalse(command.isValid(cruiseConfig));
   assertThat(
       packageRepository.errors().firstError(),
       is(
           "You have defined multiple repositories called 'npmOrg'. Repository names are case-insensitive and must be unique."));
 }
  @Test
  public void shouldNotCreatePackageRepositoryWhenRepositoryHasInvalidName() throws Exception {
    packageRepository.setName("~!@#$%^&*(");
    CreatePackageRepositoryCommand command =
        new CreatePackageRepositoryCommand(
            goConfigService, packageRepositoryService, packageRepository, currentUser, result);

    assertFalse(command.isValid(cruiseConfig));
    assertThat(
        packageRepository.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 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));
  }
 @Test
 public void shouldNotCreatePackageRepositoryWhenRepositoryHasDuplicateConfigurationProperties()
     throws Exception {
   ConfigurationProperty property =
       new ConfigurationProperty(new ConfigurationKey("foo"), new ConfigurationValue("bar"));
   Configuration configuration = new Configuration(property, property);
   packageRepository.setConfiguration(configuration);
   CreatePackageRepositoryCommand command =
       new CreatePackageRepositoryCommand(
           goConfigService, packageRepositoryService, packageRepository, currentUser, result);
   assertFalse(command.isValid(cruiseConfig));
   assertThat(
       property.errors().firstError(), is("Duplicate key 'foo' found for Repository 'npmOrg'"));
 }
  @Test
  public void shouldNotContinueIfTheUserDontHavePermissionsToOperateOnPackageRepositories()
      throws Exception {
    when(goConfigService.isUserAdmin(currentUser)).thenReturn(false);
    CreatePackageRepositoryCommand command =
        new CreatePackageRepositoryCommand(
            goConfigService, packageRepositoryService, packageRepository, currentUser, result);

    HttpLocalizedOperationResult expectedResult = new HttpLocalizedOperationResult();
    expectedResult.unauthorized(
        LocalizedMessage.string("UNAUTHORIZED_TO_EDIT"), HealthStateType.unauthorised());

    assertThat(command.canContinue(cruiseConfig), is(false));
    assertThat(result, is(expectedResult));
  }