/** * Create a subject if it does not already exist. * * @param subject * the name of the subject * @param configParams * the configuration values for the Subject, as form parameters * @return the subject name in a 200 response if successful. * HTTP 404 if the subject does not exist, or HTTP 409 if there was a conflict creating the subject */ @PUT @Path("{subject}") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response createSubject(@PathParam("subject") String subject, MultivaluedMap<String, String> configParams) { if (null == subject) { return Response.status(400).build(); } SubjectConfig.Builder builder = new SubjectConfig.Builder(); for(Map.Entry<String, List<String>> entry : configParams.entrySet()) { List<String> val = entry.getValue(); if(val.size() > 0) { builder.set(entry.getKey(), val.get(0)); } } Subject created = repo.register(subject, builder.build()); return Response.ok(created.getName()).build(); }
public Builder set(String key, String value) { if(key.startsWith(RESERVED_PREFIX)) { if(VALIDATORS_KEY.equals(key)) { setValidators(RepositoryUtil.commaSplit(value)); } else { throw new RuntimeException("SubjectConfig keys starting with '" + RESERVED_PREFIX + "' are reserved, failed to set: " + key + " to value: " + value); } } else { conf.put(key, value); } return this; }
if (subject == null) { subject = repo.register(convertSubject.toString(subjectName), defaultSubjectConfigBuilder.build());
if (subject == null) { subject = repo.register(convertSubject.toString(subjectName), defaultSubjectConfigBuilder.build());
@Test public void testSubjectConfigs() { String testKey = "test.key"; String testVal = "test.val"; String testVal2 = "test.val2"; SubjectConfig conf = new SubjectConfig.Builder().set(testKey, testVal).build(); SubjectConfig conf2 = new SubjectConfig.Builder().set(testKey, testVal2).build(); // lookup a subject that does not exist, when none do Subject none = repo.lookup(CONF); Assert.assertNull("non-existent subject lookup should return null", none); // register a subject Subject sub = repo.register(CONF, conf); Assert.assertNotNull("failed to register subject: " + CONF, sub); // a duplicate register is idempotent; the result is the same Subject sub2 = repo.register(CONF, conf2); Assert.assertNotNull("failed to re-register subject: " + SUB, sub2); validateSubject(sub, sub2); // lookup subject that was just registered Subject sub3 = repo.lookup(CONF); validateSubject(sub, sub3); // lookup something that is not there Subject sub4 = repo.lookup(NOCONF); Assert.assertNull("subject should not exist", sub4); }
/** * Create a subject if it does not already exist. * * @param subject * the name of the subject * @param configParams * the configuration values for the Subject, as form parameters * @return the subject name in a 200 response if successful. * HTTP 404 if the subject does not exist, or HTTP 409 if there was a conflict creating the subject */ @PUT @Path("{subject}") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response createSubject(@PathParam("subject") String subject, MultivaluedMap<String, String> configParams) { if (null == subject) { return Response.status(400).build(); } SubjectConfig.Builder builder = new SubjectConfig.Builder(); for(Map.Entry<String, List<String>> entry : configParams.entrySet()) { List<String> val = entry.getValue(); if(val.size() > 0) { builder.set(entry.getKey(), val.get(0)); } } Subject created = repo.register(subject, builder.build()); return Response.ok(created.getName()).build(); }
@Test public void testBuilderHashAndEquals() { SubjectConfig empty = SubjectConfig.emptyConfig(); Assert.assertEquals(empty, empty); SubjectConfig conf = new SubjectConfig.Builder().build(); Assert.assertEquals(empty, conf); SubjectConfig conf2 = new SubjectConfig.Builder() .set("repo.validators", null) .build(); // Explicitly setting empty or null validators is NOT the same as no validators // due to the default subject validators functionality (see Issue 38) Assert.assertNotEquals(conf, conf2); Assert.assertNotEquals(conf2, empty); Assert.assertNotEquals(conf.hashCode(), conf2.hashCode()); Assert.assertFalse(conf.equals(null)); Assert.assertFalse(conf.equals(new Object())); Assert.assertEquals(conf.hashCode(), empty.hashCode()); String k = "key"; String v = "val"; SubjectConfig custom = new SubjectConfig.Builder() .set(k, v).build(); SubjectConfig custom2 = new SubjectConfig.Builder() .set(custom.asMap()).build(); SubjectConfig custom3 = new SubjectConfig.Builder() .set(custom.asMap()).addValidator("foo").build(); Assert.assertEquals(custom, custom2); Assert.assertFalse(custom.equals(custom3)); Assert.assertFalse(custom.equals(conf)); Assert.assertEquals(custom.hashCode(), custom2.hashCode()); }
@Test public void testConfig() { Properties props = new Properties(); props.setProperty(Config.REPO_CLASS, InMemoryRepository.class.getName()); props.put(Config.VALIDATOR_PREFIX + "rejectAll", Reject.class.getName()); ConfigModule module = new ConfigModule(props); Injector injector = Guice.createInjector(module); Repository repo = injector.getInstance(Repository.class); Subject rejects = repo.register("rejects", new SubjectConfig.Builder() .addValidator("rejectAll").build()); boolean threw = false; try { rejects.register("stuff"); } catch (SchemaValidationException se) { threw = true; } Assert.assertTrue(threw); }
@Test public void testBuilder() { SubjectConfig conf = SubjectConfig.emptyConfig(); Assert.assertTrue(conf.getValidators().isEmpty()); SubjectConfig custom = new SubjectConfig.Builder() .set("k", "v") .set("repo.validators", "valid1, valid2 ,,") .addValidator("oneMore") .build(); Assert.assertEquals("v", custom.get("k")); Set<String> validators = custom.getValidators(); Assert.assertEquals(3, validators.size()); Assert.assertTrue(validators.contains("valid1")); Assert.assertTrue(validators.contains("valid2")); Assert.assertTrue(validators.contains("oneMore")); }
public Builder set(String key, String value) { if(key.startsWith(RESERVED_PREFIX)) { if(VALIDATORS_KEY.equals(key)) { setValidators(RepositoryUtil.commaSplit(value)); } else { throw new RuntimeException("SubjectConfig keys starting with '" + RESERVED_PREFIX + "' are reserved, failed to set: " + key + " to value: " + value); } } else { conf.put(key, value); } return this; }
@Test public void testValidation() { SubjectConfig conf = new SubjectConfig.Builder().addValidator(ValidatorFactory.REJECT_VALIDATOR).build(); // lookup a subject that does not exist, when none do Subject none = repo.lookup(VALIDATING); Assert.assertNull("non-existent subject lookup should return null", none); // register a subject that should reject all schemas Subject sub = repo.register(VALIDATING, conf); Assert.assertNotNull("failed to register subject: " + VALIDATING, sub); boolean threw = false; try { sub.register("stuff"); } catch (SchemaValidationException e) { threw = true; } Assert.assertTrue("must throw a SchemaValidationException", threw); }
public TypedSchemaRepository( Repository repo, Converter<ID> idConverter, Converter<SCHEMA> schemaConverter, Converter<SUBJECT> subjectConverter) { this(repo, idConverter, schemaConverter, subjectConverter, new SubjectConfig.Builder()); }
@Test(expected=RuntimeException.class) public void testInvalidConfigName() { new SubjectConfig.Builder() .set("repo.notValid", ""); }
@Test public void testSuccessfulValidation() throws SchemaValidationException { Subject accept = repo.register(ACCEPT, new SubjectConfig.Builder() .addValidator(ACCEPT_VALIDATOR).build()); SchemaEntry foo = accept.registerIfLatest(FOO, null); Assert.assertNotNull("failed to register schema", foo); SchemaEntry bar = accept.registerIfLatest(BAR, foo); Assert.assertNotNull("failed to register schema", bar); SchemaEntry none = accept.registerIfLatest("nothing", null); Assert.assertNull(none); SchemaEntry baz = accept.register(BAZ); Assert.assertNotNull("failed to register schema", baz); }
@Test(expected=SchemaValidationException.class) public void testCannotRegisterIfLatest() throws SchemaValidationException { Subject reject = repo.register(REJECT, new SubjectConfig.Builder() .addValidator(ValidatorFactory.REJECT_VALIDATOR).build()); reject.registerIfLatest(FOO, null); }
public TypedSchemaRepository( Repository repo, Converter<ID> idConverter, Converter<SCHEMA> schemaConverter, Converter<SUBJECT> subjectConverter) { this(repo, idConverter, schemaConverter, subjectConverter, new SubjectConfig.Builder()); }
public Builder set(Map<String, String> config) { for(Map.Entry<String, String> entry : config.entrySet()) { set(entry.getKey(), entry.getValue()); } return this; }
@Test(expected=SchemaValidationException.class) public void testCannotRegister() throws SchemaValidationException { Subject reject = repo.register(REJECT, new SubjectConfig.Builder() .addValidator(ValidatorFactory.REJECT_VALIDATOR).build()); reject.register(FOO); }
public Builder set(Map<String, String> config) { for(Map.Entry<String, String> entry : config.entrySet()) { set(entry.getKey(), entry.getValue()); } return this; }
/** * Returns an immutable Map<String, String> from the properties provided. * Includes any default values that exist in the properties. */ public static SubjectConfig configFromProperties(Properties props) { HashMap<String, String> propData = new HashMap<String, String>(); for (String key :props.stringPropertyNames()) { propData.put(key, props.getProperty(key)); } return new SubjectConfig.Builder().set(propData).build(); }