c483f1388726c127bc03e1c39367ce587c5defa9
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / TestApplicationConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so;
22
23 import java.lang.reflect.Field;
24 import java.lang.reflect.Modifier;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.onap.so.bpmn.common.WorkflowTestTransformer;
29 import org.springframework.cloud.contract.wiremock.WireMockConfigurationCustomizer;
30 import org.springframework.context.annotation.Bean;
31 import org.springframework.context.annotation.Configuration;
32 import org.springframework.context.annotation.Profile;
33
34 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
35 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
36
37 @Configuration
38 @Profile({"test"})
39 public class TestApplicationConfig {
40         
41         @Bean
42         protected ResponseTransformer[] transformerArray() {
43                 // Process WorkflowTestTransformer annotations
44                 List<ResponseTransformer> transformerList = new ArrayList<ResponseTransformer>();
45
46                 for (Field field : getClass().getFields()) {
47                         WorkflowTestTransformer annotation = field.getAnnotation(WorkflowTestTransformer.class);
48
49                         if (annotation == null) {
50                                 continue;
51                         }
52
53                         if (!Modifier.isStatic(field.getModifiers())) {
54                                 throw new RuntimeException(field.getDeclaringClass().getName()
55                                         + "#" + field.getName() + " has a @WorkflowTestTransformer "
56                                         + " annotation but it is not declared static");
57                         }
58
59                         ResponseTransformer transformer;
60
61                         try {
62                                 transformer = (ResponseTransformer) field.get(null);
63                         } catch (IllegalAccessException e) {
64                                 throw new RuntimeException(field.getDeclaringClass().getName()
65                                         + "#" + field.getName() + " is not accessible", e);
66                         } catch (ClassCastException e) {
67                                 throw new RuntimeException(field.getDeclaringClass().getName()
68                                         + "#" + field.getName() + " is not a ResponseTransformer", e);
69                         }
70
71                         if (transformer == null) {
72                                 continue;
73                         }
74
75                         transformerList.add(transformer);
76                 }
77
78                 ResponseTransformer[] transformerArray =
79                         transformerList.toArray(new ResponseTransformer[transformerList.size()]);
80
81                 optionsCustomizer(transformerArray);
82                 
83                 return transformerArray;
84         }
85         
86         @Bean
87         WireMockConfigurationCustomizer optionsCustomizer(ResponseTransformer[] transformerArray) {
88                 return new WireMockConfigurationCustomizer() {
89                         @Override
90                         public void customize(WireMockConfiguration options) {
91                                 options.extensions(transformerArray);
92                         }
93                 };
94         }
95         
96 }