@Test
  public void shouldAuthorizeCreateNewParameterAndSaveEvent() {
    // given
    SimpleParameter parameter = new SimpleParameter().withName("test").withInputLevels(1);

    // when
    paramManager.createParameter(USER, REPOSITORY_NAME, parameter);

    // then
    assertThat(inMemoryEventLogRepository.findFirstEvent(Action.CREATE_PARAMETER)).isNotNull();
    assertThat(inMemoryParamRepository.inspector().hasParameter("test")).isTrue();
  }
  @Test
  public void shouldThrowExceptionWhenAuthorizationFails() {
    // given
    UserProfile user = new UserProfile("unauthorized", "ROOT");
    inMemoryParamRepository.addEntry(
        "sp.manager.authz.login", new SimpleParameterEntry("unauthorized", "*", "*", "false"));

    // when
    catchException(paramManager).deleteParameter(user, REPOSITORY_NAME, "some parameter");

    // then
    assertThat(caughtException()).isInstanceOf(AuthorizationFailedException.class);
  }
  @Test
  public void shouldAuthorizeEntryAdditionAndSaveEvent() {
    // given
    paramManager.createParameter(
        USER, REPOSITORY_NAME, new SimpleParameter().withName("test").withInputLevels(1));
    ParameterEntry entry = new SimpleParameterEntry("value");

    // when
    paramManager.addEntries(USER, REPOSITORY_NAME, "test", Arrays.asList(entry));

    // then
    assertThat(inMemoryEventLogRepository.findFirstEvent(Action.ADD_ENTRY)).isNotNull();
    assertThat(inMemoryParamRepository.load("test")).hasEntries(1);
  }
  @Test
  public void shouldAuthorizeLevelAdditionAndSaveEvent() {
    // given
    SimpleParameter parameter = new SimpleParameter().withName("test").withInputLevels(1);
    paramManager.createParameter(USER, REPOSITORY_NAME, parameter);

    SimpleLevel level = new SimpleLevel().withName("test-level");

    // when
    paramManager.addLevel(USER, REPOSITORY_NAME, "test", level);

    // then
    assertThat(inMemoryEventLogRepository.findFirstEvent(Action.ADD_LEVEL)).isNotNull();
    assertThat(inMemoryParamRepository.load("test")).firstLevel().hasName("test-level");
  }
  @Test
  public void shouldAuthorizeParameterUpdateAndSaveEvent() {
    // given
    SimpleParameter parameter = new SimpleParameter().withName("test").withInputLevels(1);
    paramManager.createParameter(USER, REPOSITORY_NAME, parameter);

    SimpleParameter parameterUpdate = new SimpleParameter().withInputLevels(2);

    // when
    paramManager.updateParameter(USER, REPOSITORY_NAME, "test", parameterUpdate);

    // then
    assertThat(inMemoryEventLogRepository.findFirstEvent(Action.UPDATE_PARAMETER)).isNotNull();
    assertThat(inMemoryParamRepository.load("test")).hasInputLevels(2);
  }
  @Test
  public void shouldAuthorizeLevelDeletionAndSaveEvent() {
    // given
    paramManager.createParameter(
        USER, REPOSITORY_NAME, new SimpleParameter().withName("test").withInputLevels(1));
    LevelKey levelKey =
        paramManager
            .addLevel(USER, REPOSITORY_NAME, "test", new SimpleLevel().withName("test-level1"))
            .levelKey();

    // when
    paramManager.deleteLevel(USER, REPOSITORY_NAME, "test", levelKey);

    // then
    assertThat(inMemoryEventLogRepository.findFirstEvent(Action.DELETE_LEVEL)).isNotNull();
    assertThat(inMemoryParamRepository.load("test")).hasNoLevels();
  }
  @Test
  public void shouldAuthorizeLevelUpdateAndSaveEvent() {
    // given
    paramManager.createParameter(
        USER, REPOSITORY_NAME, new SimpleParameter().withName("test").withInputLevels(1));
    LevelKey levelKey =
        paramManager
            .addLevel(USER, REPOSITORY_NAME, "test", new SimpleLevel().withName("test-level"))
            .levelKey();

    SimpleLevel levelUpdate = new SimpleLevel().withName("test-level").withMatcher("matcher");

    // when
    paramManager.updateLevel(USER, REPOSITORY_NAME, "test", levelKey, levelUpdate);

    // then
    assertThat(inMemoryEventLogRepository.findFirstEvent(Action.UPDATE_LEVEL)).isNotNull();
    assertThat(inMemoryParamRepository.load("test")).firstLevel().hasMatcher("matcher");
  }
  @Test
  public void shouldAuthorizeLevelOrderingAndSaveEvent() {
    // given
    paramManager.createParameter(
        USER, REPOSITORY_NAME, new SimpleParameter().withName("test").withInputLevels(1));
    LevelKey levelKey2 =
        paramManager
            .addLevel(USER, REPOSITORY_NAME, "test", new SimpleLevel().withName("test-level2"))
            .levelKey();
    LevelKey levelKey1 =
        paramManager
            .addLevel(USER, REPOSITORY_NAME, "test", new SimpleLevel().withName("test-level1"))
            .levelKey();

    List<LevelKey> newOrder = Arrays.asList(levelKey1, levelKey2);

    // when
    paramManager.reorderLevels(USER, REPOSITORY_NAME, "test", newOrder);

    // then
    assertThat(inMemoryEventLogRepository.findFirstEvent(Action.REOREDER_LEVELS)).isNotNull();
    assertThat(inMemoryParamRepository.load("test"))
        .namedLevelsInOrder("test-level1", "test-level2");
  }
 @BeforeMethod
 public void setUp() {
   inMemoryParamRepository.clearExcept("sp.manager.authz.login", "sp.manager.authz.role");
   inMemoryEventLogRepository.clear();
   paramEngine.runtimeConfiguration().getParamCache().invalidate();
 }