897c69d144ddb088866a8a8310bc1767694e3dda
[so.git] / adapters / mso-adapters-rest-interface / src / test / java / org / openecomp / mso / adapters / json / MapDeserializerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 Huawei Technologies Co., Ltd. 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.openecomp.mso.adapters.json;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25
26 import java.util.Map;
27 import org.codehaus.jackson.JsonParser;
28 import org.codehaus.jackson.map.DeserializationContext;
29 import org.codehaus.jackson.map.ObjectMapper;
30 import org.codehaus.jettison.json.JSONException;
31 import org.codehaus.jettison.json.JSONObject;
32 import org.junit.Test;
33
34 public class MapDeserializerTest {
35
36     private static final String MAP_KEY = "keyTest";
37     private static final String MAP_VALUE = "valueTest";
38
39     @Test
40     public void mapWithProperValuesIsReturned() throws Exception {
41         JsonParser parser = new ObjectMapper().getJsonFactory().createJsonParser(getJsonAsString());
42         MapDeserializer testedObject = new MapDeserializer();
43         Map<String, String> params = testedObject.deserialize(parser, mock(DeserializationContext.class));
44         assertThat(params).hasSize(1).containsEntry(MAP_KEY, MAP_VALUE);
45     }
46
47     private String getJsonAsString() throws JSONException {
48         JSONObject child2 = new JSONObject();
49         child2.put("key", MAP_KEY);
50         child2.put("value", MAP_VALUE);
51         JSONObject child1 = new JSONObject();
52         child1.put("child2", child2);
53         JSONObject parent = new JSONObject();
54         parent.put("child1", child1);
55         return parent.toString();
56     }
57 }