Replaced all tabs with spaces in java and pom.xml
[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 import org.onap.so.bpmn.common.WorkflowTestTransformer;
28 import org.springframework.cloud.contract.wiremock.WireMockConfigurationCustomizer;
29 import org.springframework.context.annotation.Bean;
30 import org.springframework.context.annotation.Configuration;
31 import org.springframework.context.annotation.Profile;
32 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
33 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
34
35 @Configuration
36 @Profile({"test"})
37 public class TestApplicationConfig {
38
39     @Bean
40     protected ResponseTransformer[] transformerArray() {
41         // Process WorkflowTestTransformer annotations
42         List<ResponseTransformer> transformerList = new ArrayList<ResponseTransformer>();
43
44         for (Field field : getClass().getFields()) {
45             WorkflowTestTransformer annotation = field.getAnnotation(WorkflowTestTransformer.class);
46
47             if (annotation == null) {
48                 continue;
49             }
50
51             if (!Modifier.isStatic(field.getModifiers())) {
52                 throw new RuntimeException(field.getDeclaringClass().getName() + "#" + field.getName()
53                         + " has a @WorkflowTestTransformer " + " annotation but it is not declared static");
54             }
55
56             ResponseTransformer transformer;
57
58             try {
59                 transformer = (ResponseTransformer) field.get(null);
60             } catch (IllegalAccessException e) {
61                 throw new RuntimeException(
62                         field.getDeclaringClass().getName() + "#" + field.getName() + " is not accessible", e);
63             } catch (ClassCastException e) {
64                 throw new RuntimeException(
65                         field.getDeclaringClass().getName() + "#" + field.getName() + " is not a ResponseTransformer",
66                         e);
67             }
68
69             if (transformer == null) {
70                 continue;
71             }
72
73             transformerList.add(transformer);
74         }
75
76         ResponseTransformer[] transformerArray =
77                 transformerList.toArray(new ResponseTransformer[transformerList.size()]);
78
79         optionsCustomizer(transformerArray);
80
81         return transformerArray;
82     }
83
84     @Bean
85     WireMockConfigurationCustomizer optionsCustomizer(ResponseTransformer[] transformerArray) {
86         return new WireMockConfigurationCustomizer() {
87             @Override
88             public void customize(WireMockConfiguration options) {
89                 options.extensions(transformerArray);
90             }
91         };
92     }
93
94 }