Search in sources :

Example 56 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project dubbo by alibaba.

the class AnnotationPropertyValuesAdapterTest method test.

@Test
public void test() {
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("version", "1.0.0");
    mockEnvironment.setProperty("url", "   dubbo://localhost:12345");
    Field field = ReflectionUtils.findField(TestBean.class, "demoService");
    Reference reference = AnnotationUtils.getAnnotation(field, Reference.class);
    AnnotationPropertyValuesAdapter propertyValues = new AnnotationPropertyValuesAdapter(reference, mockEnvironment);
    ReferenceBean referenceBean = new ReferenceBean();
    DataBinder dataBinder = new DataBinder(referenceBean);
    dataBinder.setDisallowedFields("application", "module", "consumer", "monitor", "registry");
    DefaultConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new Converter<String[], String>() {

        @Override
        public String convert(String[] source) {
            return arrayToCommaDelimitedString(source);
        }
    });
    conversionService.addConverter(new Converter<String[], Map<String, String>>() {

        @Override
        public Map<String, String> convert(String[] source) {
            return CollectionUtils.toStringMap(source);
        }
    });
    dataBinder.setConversionService(conversionService);
    dataBinder.bind(propertyValues);
    // System.out.println(referenceBean);
    Assertions.assertEquals(DemoService.class, referenceBean.getInterfaceClass());
    Assertions.assertEquals("org.apache.dubbo.config.spring.api.DemoService", referenceBean.getInterface());
    Assertions.assertEquals("1.0.0", referenceBean.getVersion());
    Assertions.assertEquals("group", referenceBean.getGroup());
    Assertions.assertEquals("dubbo://localhost:12345", referenceBean.getUrl());
    Assertions.assertEquals("client", referenceBean.getClient());
    Assertions.assertEquals(true, referenceBean.isGeneric());
    Assertions.assertNull(referenceBean.isInjvm());
    Assertions.assertEquals(false, referenceBean.isCheck());
    Assertions.assertEquals(true, referenceBean.isInit());
    Assertions.assertEquals(true, referenceBean.getLazy());
    Assertions.assertEquals(true, referenceBean.getStubevent());
    Assertions.assertEquals("reconnect", referenceBean.getReconnect());
    Assertions.assertEquals(true, referenceBean.getSticky());
    Assertions.assertEquals("javassist", referenceBean.getProxy());
    Assertions.assertEquals("stub", referenceBean.getStub());
    Assertions.assertEquals("failover", referenceBean.getCluster());
    Assertions.assertEquals(Integer.valueOf(1), referenceBean.getConnections());
    Assertions.assertEquals(Integer.valueOf(1), referenceBean.getCallbacks());
    Assertions.assertEquals("onconnect", referenceBean.getOnconnect());
    Assertions.assertEquals("ondisconnect", referenceBean.getOndisconnect());
    Assertions.assertEquals("owner", referenceBean.getOwner());
    Assertions.assertEquals("layer", referenceBean.getLayer());
    Assertions.assertEquals(Integer.valueOf(1), referenceBean.getRetries());
    Assertions.assertEquals("random", referenceBean.getLoadbalance());
    Assertions.assertEquals(true, referenceBean.isAsync());
    Assertions.assertEquals(Integer.valueOf(1), referenceBean.getActives());
    Assertions.assertEquals(true, referenceBean.getSent());
    Assertions.assertEquals("mock", referenceBean.getMock());
    Assertions.assertEquals("validation", referenceBean.getValidation());
    Assertions.assertEquals(Integer.valueOf(2), referenceBean.getTimeout());
    Assertions.assertEquals("cache", referenceBean.getCache());
    Assertions.assertEquals("default,default", referenceBean.getFilter());
    Assertions.assertEquals("default,default", referenceBean.getListener());
    Map<String, String> data = new LinkedHashMap<String, String>();
    data.put("key1", "value1");
    Assertions.assertEquals(data, referenceBean.getParameters());
    // Bean compare
    Assertions.assertNull(referenceBean.getRegistry());
}
Also used : Reference(org.apache.dubbo.config.annotation.Reference) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) StringUtils.arrayToCommaDelimitedString(org.springframework.util.StringUtils.arrayToCommaDelimitedString) LinkedHashMap(java.util.LinkedHashMap) Field(java.lang.reflect.Field) MockEnvironment(org.springframework.mock.env.MockEnvironment) ReferenceBean(org.apache.dubbo.config.spring.ReferenceBean) DataBinder(org.springframework.validation.DataBinder) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 57 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-boot by spring-projects.

the class MapBinderTests method bindToMapWithCustomConverterAndChildElements.

@Test
void bindToMapWithCustomConverterAndChildElements() {
    // gh-11892
    DefaultConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new MapConverter());
    Binder binder = new Binder(this.sources, null, conversionService, null);
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo", "boom");
    source.put("foo.a", "a");
    source.put("foo.b", "b");
    this.sources.add(source);
    Map<String, String> map = binder.bind("foo", STRING_STRING_MAP).get();
    assertThat(map.get("a")).isEqualTo("a");
    assertThat(map.get("b")).isEqualTo("b");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.jupiter.api.Test)

Example 58 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-boot by spring-projects.

the class IsoOffsetDateTimeConverterTests method registerConverterShouldRegister.

@Test
void registerConverterShouldRegister() {
    DefaultConversionService service = new DefaultConversionService();
    IsoOffsetDateTimeConverter.registerConverter(service);
    OffsetDateTime time = service.convert("2011-12-03T10:15:30+01:00", OffsetDateTime.class);
    assertThat(time).isNotNull();
}
Also used : OffsetDateTime(java.time.OffsetDateTime) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.jupiter.api.Test)

Example 59 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-boot by spring-projects.

the class ConversionServiceParameterValueMapperTests method createWithConversionServiceShouldNotRegisterIsoOffsetDateTimeConverter.

@Test
void createWithConversionServiceShouldNotRegisterIsoOffsetDateTimeConverter() {
    ConversionService conversionService = new DefaultConversionService();
    ConversionServiceParameterValueMapper mapper = new ConversionServiceParameterValueMapper(conversionService);
    assertThatExceptionOfType(ParameterMappingException.class).isThrownBy(() -> mapper.mapParameterValue(new TestOperationParameter(OffsetDateTime.class), "2011-12-03T10:15:30+01:00"));
}
Also used : DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) ConversionService(org.springframework.core.convert.ConversionService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ParameterMappingException(org.springframework.boot.actuate.endpoint.invoke.ParameterMappingException) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.jupiter.api.Test)

Example 60 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project hub-alert by blackducksoftware.

the class AuditEntryActionsTest method testPagedRequest.

@Test
public void testPagedRequest() {
    int totalPages = 2;
    int currentPage = 0;
    int pageSize = 2;
    OffsetDateTime createdAt = DateUtils.createCurrentDateTimestamp();
    AlertNotificationModel entity_1 = new AlertNotificationModel(1L, 1L, "provider", "providerConfigName", "notificationType", "{content: \"content is here...\"}", createdAt, createdAt, false);
    entity_1.setId(1L);
    AlertNotificationModel entity_2 = new AlertNotificationModel(2L, 2L, "provider", "providerConfigName", "notificationType", "{content: \"content is here...\"}", createdAt, createdAt, false);
    entity_2.setId(2L);
    List<AlertNotificationModel> pagedEntryList = Arrays.asList(entity_1, entity_2);
    Page<AlertNotificationModel> pageResponse = Mockito.mock(Page.class);
    Mockito.when(pageResponse.getContent()).thenReturn(pagedEntryList);
    Mockito.when(pageResponse.getTotalPages()).thenReturn(totalPages);
    Mockito.when(pageResponse.getNumber()).thenReturn(currentPage);
    Mockito.when(pageResponse.getSize()).thenReturn(pageSize);
    AuthorizationManager authorizationManager = Mockito.mock(AuthorizationManager.class);
    Mockito.when(authorizationManager.hasReadPermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(Boolean.TRUE);
    AuditDescriptorKey auditDescriptorKey = new AuditDescriptorKey();
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    DefaultNotificationAccessor notificationManager = Mockito.mock(DefaultNotificationAccessor.class);
    Mockito.when(notificationManager.findAll(Mockito.any(PageRequest.class), Mockito.anyBoolean())).thenReturn(pageResponse);
    PageRequest pageRequest = PageRequest.of(currentPage, pageSize, Sort.unsorted());
    Mockito.when(notificationManager.getPageRequestForNotifications(Mockito.anyInt(), Mockito.anyInt(), Mockito.any(), Mockito.any())).thenReturn(pageRequest);
    NotificationContentRepository notificationRepository = Mockito.mock(NotificationContentRepository.class);
    AuditNotificationRepository auditNotificationRepository = Mockito.mock(AuditNotificationRepository.class);
    JobAccessor jobAccessor = Mockito.mock(JobAccessor.class);
    Mockito.when(jobAccessor.getJobById(Mockito.any())).thenReturn(null);
    ConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
    NotificationEntity notificationContent = new MockNotificationContent(DateUtils.createCurrentDateTimestamp(), "provider", DateUtils.createCurrentDateTimestamp(), "notificationType", "{content: \"content is here...\"}", 1L, 1L).createEntity();
    ContentConverter contentConverter = new ContentConverter(new DefaultConversionService());
    DistributionJobModel distributionJob = DistributionJobModel.builder().jobId(UUID.randomUUID()).enabled(true).blackDuckGlobalConfigId(2L).channelDescriptorName("distributionType").name("name").createdAt(OffsetDateTime.now()).distributionFrequency(FrequencyType.REAL_TIME).filterByProject(false).notificationTypes(List.of("type")).processingType(ProcessingType.DEFAULT).build();
    Mockito.doReturn(Optional.of(distributionJob)).when(jobAccessor).getJobById(Mockito.any());
    Mockito.when(notificationRepository.findAllById(Mockito.anyList())).thenReturn(Collections.singletonList(notificationContent));
    DefaultRestApiAuditAccessor auditEntryUtility = new DefaultRestApiAuditAccessor(auditEntryRepository, auditNotificationRepository, jobAccessor, configurationModelConfigurationAccessor, notificationManager, contentConverter);
    AuditEntryActions auditEntryActions = new AuditEntryActions(authorizationManager, auditDescriptorKey, auditEntryUtility, notificationManager, jobAccessor, null, null);
    ActionResponse<AuditEntryPageModel> response = auditEntryActions.get(currentPage, pageSize, null, null, null, true);
    assertTrue(response.hasContent());
    AuditEntryPageModel restModel = response.getContent().orElse(null);
    assertEquals(pageResponse.getTotalPages(), restModel.getTotalPages());
    assertEquals(pageResponse.getNumber(), restModel.getCurrentPage());
    assertEquals(pageResponse.getSize(), restModel.getPageSize());
    for (int index = 0; index < pageSize; index++) {
        AlertNotificationModel entity = pageResponse.getContent().get(index);
        AuditEntryModel entryRestModel = restModel.getContent().get(index);
        assertEquals(String.valueOf(entity.getId()), entryRestModel.getId());
    }
}
Also used : AlertNotificationModel(com.synopsys.integration.alert.common.rest.model.AlertNotificationModel) NotificationContentRepository(com.synopsys.integration.alert.database.notification.NotificationContentRepository) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AuditDescriptorKey(com.synopsys.integration.alert.component.audit.AuditDescriptorKey) AuditDescriptorKey(com.synopsys.integration.alert.component.audit.AuditDescriptorKey) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) PageRequest(org.springframework.data.domain.PageRequest) JobAccessor(com.synopsys.integration.alert.common.persistence.accessor.JobAccessor) AuditNotificationRepository(com.synopsys.integration.alert.database.audit.AuditNotificationRepository) DefaultRestApiAuditAccessor(com.synopsys.integration.alert.database.api.DefaultRestApiAuditAccessor) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) NotificationEntity(com.synopsys.integration.alert.database.notification.NotificationEntity) AuditEntryModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryModel) MockNotificationContent(com.synopsys.integration.alert.mock.entity.MockNotificationContent) DefaultNotificationAccessor(com.synopsys.integration.alert.database.api.DefaultNotificationAccessor) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) AuditEntryPageModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel) ContentConverter(com.synopsys.integration.alert.common.ContentConverter) AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) OffsetDateTime(java.time.OffsetDateTime) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)84 Test (org.junit.jupiter.api.Test)37 Test (org.junit.Test)29 ConversionService (org.springframework.core.convert.ConversionService)13 MethodParameter (org.springframework.core.MethodParameter)12 GenericConversionService (org.springframework.core.convert.support.GenericConversionService)12 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)12 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)12 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)11 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 RequestParam (org.springframework.web.bind.annotation.RequestParam)9 Optional (java.util.Optional)8 BeforeEach (org.junit.jupiter.api.BeforeEach)6 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)6 HashMap (java.util.HashMap)5 DefaultDatatypeChannelMessageConverter (org.springframework.integration.support.converter.DefaultDatatypeChannelMessageConverter)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 Before (org.junit.Before)4 ContentConverter (com.synopsys.integration.alert.common.ContentConverter)3 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)3