SchemaRepository { public Schema getOrAdd(TypeToken<?> type, ApiConfig config) { Map<TypeToken<?>, Schema> typesForConfig = getAllTypesForConfig(config); Schema schema = getOrCreateTypeForConfig(type, typesForConfig, config); if (schema == PLACEHOLDER_SCHEMA) { throw new IllegalStateException("schema repository is in a bad state!"); } return schema; } SchemaRepository(TypeLoader typeLoader); Schema get(TypeToken<?> type, ApiConfig config); Schema getOrAdd(TypeToken<?> type, ApiConfig config); ImmutableList<Schema> getAllSchemaForApi(ApiKey apiKey);  }
@Test public void getOrAdd_genericRequestType() throws Exception { ApiMethodConfig methodConfig = fooEndpointSetParameterized(); checkParameterizedSchema( repo.getOrAdd(getRequestResource(methodConfig).getType(), config), Integer.class); }
@Test public void getOrAdd_genericReturnType() throws Exception { ApiMethodConfig methodConfig = fooEndpointSetParameterized(); checkParameterizedSchema( repo.getOrAdd(methodConfig.getReturnType(), config), Integer.class); }
@Test public void getOrAdd_responseCollection() throws Exception { ApiMethodConfig methodConfig = getMethodConfig("getIntegerCollection"); checkIntegerCollectionResponse(repo.getOrAdd(methodConfig.getReturnType(), config)); }
@Test public void getOrAdd_intArray() throws Exception { ApiMethodConfig methodConfig = getMethodConfig("getPrimitiveIntegerArray"); checkIntegerCollection(repo.getOrAdd(methodConfig.getReturnType(), config)); }
@Test public void getOrAdd_any() throws Exception { ApiMethodConfig methodConfig = getMethodConfig("getAny"); assertThat(repo.getOrAdd(methodConfig.getReturnType(), config)) .isEqualTo(SchemaRepository.ANY_SCHEMA); }
@Test public void getOrAdd_mapType() throws Exception { checkJsonMap("getStringArrayMap"); try { checkJsonMap("getArrayStringMap"); fail("Should have failed to generate map schema"); } catch (IllegalArgumentException e) { } ApiMethodConfig methodConfig = getMethodConfig("getStringEnumMap"); Schema schema = repo.getOrAdd(methodConfig.getReturnType(), config); assertThat(schema).isEqualTo(Schema.builder() .setName("Map_String_TestEnum") .setType("object") .setMapValueSchema(Field.builder() .setName(SchemaRepository.MAP_UNUSED_MSG) .setType(FieldType.ENUM) .setSchemaReference(SchemaReference.create(repo, config, TypeToken.of(TestEnum.class))) .build()) .build()); }
@Test public void getOrAdd_mapSubType() throws Exception { Schema expectedSchema = Schema.builder() .setName("Map_String_String") .setType("object") .setMapValueSchema(Field.builder() .setName(SchemaRepository.MAP_UNUSED_MSG) .setType(FieldType.STRING) .build()) .build(); assertThat(repo.getOrAdd(getMethodConfig("getMyMap").getReturnType(), config)) .isEqualTo(expectedSchema); assertThat(repo.getOrAdd(getMethodConfig("getMySubMap").getReturnType(), config)) .isEqualTo(expectedSchema); }
@Test public void getOrAdd_NestedMap() throws Exception { Schema expectedSchema = Schema.builder() .setName("Map_String_Map_String_String") .setType("object") .setMapValueSchema(Field.builder() .setName(SchemaRepository.MAP_UNUSED_MSG) .setType(FieldType.OBJECT) .setSchemaReference(SchemaReference.create(repo, config, new TypeToken<Map<String, String>>() {})) .build()) .build(); assertThat(repo.getOrAdd(getMethodConfig("getNestedMap").getReturnType(), config)) .isEqualTo(expectedSchema); }
@Test public void getOrAdd_mapTypeArrayValues() throws Exception { System.setProperty(MAP_SCHEMA_SUPPORT_ARRAYS_VALUES.systemPropertyName, "true"); try { ApiMethodConfig methodConfig = getMethodConfig("getStringArrayMap"); Schema schema = repo.getOrAdd(methodConfig.getReturnType(), config); assertThat(schema).isEqualTo(Schema.builder() .setName("Map_String_StringCollection") .setType("object") .setMapValueSchema(Field.builder() .setName(SchemaRepository.MAP_UNUSED_MSG) .setType(FieldType.ARRAY) .setArrayItemSchema(Field.builder() .setName(SchemaRepository.ARRAY_UNUSED_MSG) .setType(FieldType.STRING) .build()) .build()) .build()); } finally { System.clearProperty(MAP_SCHEMA_SUPPORT_ARRAYS_VALUES.systemPropertyName); } }
@Test public void getOrAdd_transformer() throws Exception { ApiMethodConfig methodConfig = getMethodConfig("getTransformed"); checkParameterizedSchema( repo.getOrAdd(methodConfig.getReturnType(), config), String.class); }
@Test public void getOrAdd_primitiveReturn() throws Exception { try { repo.getOrAdd(TypeToken.of(int.class), config); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
@Test public void getOrAdd_enum() throws Exception { assertThat(repo.getOrAdd(TypeToken.of(TestEnum.class), config)) .isEqualTo(Schema.builder() .setName("TestEnum") .setType("string") .addEnumValue("VALUE1") .addEnumValue("VALUE2") .addEnumDescription("") .addEnumDescription("") .build()); }
@Test public void getOrAdd_recursiveSchema() throws Exception { TypeToken<SelfReferencingObject> type = TypeToken.of(SelfReferencingObject.class); repo.getOrAdd(type, config); assertThat(repo.getOrAdd(type, config)) .isEqualTo(Schema.builder() .setName("SelfReferencingObject") .setType("object") .addField("foo", Field.builder() .setName("foo") .setType(FieldType.OBJECT) .setSchemaReference(SchemaReference.create(repo, config, type)) .build()) .build()); }