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