DefaultMethodConfigBuilder extends ConfigBuilder implements MethodConfigBuilder { public MethodConfig build() throws Exception { validate(); ParamConfig[] pConfigMethod = buildParams(methodParamConfigBuilders); ParamConfig[] pExtraParams = merge(ParamConfig.class, extraParams, buildParams(extraParamBuilders)); ParamConfig[] allParams = merge(ParamConfig.class, pConfigMethod, pExtraParams); return new DefaultMethodConfig( charset, method, RegexPathTemplate.create(buildPath()), produces, arrify(this.consumes, String.class), meth, socketTimeout, connectionTimeout, getEntityWriter(allParams), instantiate(requestInterceptor), instantiate(responseHandler), instantiate(errorHandler), instantiate(retryHandler), getDeserializers(), pConfigMethod, pExtraParams ); } DefaultMethodConfigBuilder(InterfaceConfigBuilder parent, Method method, CRestConfig crestConfig, ComponentRegistry<String,Deserializer> mimeDeserializerRegistry, ComponentRegistry<Class<?>, Serializer> classSerializerRegistry); MethodConfig build(); ParamConfigBuilder startParamConfig(int index); ParamConfigBuilder startExtraParamConfig(); MethodConfigBuilder setConsumes(String... mimeTypes); MethodConfigBuilder setCharset(Charset charset); MethodConfigBuilder setProduces(String contentType); MethodConfigBuilder appendPath(String path); MethodConfigBuilder setEndPoint(String endPoint); MethodConfigBuilder setType(MethodType meth); MethodConfigBuilder setSocketTimeout(int socketTimeout); MethodConfigBuilder setConnectionTimeout(int connectionTimeout); MethodConfigBuilder setDeserializer(Class<? extends Deserializer> deserializerClass); MethodConfigBuilder setRequestInterceptor(Class<? extends RequestInterceptor> requestInterceptorClass); MethodConfigBuilder setResponseHandler(Class<? extends ResponseHandler> responseHandlerClass); MethodConfigBuilder setErrorHandler(Class<? extends ErrorHandler> errorHandlerClass); MethodConfigBuilder setRetryHandler(Class<? extends RetryHandler> retryHandlerClass); MethodConfigBuilder setEntityWriter(Class<? extends EntityWriter> entityWriterClass); MethodConfigBuilder setParamsSerializer(Class<? extends Serializer> serializerClass); MethodConfigBuilder setParamsEncoded(boolean encoded); MethodConfigBuilder setParamsListSeparator(String listSeparator); @Override String toString();  }
@Test(expected = IllegalStateException.class) public void shouldUseDefaultValueForEndpoint() throws Exception { DefaultMethodConfigBuilder toTest = newToTest(); try { toTest.build(); } catch (Exception e) { assertEquals("End-point is mandatory. This is probably due to a missing or empty @EndPoint annotation.\n" + "Either provide an @EndPoint annotation or build a CRest instance as follow:\n" + "\n" + " String defaultEndPoint = ...;\n" + " CRest crest = CRest.getInstance(defaultEndPoint);\n" + "\n" + "Location information:\n" + "Method[method=public abstract void org.codegist.crest.test.util.TestInterface.m1(java.lang.String,int),interface=Mock for InterfaceConfigBuilder, hashCode: "+interfaceConfigBuilder.hashCode()+"]", e.getMessage()); throw e; } }
@Test public void shouldOverrideValueForEndpoint() throws Exception { mockEndpoint(); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockRegexPathTemplate, actual.getPathTemplate()); }
@Test public void shouldUseDefaultValueForProduces() throws Exception { mockEndpoint(); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertNull(actual.getProduces()); }
@Test public void shouldOverrideValueForProduces() throws Exception { mockEndpoint(); mockOverride(MethodConfig.METHOD_CONFIG_DEFAULT_PRODUCES, "value"); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertEquals("value",actual.getProduces()); }
@Test public void shouldUseDefaultValueForCharset() throws Exception { mockEndpoint(); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertEquals(Values.UTF8, actual.getCharset()); }
@Test public void shouldOverrideValueForCharset() throws Exception { mockEndpoint(); mockOverride(MethodConfig.METHOD_CONFIG_DEFAULT_CHARSET, Values.ISO_8859_1); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertEquals(Values.ISO_8859_1,actual.getCharset()); }
@Test public void shouldUseDefaultValueForSocketTimeout() throws Exception { mockEndpoint(); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertEquals(20000, actual.getSocketTimeout()); }
@Test public void shouldOverrideValueForSocketTimeout() throws Exception { mockEndpoint(); mockOverride(MethodConfig.METHOD_CONFIG_DEFAULT_SO_TIMEOUT, 10); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertEquals(10,actual.getSocketTimeout()); }
@Test public void shouldUseDefaultValueForConnectionTimeout() throws Exception { mockEndpoint(); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertEquals(20000, actual.getConnectionTimeout()); }
@Test public void shouldOverrideValueForConnectionTimeout() throws Exception { mockEndpoint(); mockOverride(MethodConfig.METHOD_CONFIG_DEFAULT_CO_TIMEOUT, 10); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertEquals(10,actual.getConnectionTimeout()); }
@Test public void shouldUseDefaultValueForExtraParams() throws Exception { mockEndpoint(); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertEquals(0, actual.getExtraParams().length); }
@Test public void shouldUseDefaultValueForRequestInterceptor() throws Exception { mockEndpoint(); NoOpRequestInterceptor mockNoOpRequestInterceptor = mock(NoOpRequestInterceptor.class); mockStatic(ComponentFactory.class); when(ComponentFactory.instantiate(NoOpRequestInterceptor.class, mockCRestConfig)).thenReturn(mockNoOpRequestInterceptor); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockNoOpRequestInterceptor, actual.getRequestInterceptor()); }
@Test public void shouldOverrideValueForRequestInterceptor() throws Exception { mockEndpoint(); TestRequestInterceptor mockTestRequestInterceptor = mock(TestRequestInterceptor.class); mockStatic(ComponentFactory.class); when(ComponentFactory.instantiate(TestRequestInterceptor.class, mockCRestConfig)).thenReturn(mockTestRequestInterceptor); mockOverride(MethodConfig.METHOD_CONFIG_DEFAULT_REQUEST_INTERCEPTOR, TestRequestInterceptor.class); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockTestRequestInterceptor, actual.getRequestInterceptor()); }
@Test public void shouldUseDefaultValueForResponseHandler() throws Exception { mockEndpoint(); DefaultResponseHandler mockDefaultResponseHandler = mock(DefaultResponseHandler.class); mockStatic(ComponentFactory.class); when(ComponentFactory.instantiate(DefaultResponseHandler.class, mockCRestConfig)).thenReturn(mockDefaultResponseHandler); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockDefaultResponseHandler, actual.getResponseHandler()); }
@Test public void shouldOverrideValueForResponseHandler() throws Exception { mockEndpoint(); TestResponseHandler mockTestResponseHandler = mock(TestResponseHandler.class); mockStatic(ComponentFactory.class); when(ComponentFactory.instantiate(TestResponseHandler.class, mockCRestConfig)).thenReturn(mockTestResponseHandler); mockOverride(MethodConfig.METHOD_CONFIG_DEFAULT_RESPONSE_HANDLER, TestResponseHandler.class); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockTestResponseHandler, actual.getResponseHandler()); }
@Test public void shouldUseDefaultValueForErrorHandler() throws Exception { mockEndpoint(); ErrorDelegatorHandler mockErrorHandler = mock(ErrorDelegatorHandler.class); mockStatic(ComponentFactory.class); when(ComponentFactory.instantiate(ErrorDelegatorHandler.class, mockCRestConfig)).thenReturn(mockErrorHandler); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockErrorHandler, actual.getErrorHandler()); }
@Test public void shouldOverrideValueForErrorHandler() throws Exception { mockEndpoint(); TestErrorHandler mockTestErrorHandler = mock(TestErrorHandler.class); mockStatic(ComponentFactory.class); when(ComponentFactory.instantiate(TestErrorHandler.class, mockCRestConfig)).thenReturn(mockTestErrorHandler); mockOverride(MethodConfig.METHOD_CONFIG_DEFAULT_ERROR_HANDLER, TestErrorHandler.class); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockTestErrorHandler, actual.getErrorHandler()); }
@Test public void shouldUseDefaultValueForRetryHandler() throws Exception { mockEndpoint(); MaxAttemptRetryHandler mockRetryHandler = mock(MaxAttemptRetryHandler.class); mockStatic(ComponentFactory.class); when(ComponentFactory.instantiate(MaxAttemptRetryHandler.class, mockCRestConfig)).thenReturn(mockRetryHandler); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockRetryHandler, actual.getRetryHandler()); }
@Test public void shouldOverrideValueForRetryHandler() throws Exception { mockEndpoint(); TestRetryHandler mockTestRetryHandler = mock(TestRetryHandler.class); mockStatic(ComponentFactory.class); when(ComponentFactory.instantiate(TestRetryHandler.class, mockCRestConfig)).thenReturn(mockTestRetryHandler); mockOverride(MethodConfig.METHOD_CONFIG_DEFAULT_RETRY_HANDLER, TestRetryHandler.class); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockTestRetryHandler, actual.getRetryHandler()); }
@Test public void shouldUseDefaultValueForPath() throws Exception { mockEndpoint(); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockRegexPathTemplate, actual.getPathTemplate()); }
@Test public void shouldOverrideValueForPath() throws Exception { mockEndpoint("http: mockOverride(MethodConfig.METHOD_CONFIG_DEFAULT_PATH, asList("/some","path")); DefaultMethodConfigBuilder toTest = newToTest(); MethodConfig actual = toTest.build(); assertCommons(actual); assertSame(mockRegexPathTemplate, actual.getPathTemplate()); }