FactoryBuilder { public void checkValid(Factory factory) throws ApiException { if (null == factory) { throw new ConflictException(FactoryConstants.UNPARSABLE_FACTORY_MESSAGE); } if (factory.getV() == null) { throw new ConflictException(FactoryConstants.INVALID_VERSION_MESSAGE); } Version v; try { v = Version.fromString(factory.getV()); } catch (IllegalArgumentException e) { throw new ConflictException(FactoryConstants.INVALID_VERSION_MESSAGE); } Class usedFactoryVersionMethodProvider; switch (v) { case V2_0: usedFactoryVersionMethodProvider = FactoryV2_0.class; break; case V2_1: usedFactoryVersionMethodProvider = FactoryV2_1.class; break; default: throw new ConflictException(FactoryConstants.INVALID_VERSION_MESSAGE); } validateCompatibility(factory, Factory.class, usedFactoryVersionMethodProvider, v, ""); } @Inject  FactoryBuilder(SourceProjectParametersValidator sourceProjectParametersValidator); Factory buildEncoded(Reader json); Factory buildEncoded(String json); Factory buildEncoded(InputStream json); void checkValid(Factory factory); Factory convertToLatest(Factory factory);  }
@Test public void shouldBeAbleToValidateEncodedV2_0() throws Exception { actual.withV("2.0") .withSource(dto.createDto(Source.class) .withProject(dto.createDto(ImportSourceDescriptor.class) .withType("git") .withLocation("location") .withParameters(singletonMap("key", "value"))) .withRunners(singletonMap("runEnv", dto.createDto(RunnerSource.class) .withLocation("location") .withParameters(singletonMap("key", "value"))))) .withProject(dto.createDto(NewProject.class) .withType("type") .withAttributes(singletonMap("key", singletonList("value"))) .withBuilders(dto.createDto(BuildersDescriptor.class).withDefault("default")) .withDescription("description") .withName("name") .withRunners(dto.createDto(RunnersDescriptor.class) .withDefault("default") .withConfigs(singletonMap("key", dto.createDto(RunnerConfiguration.class) .withRam(768) .withOptions(singletonMap("key", "value")) .withVariables(singletonMap("key", "value"))))) .withVisibility("private")) .withCreator(dto.createDto(Author.class) .withAccountId("accountId") .withEmail("email") .withName("name")) .withPolicies(dto.createDto(Policies.class) .withRefererHostname("referrer") .withValidSince(123L) .withValidUntil(123L)) .withActions(dto.createDto(Actions.class) .withFindReplace(singletonList(dto.createDto(ReplacementSet.class) .withFiles(singletonList("file")) .withEntries(singletonList(dto.createDto(Variable.class) .withFind("find") .withReplace("replace") .withReplacemode("mode"))))) .withOpenFile("openFile") .withWarnOnClose(true) .withWelcome(dto.createDto(WelcomePage.class) .withAuthenticated(dto.createDto(WelcomeConfiguration.class) .withContenturl("url") .withNotification("notification") .withTitle("title")) .withNonauthenticated(dto.createDto(WelcomeConfiguration.class) .withContenturl("url") .withNotification("notification") .withTitle("title")))) .withButton(dto.createDto(Button.class) .withType(Button.ButtonType.logo) .withAttributes(dto.createDto(ButtonAttributes.class) .withColor("color") .withCounter(true) .withLogo("logo") .withStyle("style"))) .withWorkspace(dto.createDto(Workspace.class) .withType("named")); factoryBuilder.checkValid(actual); verify(sourceProjectParametersValidator).validate(any(ImportSourceDescriptor.class), eq(FactoryParameter.Version.V2_0)); }
@Test public void shouldBeAbleToValidateEncodedV2_1() throws Exception { actual.withV("2.1") .withSource(dto.createDto(Source.class) .withProject(dto.createDto(ImportSourceDescriptor.class) .withType("git") .withLocation("location") .withParameters(singletonMap("key", "value"))) .withRunners(singletonMap("runEnv", dto.createDto(RunnerSource.class) .withLocation("location") .withParameters(singletonMap("key", "value"))))) .withProject(dto.createDto(NewProject.class) .withType("type") .withAttributes(singletonMap("key", singletonList("value"))) .withBuilders(dto.createDto(BuildersDescriptor.class).withDefault("default")) .withDescription("description") .withName("name") .withRunners(dto.createDto(RunnersDescriptor.class) .withDefault("default") .withConfigs(singletonMap("key", dto.createDto(RunnerConfiguration.class) .withRam(768) .withOptions(singletonMap("key", "value")) .withVariables(singletonMap("key", "value"))))) .withVisibility("private")) .withCreator(dto.createDto(Author.class) .withAccountId("accountId") .withEmail("email") .withName("name")) .withPolicies(dto.createDto(Policies.class) .withRefererHostname("referrer") .withValidSince(123L) .withValidUntil(123L)) .withButton(dto.createDto(Button.class) .withType(Button.ButtonType.logo) .withAttributes(dto.createDto(ButtonAttributes.class) .withColor("color") .withCounter(true) .withLogo("logo") .withStyle("style"))) .withWorkspace(dto.createDto(Workspace.class) .withType("named")) .withIde(dto.createDto(Ide.class) .withOnAppClosed( dto.createDto(OnAppClosed.class) .withActions(singletonList(dto.createDto(Action.class).withId("warnOnClose")))) .withOnAppLoaded( dto.createDto(OnAppLoaded.class) .withActions(Arrays.asList(dto.createDto(Action.class).withId("newProject"), dto.createDto(Action.class) .withId("openWelcomePage") .withProperties(ImmutableMap.of( "authenticatedTitle", "Greeting title for authenticated users", "authenticatedContentUrl", "http: .withOnProjectOpened(dto.createDto(OnProjectOpened.class) .withActions(Arrays.asList( dto.createDto(Action.class) .withId("openFile") .withProperties(singletonMap("file", "pom.xml")), dto.createDto(Action.class) .withId("run"), dto.createDto(Action.class) .withId("findReplace") .withProperties(ImmutableMap.of("in", "src/main/resources/consts2.properties", "find", "OLD_VALUE_2", "replace", "NEW_VALUE_2", "replaceMode", "mode")))))); factoryBuilder.checkValid(actual); verify(sourceProjectParametersValidator).validate(any(ImportSourceDescriptor.class), eq(FactoryParameter.Version.V2_1)); }
@Test(expectedExceptions = ApiException.class) public void shouldNotValidateUnparseableFactory() throws ApiException, URISyntaxException { factoryBuilder.checkValid(null); }
@Test(expectedExceptions = ApiException.class, dataProvider = "setByServerParamsProvider", expectedExceptionsMessageRegExp = "You have provided an invalid parameter .* for this version of Factory parameters.*") public void shouldNotAllowUsingParamsThatCanBeSetOnlyByServer(Factory factory) throws InvocationTargetException, IllegalAccessException, ApiException, NoSuchMethodException { factoryBuilder.checkValid(factory); }
@Test(expectedExceptions = ApiException.class, dataProvider = "notValidParamsProvider") public void shouldNotAllowUsingNotValidParams(Factory factory) throws InvocationTargetException, IllegalAccessException, ApiException, NoSuchMethodException { factoryBuilder.checkValid(factory); }
@Test public void shouldBeAbleToValidateV2_0WithTrackedParamsWithoutAccountIdIfOnPremisesIsEnabled() throws Exception { factoryBuilder = new FactoryBuilder(sourceProjectParametersValidator); Factory factory = dto.createDto(Factory.class); factory.withV("2.0") .withSource(dto.createDto(Source.class) .withProject(dto.createDto(ImportSourceDescriptor.class) .withType("git") .withLocation("location"))) .withPolicies(dto.createDto(Policies.class) .withRefererHostname("referrer") .withValidSince(123L) .withValidUntil(123L)) .withActions(dto.createDto(Actions.class).withWelcome(dto.createDto(WelcomePage.class))); factoryBuilder.checkValid(factory); }