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