Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / assignment-provider / src / test / java / org / onap / ccsdk / config / assignment / service / ConfigResourceAssignmentTestUtils.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.config.assignment.service;\r
19 \r
20 import static org.mockito.Matchers.any;\r
21 import static org.mockito.Matchers.anyString;\r
22 import java.io.File;\r
23 import java.nio.charset.Charset;\r
24 import java.util.ArrayList;\r
25 import java.util.Arrays;\r
26 import java.util.Collection;\r
27 import java.util.List;\r
28 import java.util.Map;\r
29 import org.apache.commons.io.FileUtils;\r
30 import org.apache.commons.io.FilenameUtils;\r
31 import org.apache.commons.io.IOUtils;\r
32 import org.mockito.Matchers;\r
33 import org.mockito.Mockito;\r
34 import org.mockito.invocation.InvocationOnMock;\r
35 import org.mockito.stubbing.Answer;\r
36 import org.onap.ccsdk.config.data.adaptor.domain.ConfigResource;\r
37 import org.onap.ccsdk.config.data.adaptor.domain.TransactionLog;\r
38 import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService;\r
39 import org.onap.ccsdk.config.model.ConfigModelConstant;\r
40 import org.onap.ccsdk.config.model.data.dict.ResourceDefinition;\r
41 import org.onap.ccsdk.config.model.domain.ConfigModel;\r
42 import org.onap.ccsdk.config.model.domain.ConfigModelContent;\r
43 import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService;\r
44 import com.att.eelf.configuration.EELFLogger;\r
45 import com.att.eelf.configuration.EELFManager;\r
46 import com.fasterxml.jackson.core.type.TypeReference;\r
47 import com.fasterxml.jackson.databind.ObjectMapper;\r
48 \r
49 public class ConfigResourceAssignmentTestUtils {\r
50 \r
51     private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigResourceAssignmentTestUtils.class);\r
52 \r
53     public static void injectTransactionLogSaveMock(ConfigResourceService configResourceService) throws Exception {\r
54         Mockito.doAnswer(new Answer<Void>() {\r
55             @Override\r
56             public Void answer(InvocationOnMock invocationOnMock) throws Throwable {\r
57                 Object[] args = invocationOnMock.getArguments();\r
58                 if (args != null) {\r
59                     logger.trace("Transaction info " + Arrays.asList(args));\r
60                 }\r
61                 return null;\r
62             }\r
63         }).when(configResourceService).save(any(TransactionLog.class));\r
64     }\r
65 \r
66     public static void injectConfigModelMock(ConfigRestAdaptorService configRestAdaptorService,\r
67             String serviceTemplateName) throws Exception {\r
68 \r
69         Mockito.doAnswer(new Answer<ConfigModel>() {\r
70             @Override\r
71             public org.onap.ccsdk.config.model.domain.ConfigModel answer(InvocationOnMock invocationOnMock)\r
72                     throws Throwable {\r
73                 Object[] args = invocationOnMock.getArguments();\r
74                 org.onap.ccsdk.config.model.domain.ConfigModel serviceArtifact = null;\r
75                 if (args != null && args.length == 3) {\r
76 \r
77                     logger.info("Artifact info " + Arrays.asList(args));\r
78                     String modelContent = IOUtils.toString(\r
79                             ConfigResourceAssignmentTestUtils.class.getClassLoader().getResourceAsStream(\r
80                                     "service_templates/" + serviceTemplateName + "/" + serviceTemplateName + ".json"),\r
81                             Charset.defaultCharset());\r
82 \r
83                     ConfigModelContent configModelContent = new ConfigModelContent();\r
84                     configModelContent.setContent(modelContent);\r
85                     configModelContent.setContentType(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON);\r
86 \r
87                     List<ConfigModelContent> configModelContents = new ArrayList<>();\r
88                     configModelContents.add(configModelContent);\r
89 \r
90                     String velocityDir = ConfigResourceAssignmentTestUtils.class.getClassLoader()\r
91                             .getResource("service_templates/" + serviceTemplateName + "/velocity").getPath();\r
92 \r
93                     Collection<File> templateFiles =\r
94                             FileUtils.listFiles(new File(velocityDir), new String[] {"vtl"}, true);\r
95                     logger.info("Template Files info " + templateFiles);\r
96                     for (File templateFile : templateFiles) {\r
97                         String templateContent = FileUtils.readFileToString(templateFile, Charset.defaultCharset());\r
98                         ConfigModelContent configModelTemplateContent = new ConfigModelContent();\r
99                         configModelTemplateContent.setContent(templateContent);\r
100                         configModelTemplateContent.setName(FilenameUtils.getBaseName(templateFile.getName()));\r
101                         configModelTemplateContent.setContentType(ConfigModelConstant.MODEL_CONTENT_TYPE_TEMPLATE);\r
102                         configModelContents.add(configModelTemplateContent);\r
103                     }\r
104 \r
105                     serviceArtifact = new org.onap.ccsdk.config.model.domain.ConfigModel();\r
106                     serviceArtifact.setArtifactName(String.valueOf(args[0]));\r
107                     serviceArtifact.setArtifactVersion(String.valueOf(args[1]));\r
108                     serviceArtifact.setPublished("Y");\r
109                     serviceArtifact.setConfigModelContents(configModelContents);\r
110                 }\r
111 \r
112                 return serviceArtifact;\r
113             }\r
114         }).when(configRestAdaptorService).getResource(anyString(), anyString(), Matchers.any(Class.class));\r
115     }\r
116 \r
117     public static void injectResourceDictionaryMock(ConfigRestAdaptorService configRestAdaptorService,\r
118             String dictionaryFileName) throws Exception {\r
119 \r
120         Mockito.doAnswer(new Answer<String>() {\r
121             @Override\r
122             public String answer(InvocationOnMock invocationOnMock) throws Throwable {\r
123                 Object[] args = invocationOnMock.getArguments();\r
124                 String dictionaties = "[]";\r
125                 if (args != null) {\r
126                     logger.info("getResourceDictionaryByNames " + Arrays.asList(args));\r
127                     dictionaties = IOUtils.toString(\r
128                             ConfigAssignmentNodeTest.class.getClassLoader().getResourceAsStream(dictionaryFileName),\r
129                             Charset.defaultCharset());\r
130                 }\r
131                 return dictionaties;\r
132             }\r
133         }).when(configRestAdaptorService).postResource(Matchers.any(), Matchers.any(), Matchers.any(),\r
134                 Matchers.any(Class.class));\r
135     }\r
136 \r
137     public static void injectConfigResourceSaveMock(ConfigResourceService configResourceService) throws Exception {\r
138         Mockito.doAnswer(new Answer<ConfigResource>() {\r
139             @Override\r
140             public ConfigResource answer(InvocationOnMock invocationOnMock) throws Throwable {\r
141                 Object[] args = invocationOnMock.getArguments();\r
142                 ConfigResource configResource = null;\r
143                 if (args != null) {\r
144                     configResource = (ConfigResource) args[0];\r
145                     logger.info("Config Resource Save info " + configResource);\r
146                     return configResource;\r
147                 }\r
148                 return configResource;\r
149             }\r
150         }).when(configResourceService).saveConfigResource(any(ConfigResource.class));\r
151     }\r
152 \r
153     public static void injectGetConfigResourceMock(ConfigResourceService configResourceService,\r
154             ConfigResource configResource) throws Exception {\r
155         Mockito.doAnswer(new Answer<List<ConfigResource>>() {\r
156             @Override\r
157             public List<ConfigResource> answer(InvocationOnMock invocationOnMock) throws Throwable {\r
158                 Object[] args = invocationOnMock.getArguments();\r
159                 List<ConfigResource> configResources = new ArrayList<>();\r
160                 if (args != null) {\r
161                     configResources.add(configResource);\r
162                 }\r
163                 return configResources;\r
164             }\r
165         }).when(configResourceService).getConfigResource(any(ConfigResource.class));\r
166     }\r
167 \r
168     public static String getFileContent(String filePath) throws Exception {\r
169         return IOUtils.toString(ConfigResourceAssignmentTestUtils.class.getClassLoader().getResourceAsStream(filePath),\r
170                 Charset.defaultCharset());\r
171     }\r
172 \r
173     public static Map<String, ResourceDefinition> getMapfromJson(String content) {\r
174         try {\r
175             ObjectMapper mapper = new ObjectMapper();\r
176             return mapper.readValue(content, new TypeReference<Map<String, ResourceDefinition>>() {});\r
177         } catch (Exception e) {\r
178             logger.info("getMapfromJson Exception ({})", e);\r
179         }\r
180         return null;\r
181     }\r
182 }\r