|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2019 the original author or authors. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + *      https://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | +package io.pivotal.cfenv.boot.scs.serviceregistry; | 
|  | 17 | + | 
|  | 18 | +import java.util.HashMap; | 
|  | 19 | +import java.util.Map; | 
|  | 20 | + | 
|  | 21 | +import org.junit.Before; | 
|  | 22 | +import org.junit.Test; | 
|  | 23 | +import org.junit.runner.RunWith; | 
|  | 24 | +import org.mockito.InjectMocks; | 
|  | 25 | +import org.mockito.Mock; | 
|  | 26 | +import org.mockito.junit.MockitoJUnitRunner; | 
|  | 27 | + | 
|  | 28 | +import io.pivotal.cfenv.boot.scs.CfConfigClientProcessor; | 
|  | 29 | +import io.pivotal.cfenv.core.CfCredentials; | 
|  | 30 | +import io.pivotal.cfenv.core.CfService; | 
|  | 31 | + | 
|  | 32 | +import static org.assertj.core.api.Assertions.assertThat; | 
|  | 33 | +import static org.mockito.Mockito.when; | 
|  | 34 | + | 
|  | 35 | +@RunWith(MockitoJUnitRunner.class) | 
|  | 36 | +public class CfConfigClientProcessorTest { | 
|  | 37 | +    private static final String URI = "uri"; | 
|  | 38 | +    private static final String CLIENT_ID = "clientId"; | 
|  | 39 | +    private static final String CLIENT_SECRET = "clientSecret"; | 
|  | 40 | +    private static final String ACCESS_TOKEN_URI = "accessTokenUri"; | 
|  | 41 | + | 
|  | 42 | +    @Mock | 
|  | 43 | +    private CfService configService; | 
|  | 44 | +    @Mock | 
|  | 45 | +    private CfService otherService; | 
|  | 46 | +    @Mock | 
|  | 47 | +    private CfCredentials cfCredentials; | 
|  | 48 | + | 
|  | 49 | +    @InjectMocks | 
|  | 50 | +    private CfConfigClientProcessor configClientProcessor; | 
|  | 51 | + | 
|  | 52 | +    @Before | 
|  | 53 | +    public void setUp() { | 
|  | 54 | +        when(configService.existsByTagIgnoreCase("configuration")).thenReturn(true); | 
|  | 55 | + | 
|  | 56 | +        when(cfCredentials.getUri()).thenReturn(URI); | 
|  | 57 | +        when(cfCredentials.getString("client_id")).thenReturn(CLIENT_ID); | 
|  | 58 | +        when(cfCredentials.getString("client_secret")).thenReturn(CLIENT_SECRET); | 
|  | 59 | +        when(cfCredentials.getString("access_token_uri")).thenReturn(ACCESS_TOKEN_URI); | 
|  | 60 | +    } | 
|  | 61 | + | 
|  | 62 | +    @Test | 
|  | 63 | +    public void shouldAcceptConfigService() { | 
|  | 64 | +        boolean actual = configClientProcessor.accept(configService); | 
|  | 65 | + | 
|  | 66 | +        assertThat(actual).isTrue(); | 
|  | 67 | +    } | 
|  | 68 | + | 
|  | 69 | +    @Test | 
|  | 70 | +    public void shouldNotAcceptOtherService() { | 
|  | 71 | +        boolean actual = configClientProcessor.accept(otherService); | 
|  | 72 | + | 
|  | 73 | +        assertThat(actual).isFalse(); | 
|  | 74 | +    } | 
|  | 75 | + | 
|  | 76 | +    @Test | 
|  | 77 | +    public void shouldProcessCfCredentials() { | 
|  | 78 | +        Map<String, Object> properties = new HashMap<>(); | 
|  | 79 | + | 
|  | 80 | +        configClientProcessor.process(cfCredentials, properties); | 
|  | 81 | + | 
|  | 82 | +        assertThat(properties.get("spring.cloud.config.uri")).isEqualTo( URI); | 
|  | 83 | +        assertThat(properties.get("spring.cloud.config.client.oauth2.client-id")).isEqualTo(CLIENT_ID); | 
|  | 84 | +        assertThat(properties.get("spring.cloud.config.client.oauth2.client-secret")).isEqualTo(CLIENT_SECRET); | 
|  | 85 | +        assertThat(properties.get("spring.cloud.config.client.oauth2.access-token-uri")).isEqualTo(ACCESS_TOKEN_URI); | 
|  | 86 | +        assertThat(properties.get("spring.cloud.config.client.oauth2.scope")).isEqualTo(""); | 
|  | 87 | +    } | 
|  | 88 | + | 
|  | 89 | +    @Test | 
|  | 90 | +    public void shouldRetainThePropertiesDuringRefresh() { | 
|  | 91 | +        Map<String, Object> properties = new HashMap<>(); | 
|  | 92 | + | 
|  | 93 | +        configClientProcessor.process(cfCredentials, properties); | 
|  | 94 | + | 
|  | 95 | +        assertThat(properties.get("spring.cloud.refresh.additional-property-sources-to-retain")) | 
|  | 96 | +                .isEqualTo("CfConfigClientProcessor"); | 
|  | 97 | +    } | 
|  | 98 | +} | 
0 commit comments