Consolidate PolicyRestAdapter setup
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / controller / CreateDcaeMicroServiceControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.controller;
24
25 import static org.easymock.EasyMock.createMock;
26 import static org.easymock.EasyMock.expect;
27 import static org.easymock.EasyMock.replay;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 import com.fasterxml.jackson.databind.DeserializationFeature;
35 import com.fasterxml.jackson.databind.JsonNode;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.github.fge.jackson.JsonLoader;
38
39 import java.io.BufferedReader;
40 import java.io.File;
41 import java.io.FileInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.StringReader;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.LinkedList;
48 import java.util.List;
49 import java.util.Map;
50
51 import javax.servlet.ReadListener;
52 import javax.servlet.ServletInputStream;
53 import javax.servlet.http.HttpServletRequest;
54
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
60 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
61 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
62
63 import org.junit.Before;
64 import org.junit.Test;
65 import org.onap.policy.common.logging.flexlogger.FlexLogger;
66 import org.onap.policy.common.logging.flexlogger.Logger;
67 import org.onap.policy.rest.adapter.PolicyRestAdapter;
68 import org.onap.policy.rest.dao.CommonClassDao;
69 import org.onap.policy.rest.jpa.ConfigurationDataEntity;
70 import org.onap.policy.rest.jpa.MicroServiceModels;
71 import org.onap.policy.rest.jpa.PolicyEntity;
72 import org.springframework.mock.web.MockHttpServletRequest;
73 import org.springframework.mock.web.MockHttpServletResponse;
74
75 /**
76  * The class <code>CreateDcaeMicroServiceControllerTest</code> contains tests for the class
77  *
78  * <p/>All JUnits are designed to run in the local development environment where they have write privileges
79  * and can execute time-sensitive tasks.
80  *
81  */
82
83 public class CreateDcaeMicroServiceControllerTest {
84
85     private static Logger logger = FlexLogger.getLogger(CreateDcaeMicroServiceControllerTest.class);
86     private static CommonClassDao commonClassDao;
87     private String jsonString = null;
88     private String configBodyString = null;
89     private HttpServletRequest request = null;
90
91     /**
92      * setUp.
93      *
94      * @throws Exception should not throw one
95      */
96     @Before
97     public void setUp() throws Exception {
98         logger.info("setUp: Entering");
99         commonClassDao = mock(CommonClassDao.class);
100         MicroServiceModels testData = new MicroServiceModels();
101         testData.setVersion("OpenOnap-Junit");
102         testData.setModelName("modelName");
103         testData.setRuleFormation("triggerSignature.signaturesAlarm.alarmSignatures.alarmSignature[VnfType, "
104                 + "Contains, FilterValue]@SymptomTriggerSignature.signaturesSymptom.symptomSignatures."
105                 + "symptomSignature[symptomVnfType, symptomContains, symptomFilterValue]");
106         testData.setAttributes(
107                 "ParentCorrelation Name=String:defaultValue-null:required-true:MANY-false:description-null,"
108                         + "CorrelationWindow=String:defaultValue-null:required-true:MANY-false:description-null,"
109                         + "EmailNotification=String:defaultValue-null:required-true:MANY-false:description-null,"
110                         + "CorrelationPriority=string:defaultValue-null:required-true:MANY-false:description-null,");
111         testData.setRef_attributes("SymptomTriggerSignature=resource-model-symptomEntity:MANY-true:description-null,"
112                 + "triggerSignature=resource-model-entity:MANY-true:description-null,"
113                 + "SelectServerScope=SELECTSERVERSCOPE:MANY-false,logicalConnector=LOGICALCONNECTOR:MANY-false,"
114                 + "ParentCorrelationTraversal=PARENTCORRELATIONTRAVERSAL:MANY-false,");
115         testData.setSub_attributes(
116                 "{\"symptomAlarms\":{\"symptomContains\":\"SYMPTOMCONTAINS:defaultValue-null:required-true:MANY-false:"
117                         + "description-null\",\"symptomFilterValue\":\"string:defaultValue-null:"
118                         + "required-true:MANY-false:"
119                         + "description-null\",\"symptomVnfType\":\"SYMPTOMVNFTYPE:defaultValue-null:"
120                         + "required-true:MANY-false:"
121                         + "description-null\"},\"symptomElement\":{\"symptomSignatures\":\"symptomRange:required-true:"
122                         + "MANY-true:description-null\",\"symptomTraversal\":\"SYMPTOMTRAVERSAL:defaultValue-null:"
123                         + "required-true:MANY-false:description-null\"},\"alarms\":{\"Contains\":\"CONTAINS:"
124                         + "defaultValue-null:required-true:MANY-false:description-null\",\"VnfType\":\"VNFTYPE:"
125                         + "defaultValue-null:required-true:MANY-false:description-null\",\"FilterValue\":\"string:"
126                         + "defaultValue-null:required-true:MANY-false:description-null\"},\"resource-model-entity\":"
127                         + "{\"signaturesAlarm\":\"element:required-false:MANY-false:description-null\"},\"range\":"
128                         + "{\"alarmSignature\":\"alarms:required-true:MANY-false:description-null\"},\"symptomRange\":"
129                         + "{\"symptomSignature\":\"symptomAlarms:required-true:MANY-false:description-null\"},"
130                         + "\"element\":"
131                         + "{\"alarmSignatures\":\"range:required-true:MANY-true:description-null\",\"traversal\":"
132                         + "\"TRAVERSAL:" + "defaultValue-null:required-true:MANY-false:description-null\"},"
133                         + "\"resource-model-symptomEntity\":"
134                         + "{\"signaturesSymptom\":\"symptomElement:required-false:MANY-false:description-null\"}}");
135         testData.setAnnotation(
136                 "alarmSignatures=matching-true, symptomContains=matching-true, symptomSignatures=matching-true, "
137                         + "symptomTraversal=matching-true, symptomVnfType=matching-true, Contains=matching-true, "
138                         + "SelectServerScope=matching-true, VnfType=matching-true, traversal=matching-true, "
139                         + "logicalConnector=matching-true, ParentCorrelationTraversal=matching-true");
140         testData.setEnumValues("triggerSignature.signaturesAlarm.alarmSignatures.alarmSignature[VnfType, Contains, "
141                 + "FilterValue]@SymptomTriggerSignature.signaturesSymptom.symptomSignatures.symptomSignature"
142                 + "[symptomVnfType, symptomContains, symptomFilterValue]");
143         testData.setDataOrderInfo("triggerSignature.signaturesAlarm.alarmSignatures.alarmSignature[VnfType, Contains, "
144                 + "FilterValue]@SymptomTriggerSignature.signaturesSymptom.symptomSignatures."
145                 + "symptomSignature[symptomVnfType, symptomContains, symptomFilterValue]");
146         List<Object> microServiceModelsData = new ArrayList<Object>();
147         microServiceModelsData.add(testData);
148
149         // mock the getDataById() call
150         when(commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", "TESTMODEL" + ":" + "TODAY"))
151                 .thenReturn(microServiceModelsData);
152
153         jsonString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"DCAE\","
154                 + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,\"createdDate\":"
155                 + "\"2019-02-26 09:56:23.0\",\"modifiedDate\":\"2019-02-26 09:56:23.0\",\"version\":"
156                 + "\"\",\"createdBy\":\"super-admin\",\"modifiedBy\":\"super-admin\",\"roleType\":\"super-admin\","
157                 + "\"content\":\"\",\"recursive\":false},\"tempModel\":{\"name\":\"DCAE\",\"subScopename\":\"\","
158                 + "\"path\":[],\"type\":\"dir\",\"size\":0,\"createdDate\":\"2019-02-26 09:56:23.0\","
159                 + "\"modifiedDate\":\"2019-02-26 09:56:23.0\",\"version\":\"\",\"createdBy\":\"super-admin\","
160                 + "\"modifiedBy\":\"super-admin\",\"roleType\":\"super-admin\",\"content\":\"\","
161                 + "\"recursive\":false},\"$$hashKey\":\"object:354\",\"policy\":{\"policyType\":\"Config\","
162                 + "\"configPolicyType\":\"Micro Service\",\"serviceType\":\"TESTMODEL\",\"version\":\"TODAY\","
163                 + "\"ruleGridData\":[\"Correlation Priority\",\"Correlation Window\","
164                 + "\"Email Notification for failures\",\"Select Server Scope\","
165                 + "\"Parent Correlation Name\",\"Parent Correlation Traversal\","
166                 + "\"traversal\",\"FilterValue\"],\"policyName\":\"testttt\",\"onapName\":"
167                 + "\"asdafadf\",\"guard\":\"True\",\"riskType\":\"sfsgs\",\"riskLevel\":\"1\","
168                 + "\"priority\":\"1\",\"configName\":\"Search\",\"location\":\"Search\","
169                 + "\"uuid\":\"Search\",\"policyScope\":\"PolicyScope_ssaaa123\"}},"
170                 + "\"policyJSON\":{\"Correlation Priority\":\"testttt\",\"Correlation Window\":"
171                 + "\"testttt\",\"Email Notification for failures\":\"sds@l.com\","
172                 + "\"Select Server Scope\":\"testttt\",\"Parent Correlation Name\":"
173                 + "\"testttt\",\"Parent Correlation Traversal\":\"testttt\",\"logicalConnector\":"
174                 + "\"OR\",\"triggerSignature@0.signaturesAlarm.alarmSignatures@0.traversal\":\"testttt\","
175                 + "\"triggerSignature@0.signaturesAlarm.alarmSignatures@0.alarmSignature@0.VnfType\":"
176                 + "\"  testttt\",\"triggerSignature@0.signaturesAlarm.alarmSignatures@0."
177                 + "alarmSignature@0.Contains\":\"AND\",\"triggerSignature@0.signaturesAlarm."
178                 + "alarmSignatures@0.alarmSignature@0.FilterValue\":\"testttt\"}}";
179
180         configBodyString = "{\"service\":\"SniroPolicyEntityTest\",\"policyName\":\"someone\",\"description\":\"test\","
181                 + "\"templateVersion\":\"1607\",\"version\":\"HD\",\"priority\":\"2\","
182                 + "\"content\":{\"lastPolled\":\"1\",\"boolen-test\":\"true\",\"created\":\"test\","
183                 + "\"retiredDate\":\"test\",\"scope\":\"SNIRO_PLACEMENT_VDHV\",\"name\":\"test\","
184                 + "\"lastModified\":\"test\",\"state\":\"CREATED\",\"type\":\"CONFIG\",\"intent\":\"test\","
185                 + "\"target\":\"SNIRO\"}}";
186
187         request = mock(HttpServletRequest.class);
188         BufferedReader br = new BufferedReader(new StringReader(jsonString));
189         // mock the getReader() call
190         when(request.getReader()).thenReturn(br);
191
192         logger.info("setUp: exit");
193     }
194
195     /**
196      * Run the PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter, JsonNode) method test.
197      */
198
199     @Test
200     public void testSetDataToPolicyRestAdapter() {
201
202         logger.debug("testSetDataToPolicyRestAdapter: enter");
203
204         CreateDcaeMicroServiceController.setCommonClassDao(commonClassDao);
205
206         JsonNode root = null;
207         ObjectMapper mapper = new ObjectMapper();
208         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
209         PolicyRestAdapter policyData = null;
210         try {
211             root = JsonLoader.fromString(jsonString);
212             policyData = mapper.readValue(root.get("policyData").get("policy").toString(), PolicyRestAdapter.class);
213         } catch (Exception e) {
214             logger.error("testSetDataToPolicyRestAdapter", e);
215         }
216         CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
217         PolicyRestAdapter result = controller.setDataToPolicyRestAdapter(policyData, root);
218         assertTrue(result != null && result.getJsonBody() != null && !result.getJsonBody().isEmpty());
219
220         logger.debug("result.getJsonBody() : " + result.getJsonBody());
221         logger.debug("testSetDataToPolicyRestAdapter: exit");
222     }
223
224     /**
225      * Run the ModelAndView getDCAEMSTemplateData(HttpServletRequest, HttpServletResponse) method test.
226      */
227
228     @Test
229     public void testGetDcaeMsTemplateData() {
230
231         logger.debug("testGetDCAEMSTemplateData: enter");
232
233         CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
234         MockHttpServletResponse response = new MockHttpServletResponse();
235         String msModelJson = "{\"policyData\":\"DkatPolicyBody\"}";
236         try {
237
238             CreateDcaeMicroServiceController.setCommonClassDao(commonClassDao);
239
240             BufferedReader br = new BufferedReader(new StringReader(msModelJson));
241             // mock the getReader() call
242             when(request.getReader()).thenReturn(br);
243
244             List<Object> microServiceModelsData = new ArrayList<Object>();
245             MicroServiceModels testData = new MicroServiceModels();
246             testData.setVersion("1707.4.1.2-Junit");
247             microServiceModelsData.add(testData);
248             // mock the getDataById() call with the same MS model name
249             when(commonClassDao.getDataById(MicroServiceModels.class, "modelName", "DkatPolicyBody"))
250                     .thenReturn(microServiceModelsData);
251
252             controller.getDCAEMSTemplateData(request, response);
253
254             assertTrue(
255                     response.getContentAsString() != null && response.getContentAsString().contains("dcaeModelData"));
256
257             logger.debug("response: " + response.getContentAsString());
258
259         } catch (Exception e) {
260             logger.error("testGetDCAEMSTemplateData", e);
261         }
262
263         logger.debug("testGetDCAEMSTemplateData: exit");
264     }
265
266     /**
267      * Run the ModelAndView getModelServiceVersionData(HttpServletRequest, HttpServletResponse) method test.
268      */
269
270     @Test
271     public void testGetModelServiceVersionData() {
272
273         logger.debug("testGetModelServiceVersionData: enter");
274
275         CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
276         MockHttpServletResponse response = new MockHttpServletResponse();
277         String msModelJson = "{\"policyData\":\"DkatPolicyBody\"}";
278         try {
279
280             CreateDcaeMicroServiceController.setCommonClassDao(commonClassDao);
281
282             BufferedReader br = new BufferedReader(new StringReader(msModelJson));
283             // mock the getReader() call
284             when(request.getReader()).thenReturn(br);
285
286             List<Object> microServiceModelsData = new ArrayList<Object>();
287             MicroServiceModels testData = new MicroServiceModels();
288             testData.setVersion("1707.4.1.2-Junit");
289             microServiceModelsData.add(testData);
290
291             // mock the getDataById() call with the same MS model name
292             when(commonClassDao.getDataById(MicroServiceModels.class, "modelName", "DkatPolicyBody"))
293                     .thenReturn(microServiceModelsData);
294             controller.getModelServiceVersionData(request, response);
295
296             assertTrue(response.getContentAsString() != null
297                     && response.getContentAsString().contains("1707.4.1.2-Junit"));
298
299             logger.debug("response: " + response.getContentAsString());
300
301         } catch (Exception e) {
302             logger.error("testGetModelServiceVersionData", e);
303             fail("testGetModelServiceVersionData failed due to: " + e);
304         }
305
306         logger.debug("testGetModelServiceVersionData: exit");
307     }
308
309     /**
310      * Run the void getDCAEPriorityValuesData(HttpServletRequest, HttpServletResponse) method test.
311      */
312
313     @Test
314     public void testGetDcaePriorityValuesData() {
315
316         logger.debug("testGetDCAEPriorityValuesData: enter");
317
318         CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
319
320         MockHttpServletRequest request = new MockHttpServletRequest();
321         MockHttpServletResponse response = new MockHttpServletResponse();
322         try {
323             controller.getDCAEPriorityValuesData(request, response);
324             assertTrue(
325                     response.getContentAsString() != null && response.getContentAsString().contains("priorityDatas"));
326             logger.debug("response: " + response.getContentAsString());
327         } catch (Exception e) {
328             logger.error("testGetDCAEPriorityValuesData", e);
329             fail("testGetDCAEPriorityValuesData failed due to: " + e);
330         }
331
332         logger.debug("testGetDCAEPriorityValuesData: exit");
333     }
334
335     /**
336      * Run the void prePopulateDCAEMSPolicyData(PolicyRestAdapter, PolicyEntity) method test.
337      */
338
339     @Test
340     public void testPrePopulateDcaeMsPolicyData() {
341
342         logger.debug("testPrePopulateDCAEMSPolicyData: enter");
343
344         CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
345
346         // populate an entity object for testing
347         PolicyEntity entity = new PolicyEntity();
348         ConfigurationDataEntity configData = new ConfigurationDataEntity();
349         configData.setConfigBody(configBodyString);
350         entity.setConfigurationData(configData);
351
352         JsonNode root = null;
353         ObjectMapper mapper = new ObjectMapper();
354         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
355         PolicyRestAdapter restAdapter = null;
356
357         try {
358             root = JsonLoader.fromString(jsonString);
359             restAdapter = mapper.readValue(root.get("policyData").get("policy").toString(), PolicyRestAdapter.class);
360
361             // create guard attribute
362             MatchType matchType = new MatchType();
363             // set value
364             AttributeValueType attributeValue1 = new AttributeValueType();
365             attributeValue1.getContent().add("True");
366             matchType.setAttributeValue(attributeValue1);
367             // set Id
368             AttributeDesignatorType designator = new AttributeDesignatorType();
369             designator.setAttributeId("guard");
370             matchType.setAttributeDesignator(designator);
371             AllOfType alltype = new AllOfType();
372             alltype.getMatch().add(matchType);
373
374             // add a dummy MatchType object since while (matchList.size()>1 ...)
375             MatchType matchDummy = new MatchType();
376             // set value
377             AttributeValueType dummyValue = new AttributeValueType();
378             dummyValue.getContent().add("dummy");
379             matchDummy.setAttributeValue(dummyValue);
380             // set Id
381             AttributeDesignatorType designatorDummy = new AttributeDesignatorType();
382             designatorDummy.setAttributeId("dummyId");
383             matchDummy.setAttributeDesignator(designatorDummy);
384
385             alltype.getMatch().add(matchDummy);
386             AnyOfType anyOfType = new AnyOfType();
387             anyOfType.getAllOf().add(alltype);
388
389             TargetType target = new TargetType();
390             target.getAnyOf().add(anyOfType);
391
392             // create RiskType attribute
393             AnyOfType anyRiskType = new AnyOfType();
394             AllOfType allRiskType = new AllOfType();
395             MatchType matchRiskType = new MatchType();
396             // set value
397             AttributeValueType riskTypeValue = new AttributeValueType();
398             riskTypeValue.getContent().add("test");
399             matchRiskType.setAttributeValue(riskTypeValue);
400             // set Id
401             AttributeDesignatorType designatorRiskType = new AttributeDesignatorType();
402             designatorRiskType.setAttributeId("RiskType");
403             matchRiskType.setAttributeDesignator(designatorRiskType);
404             allRiskType.getMatch().add(matchRiskType);
405
406             // add a dummy MatchType object since while (matchList.size()>1 ...)
407             MatchType matchDummy1 = new MatchType();
408             // set value
409             AttributeValueType dummy1Value = new AttributeValueType();
410             dummy1Value.getContent().add("dummy");
411             matchDummy1.setAttributeValue(dummy1Value);
412             // set Id
413             AttributeDesignatorType designatorDummy1 = new AttributeDesignatorType();
414             designatorDummy1.setAttributeId("dummyId");
415             matchDummy1.setAttributeDesignator(designatorDummy1);
416
417             allRiskType.getMatch().add(matchDummy1);
418
419             anyRiskType.getAllOf().add(allRiskType);
420
421             target.getAnyOf().add(anyRiskType);
422
423             // create RiskLevel attribute
424             MatchType matchRiskLevel = new MatchType();
425             // set value
426             AttributeValueType riskLevel = new AttributeValueType();
427             riskLevel.getContent().add("3");
428             matchRiskLevel.setAttributeValue(riskLevel);
429             // set Id
430             AttributeDesignatorType designatorRiskLevel = new AttributeDesignatorType();
431             designatorRiskLevel.setAttributeId("RiskLevel");
432             matchRiskLevel.setAttributeDesignator(designatorRiskLevel);
433             AllOfType allRiskLevel = new AllOfType();
434             allRiskLevel.getMatch().add(matchRiskLevel);
435
436             // add a dummy MatchType object since while (matchList.size()>1 ...)
437             MatchType matchDummy2 = new MatchType();
438             // set value
439             AttributeValueType dummy2Value = new AttributeValueType();
440             dummy2Value.getContent().add("dummy");
441             matchDummy2.setAttributeValue(dummy2Value);
442             // set Id
443             AttributeDesignatorType designatorDummy2 = new AttributeDesignatorType();
444             designatorDummy2.setAttributeId("dummyId");
445             matchDummy2.setAttributeDesignator(designatorDummy2);
446
447             allRiskLevel.getMatch().add(matchDummy2);
448             AnyOfType anyRiskLevel = new AnyOfType();
449             anyRiskLevel.getAllOf().add(allRiskLevel);
450             target.getAnyOf().add(anyRiskLevel);
451             PolicyType policyType = new PolicyType();
452             policyType.setTarget(target);
453
454             restAdapter.setPolicyData(policyType);
455
456             controller.prePopulateDCAEMSPolicyData(restAdapter, entity);
457
458             logger.error("restAdapter.getRiskType() : " + restAdapter.getRiskType());
459             logger.error("restAdapter.getRiskLevel() : " + restAdapter.getRiskLevel());
460             logger.error("restAdapter.getGuard() : " + restAdapter.getGuard());
461
462             assertEquals("True", restAdapter.getGuard());
463             assertEquals("3", restAdapter.getRiskLevel());
464             assertEquals("test", restAdapter.getRiskType());
465
466         } catch (Exception e) {
467             logger.error("testPrePopulateDCAEMSPolicyData", e);
468             fail("testPrePopulateDCAEMSPolicyData failed due to: " + e);
469         }
470
471         logger.debug("testPrePopulateDCAEMSPolicyData: exit");
472
473     }
474
475     /**
476      * Run the Map<String,String> convert(String, String) method test.
477      */
478
479     @Test
480     public void testConvert() {
481         logger.debug("testConvert: enter");
482
483         String str = "k1=v1,k2=v2,k3=v3";
484         String split = ",";
485         Map<String, String> result = new CreateDcaeMicroServiceController().convert(str, split);
486         assertTrue(result != null && result.size() == 3);
487
488         logger.debug("testConvert: exit");
489     }
490
491     /**
492      * Run the Map<String,String> convertMap(Map<String,String>, Map<String,String>) method test.
493      */
494
495     @Test
496     public void testConvertMap() {
497         logger.debug("testConvertMap: enter");
498         Map<String, String> attributesMap = new HashMap<String, String>();
499
500         attributesMap.put("keyOne", "valueOne");
501         attributesMap.put("keyTwo", "valueTwo");
502         attributesMap.put("keyThree", "valueThree");
503         Map<String, String> attributesRefMap = new HashMap<String, String>();
504
505         attributesRefMap.put("key4", "value4");
506         attributesRefMap.put("key5", "value5");
507         attributesRefMap.put("key6", "value6");
508         CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
509         Map<String, String> attributesListRefMap = controller.getAttributesListRefMap();
510         LinkedList<String> list = new LinkedList<String>();
511
512         attributesListRefMap.put("key7", "value7");
513
514         list.add("l1");
515         list.add("l2");
516         Map<String, LinkedList<String>> arrayTextList = controller.getArrayTextList();
517         arrayTextList.put("key8", list);
518
519         Map<String, String> result = controller.convertMap(attributesMap, attributesRefMap);
520
521         assertTrue(result != null && result.size() == 8);
522
523         assertTrue(arrayTextList.get("key8").toString().contains("[l1, l2]"));
524
525         logger.debug("testConvertMap: exit");
526     }
527
528     /**
529      * Run the void SetMSModelData(HttpServletRequest, HttpServletResponse) method test.
530      */
531
532     // @Ignore
533     @Test
534     public void testSetMsModelData() {
535
536         logger.debug("testSetMSModelData: enter");
537
538         HttpServletRequest request = createMock(HttpServletRequest.class);
539         expect(request.getContentType())
540                 .andReturn("multipart/form-data; boundary=----WebKitFormBoundaryWcRUaIbC8kXgjr3p");
541         expect(request.getMethod()).andReturn("post");
542         expect(request.getHeader("Content-length")).andReturn("7809");
543
544         expect(request.getContentLength()).andReturn(7809);
545
546         try {
547             // value of fileName needs to be matched to your local directory
548             String fileName = "";
549             try {
550                 ClassLoader classLoader = getClass().getClassLoader();
551                 fileName = new File(classLoader.getResource("schedulerPolicies-v1707.xmi").getFile()).getAbsolutePath();
552             } catch (Exception e1) {
553                 logger.error("Exception Occured while loading file" + e1);
554             }
555             expect(request.getInputStream()).andReturn(new MockServletInputStream(fileName));
556             expect(request.getCharacterEncoding()).andReturn("UTF-8");
557             expect(request.getContentLength()).andReturn(1024);
558             replay(request);
559
560         } catch (Exception e) {
561             logger.error("testSetMSModelData" + e);
562             e.printStackTrace();
563         }
564
565         logger.debug("testSetMSModelData: exit");
566     }
567
568     /**
569      * @ Get File Stream.
570      *
571      */
572     private class MockServletInputStream extends ServletInputStream {
573
574         InputStream fis = null;
575
576         public MockServletInputStream(String fileName) {
577             try {
578                 fis = new FileInputStream(fileName);
579             } catch (Exception genExe) {
580                 genExe.printStackTrace();
581             }
582         }
583
584         @Override
585         public int read() throws IOException {
586             if (fis.available() > 0) {
587                 return fis.read();
588             }
589             return 0;
590         }
591
592         @Override
593         public int read(byte[] bytes, int len, int size) throws IOException {
594             if (fis.available() > 0) {
595                 int length = fis.read(bytes, len, size);
596                 return length;
597             }
598             return -1;
599         }
600
601         @Override
602         public boolean isFinished() {
603             return false;
604         }
605
606         @Override
607         public boolean isReady() {
608             return false;
609         }
610
611         @Override
612         public void setReadListener(ReadListener arg0) {
613
614         }
615     }
616 }