CRestBuilder { public CRest build() { putIfAbsentAndNotNull(crestProperties, DefaultResponseHandler.MIN_ERROR_STATUS_CODE_PROP, MIN_ERROR_STATUS_CODE); putIfAbsentAndNotNull(crestProperties, CRestConfig.class.getName() + "#placeholders", compile(placeholders)); CRestConfig crestConfig = new DefaultCRestConfig(crestProperties); ComponentRegistry<String,Deserializer> mimeDeserializerRegistry = buildDeserializerRegistry(crestConfig); ComponentRegistry<Class<?>,Deserializer> classDeserializerRegistry = classDeserializerBuilder.build(crestConfig); ComponentRegistry<Class<?>,Serializer> classSerializerRegistry = classSerializerBuilder.build(crestConfig); InterfaceConfigBuilderFactory icbf = new DefaultInterfaceConfigBuilderFactory(crestConfig, mimeDeserializerRegistry, classSerializerRegistry); ParamConfigBuilderFactory pcbf = new DefaultParamConfigBuilderFactory(crestConfig, classSerializerRegistry); putIfAbsentAndNotNull(crestProperties, ParamConfigBuilderFactory.class.getName(), pcbf); ProxyFactory pProxyFactory = getInstance(this.proxyFactory, this.proxyFactoryClass, crestConfig); HttpChannelFactory plainChannelFactory = getInstance(this.httpChannelFactory, this.httpChannelFactoryClass, crestConfig); Authorization authorization = buildAuthorization(plainChannelFactory); putIfAbsentAndNotNull(crestProperties, Authorization.class.getName(), authorization); ResponseDeserializer mimeResponseDeserializer = new ResponseDeserializerByMimeType(mimeDeserializerRegistry); ResponseDeserializer classResponseDeserializer = new ResponseDeserializerByClass(classDeserializerRegistry); ResponseDeserializer deserializersResponseDeserializer = new ResponseDeserializerByDeserializers(); ResponseDeserializer baseResponseDeserializer = new ResponseDeserializerComposite(deserializersResponseDeserializer, mimeResponseDeserializer, classResponseDeserializer); ResponseDeserializer customTypeResponseDeserializer = new ResponseDeserializerComposite(classResponseDeserializer, mimeResponseDeserializer); RequestExecutor requestExecutor = buildRequestExecutor(plainChannelFactory, authorization, baseResponseDeserializer, customTypeResponseDeserializer); if(JaxRsAnnotations.isJaxRsAware()) { this.annotationHandlerBuilder.register(JaxRsAnnotations.getMapping()); } InterfaceConfigFactory configFactory = new AnnotationDrivenInterfaceConfigFactory(icbf, annotationHandlerBuilder.build(crestConfig)); return new DefaultCRest(pProxyFactory, requestExecutor, requestBuilderFactory, configFactory); }  CRest build(); CRestBuilder setProxyFactory(Class<? extends ProxyFactory> proxyFactory); CRestBuilder setProxyFactory(ProxyFactory proxyFactory); CRestBuilder setHttpChannelFactory(Class<? extends HttpChannelFactory> httpChannelFactory); CRestBuilder setHttpChannelFactory(HttpChannelFactory httpChannelFactory); CRestBuilder setConcurrencyLevel(int concurrencyLevel); CRestBuilder dateFormat(String dateFormat); CRestBuilder booleanFormat(String trueFormat, String falseFormat); CRestBuilder addProperties(Map<String, Object> crestProperties); CRestBuilder property(String name, Object value); CRestBuilder setProperties(Map<String, Object> crestProperties); CRestBuilder endpoint(String endpoint); CRestBuilder addPlaceholders(Map<String,String> placeholders); CRestBuilder placeholder(String placeholder, String value); CRestBuilder setPlaceholders(Map<String,String> placeholders); CRestBuilder bindAnnotationHandler(Class<? extends AnnotationHandler<A>> handler, Class<A> annotationCls); CRestBuilder bindAnnotationHandler(Class<? extends AnnotationHandler<A>> handler, Class<A> annotationCls, Map<String, Object> config); CRestBuilder bindDeserializer(Class<? extends Deserializer> deserializer, String... mimeTypes); CRestBuilder bindDeserializer(Class<? extends Deserializer> deserializer, String[] mimeTypes, Map<String, Object> config); CRestBuilder bindDeserializer(Class<? extends Deserializer> deserializer, Class<?>... classes); CRestBuilder bindDeserializer(Class<? extends Deserializer> deserializer, Class<?>[] classes, Map<String, Object> config); CRestBuilder bindSerializer(Class<? extends Serializer> serializer, Class<?>... classes); CRestBuilder bindSerializer(Class<? extends Serializer> serializer, Class<?>[] classes, Map<String, Object> config); CRestBuilder oauth(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret); CRestBuilder oauth(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret, String sessionHandle, String accessTokenRefreshUrl); CRestBuilder basicAuth(String username, String password); CRestBuilder extractsEntityAuthParamsWith(String entityContentType, EntityParamExtractor entityParamExtractor); CRestBuilder bindJsonDeserializerWith(String... mimeTypes); CRestBuilder bindXmlDeserializerWith(String... mimeTypes); CRestBuilder bindPlainTextDeserializerWith(String... mimeTypes); CRestBuilder deserializeXmlWith(Class<? extends Deserializer> deserializer); CRestBuilder deserializeXmlWith(Class<? extends Deserializer> deserializer, Map<String, Object> config); CRestBuilder deserializeJsonWith(Class<? extends Deserializer> deserializer); CRestBuilder deserializeJsonWith(Class<? extends Deserializer> deserializer, Map<String, Object> config);  }
@Test public void buildShouldUseJdkProxyFactory() throws NoSuchFieldException, IllegalAccessException { CRest actual = toTest.build(); assertEquals(JdkProxyFactory.class, getFieldValue(actual, "proxyFactory").getClass()); }
@Test public void buildShouldUseHttpURLConnectionHttpChannelFactory() throws Exception{ CRestConfig config = mockCRestConfig(false); HttpURLConnectionHttpChannelFactory expected = mock(HttpURLConnectionHttpChannelFactory.class); mockStatic(ComponentFactory.class); when(ComponentFactory.instantiate(HttpURLConnectionHttpChannelFactory.class, config)).thenReturn(expected); CRest actual = toTest.build(); RequestExecutor retryingRequestExecutor = getFieldValue(actual, "requestExecutor"); assertEquals(RetryingRequestExecutor.class, retryingRequestExecutor.getClass()); RequestExecutor httpRequestExecutor = getFieldValue(retryingRequestExecutor, "delegate"); assertSame(HttpRequestExecutor.class, httpRequestExecutor.getClass()); HttpChannelFactory channelFactory = getFieldValue(httpRequestExecutor, "channelFactory"); assertSame(expected, channelFactory); verifyNew(DefaultCRestConfig.class).withArguments(baseCRestProperties()); }
@Test public void defaultBuildShouldUseDefaultCRestConfig() throws Exception{ mockCRestConfig(); toTest.build(); verifyNew(DefaultCRestConfig.class).withArguments(baseCRestProperties()); }
@Test public void buildShouldUseDefaultAnnotationHandlers() throws Exception { CRest actual = toTest.build(); InterfaceConfigFactory configFactory = Classes.getFieldValue(actual, "configFactory"); assertEquals(AnnotationDrivenInterfaceConfigFactory.class, configFactory.getClass()); ComponentRegistry annotationHandlerRegistry = Classes.getFieldValue(configFactory, "handlersRegistry"); Map mapping = Classes.getFieldValue(annotationHandlerRegistry, "mapping"); assertEquals(CRestAnnotations.getMapping().size() + JaxRsAnnotations.getMapping().size(), mapping.size()); assertTrue(mapping.keySet().containsAll(CRestAnnotations.getMapping().keySet())); assertTrue(mapping.keySet().containsAll(JaxRsAnnotations.getMapping().keySet())); }