FactoryBaseValidator { protected void validateProjectActions(FactoryDto factory) throws BadRequestException { final IdeDto ide = factory.getIde(); if (ide == null) { return; } final List<IdeActionDto> applicationActions = new ArrayList<>(); if (ide.getOnAppClosed() != null) { applicationActions.addAll(ide.getOnAppClosed().getActions()); } if (ide.getOnAppLoaded() != null) { applicationActions.addAll(ide.getOnAppLoaded().getActions()); } for (IdeActionDto applicationAction : applicationActions) { String id = applicationAction.getId(); if ("openFile".equals(id) || "findReplace".equals(id) || "runCommand".equals(id) || "newTerminal".equals(id)) { throw new BadRequestException(format(FactoryConstants.INVALID_ACTION_SECTION, id)); } } final OnAppLoadedDto onAppLoaded = ide.getOnAppLoaded(); if (onAppLoaded != null) { for (IdeActionDto action : onAppLoaded.getActions()) { final Map<String, String> properties = action.getProperties(); if ("openWelcomePage".equals(action.getId()) && isNullOrEmpty(properties.get("greetingContentUrl"))) { throw new BadRequestException(FactoryConstants.INVALID_WELCOME_PAGE_ACTION); } } } final OnProjectsLoadedDto onLoaded = ide.getOnProjectsLoaded(); if (onLoaded != null) { final List<IdeActionDto> onProjectOpenedActions = onLoaded.getActions(); for (IdeActionDto applicationAction : onProjectOpenedActions) { final String id = applicationAction.getId(); final Map<String, String> properties = applicationAction.getProperties(); switch (id) { case "openFile": if (isNullOrEmpty(properties.get("file"))) { throw new BadRequestException(FactoryConstants.INVALID_OPENFILE_ACTION); } break; case "runCommand": if (isNullOrEmpty(properties.get("name"))) { throw new BadRequestException(FactoryConstants.INVALID_RUNCOMMAND_ACTION); } break; case "findReplace": if (isNullOrEmpty(properties.get("in")) || isNullOrEmpty(properties.get("find")) || isNullOrEmpty(properties.get("replace"))) { throw new BadRequestException(FactoryConstants.INVALID_FIND_REPLACE_ACTION); } break; } } } }    }
@Test(expectedExceptions = BadRequestException.class) public void shouldNotValidateOpenfileActionIfInWrongSectionOnAppClosed() throws Exception { validator = new TesterFactoryBaseValidator(); List<IdeActionDto> actions = singletonList(newDto(IdeActionDto.class).withId("openFile")); IdeDto ide = newDto(IdeDto.class).withOnAppClosed(newDto(OnAppClosedDto.class).withActions(actions)); FactoryDto factoryWithAccountId = requireNonNull(getInstance().clone(factory)).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test(expectedExceptions = BadRequestException.class) public void shouldNotValidateFindReplaceActionIfInWrongSectionOnAppLoaded() throws Exception { validator = new TesterFactoryBaseValidator(); List<IdeActionDto> actions = singletonList(newDto(IdeActionDto.class).withId("findReplace")); IdeDto ide = newDto(IdeDto.class).withOnAppLoaded(newDto(OnAppLoadedDto.class).withActions(actions)); FactoryDto factoryWithAccountId = requireNonNull(getInstance().clone(factory)).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test(expectedExceptions = BadRequestException.class) public void shouldNotValidateIfOpenfileActionInsufficientParams() throws Exception { validator = new TesterFactoryBaseValidator(); List<IdeActionDto> actions = singletonList(newDto(IdeActionDto.class).withId("openFile")); IdeDto ide = newDto(IdeDto.class) .withOnProjectsLoaded(newDto(OnProjectsLoadedDto.class).withActions(actions)); FactoryDto factoryWithAccountId = requireNonNull(getInstance().clone(factory)).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test(expectedExceptions = BadRequestException.class) public void shouldNotValidateIfrunCommandActionInsufficientParams() throws Exception { validator = new TesterFactoryBaseValidator(); List<IdeActionDto> actions = singletonList(newDto(IdeActionDto.class).withId("openFile")); IdeDto ide = newDto(IdeDto.class) .withOnProjectsLoaded(newDto(OnProjectsLoadedDto.class).withActions(actions)); FactoryDto factoryWithAccountId = requireNonNull(getInstance().clone(factory)).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test(expectedExceptions = BadRequestException.class) public void shouldNotValidateIfOpenWelcomePageActionInsufficientParams() throws Exception { validator = new TesterFactoryBaseValidator(); List<IdeActionDto> actions = singletonList(newDto(IdeActionDto.class).withId("openWelcomePage")); IdeDto ide = newDto(IdeDto.class).withOnAppLoaded((newDto(OnAppLoadedDto.class).withActions(actions))); FactoryDto factoryWithAccountId = requireNonNull(getInstance().clone(factory)).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test(expectedExceptions = BadRequestException.class) public void shouldNotValidateIfFindReplaceActionInsufficientParams() throws Exception { validator = new TesterFactoryBaseValidator(); Map<String, String> params = new HashMap<>(); params.put("in", "pom.xml"); params.put("replace", "123"); List<IdeActionDto> actions = singletonList(newDto(IdeActionDto.class).withId("findReplace").withProperties(params)); IdeDto ide = newDto(IdeDto.class) .withOnProjectsLoaded(newDto(OnProjectsLoadedDto.class).withActions(actions)); FactoryDto factoryWithAccountId = requireNonNull(getInstance().clone(factory)).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test public void shouldValidateFindReplaceAction() throws Exception { validator = new TesterFactoryBaseValidator(); Map<String, String> params = new HashMap<>(); params.put("in", "pom.xml"); params.put("find", "123"); params.put("replace", "456"); List<IdeActionDto> actions = singletonList(newDto(IdeActionDto.class).withId("findReplace").withProperties(params)); IdeDto ide = newDto(IdeDto.class) .withOnProjectsLoaded(newDto(OnProjectsLoadedDto.class).withActions(actions)); FactoryDto factoryWithAccountId = requireNonNull(getInstance().clone(factory)).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test public void shouldValidateOpenfileAction() throws Exception { validator = new TesterFactoryBaseValidator(); Map<String, String> params = new HashMap<>(); params.put("file", "pom.xml"); List<IdeActionDto> actions = singletonList(newDto(IdeActionDto.class).withId("openFile").withProperties(params)); IdeDto ide = newDto(IdeDto.class) .withOnProjectsLoaded(newDto(OnProjectsLoadedDto.class).withActions(actions)); FactoryDto factoryWithAccountId = requireNonNull(getInstance().clone(factory)).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }