Open Kilda Java Documentation
MappingConfigurationValueProcessorTest.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.config.mapping;
17 
18 import static java.util.Collections.emptyMap;
19 import static java.util.Collections.singletonList;
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import com.google.common.collect.ImmutableMap;
27 import com.sabre.oss.conf4j.annotation.Configuration;
28 import com.sabre.oss.conf4j.annotation.Default;
29 import com.sabre.oss.conf4j.annotation.Key;
30 import com.sabre.oss.conf4j.factory.jdkproxy.JdkProxyStaticConfigurationFactory;
31 import com.sabre.oss.conf4j.source.MapConfigurationSource;
32 import org.junit.Before;
33 import org.junit.Test;
34 
36  private static final String TEST_VALUE = "test_value";
37  private static final String TEST_DEFAULT_VALUE = "test_default";
38  private static final String TEST_MAPPING_TARGET = "test_target";
39 
40  private JdkProxyStaticConfigurationFactory factory;
41  private MappingStrategy mappingStrategy;
42 
43  @Before
45  factory = new JdkProxyStaticConfigurationFactory();
46 
47  // Define mapping for TEST_MAPPING_TARGET.
48  mappingStrategy = mock(MappingStrategy.class);
49  when(mappingStrategy.isApplicable(eq(TEST_MAPPING_TARGET))).thenReturn(true);
50  when(mappingStrategy.apply(eq(TEST_MAPPING_TARGET), any())).thenReturn("mapped_value");
51  factory.setConfigurationValueProcessors(singletonList(new MappingConfigurationValueProcessor(mappingStrategy)));
52  }
53 
54  @Test
56  // given
57  MapConfigurationSource source = new MapConfigurationSource(ImmutableMap.of("test_key", TEST_VALUE));
58 
59  // when
60  factory.createConfiguration(TestConfig.class, source);
61 
62  // then
63  verify(mappingStrategy).apply(eq(TEST_MAPPING_TARGET), eq(TEST_VALUE));
64  }
65 
66  @Test
68  // given
69  MapConfigurationSource source = new MapConfigurationSource(emptyMap());
70 
71  // when
72  factory.createConfiguration(TestConfig.class, source);
73 
74  // then
75  verify(mappingStrategy).apply(eq(TEST_MAPPING_TARGET), eq(TEST_DEFAULT_VALUE));
76  }
77 
78  @Configuration
79  public interface TestConfig {
80  @Key("test_key")
81  @Default(TEST_DEFAULT_VALUE)
82  @Mapping(target = TEST_MAPPING_TARGET)
83  String getTestProperty();
84  }
85 }
boolean isApplicable(String mappingTarget)
target
Definition: nodes.py:50
source
Definition: nodes.py:53
String apply(String mappingTarget, String value)