added test cases to JsonResponseTest.java
[music.git] / src / test / java / org / onap / music / unittests / JsonResponseTest.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Modifications Copyright (c) 2018-2019 IBM.
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  * 
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.unittests;
26
27 import static org.junit.Assert.*;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import org.junit.Test;
34 import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus;
35 import org.onap.music.main.ResultType;
36 import org.onap.music.response.jsonobjects.JsonResponse;
37
38 public class JsonResponseTest {
39
40     JsonResponse result = null;
41     
42     @Test
43     public void testJsonResponseBooleanStringString() {
44         result = new JsonResponse(ResultType.SUCCESS).setError("error").setMusicVersion("version");
45         assertEquals("error",result.getError());
46     }
47
48     @Test
49     public void testStatus() {
50         result = new JsonResponse(ResultType.SUCCESS);
51         result.setStatus(ResultType.SUCCESS);
52         assertEquals(ResultType.SUCCESS, result.getStatus());
53         result = new JsonResponse(ResultType.FAILURE).setError("error").setMusicVersion("version");
54         assertEquals(ResultType.FAILURE, result.getStatus());
55     }
56
57     @Test
58     public void testError() {
59         result = new JsonResponse(ResultType.FAILURE);
60         result.setError("error");
61         assertTrue(result.getError().equals("error"));
62         result.setError("");
63         assertFalse(result.getError().equals("error"));
64     }
65
66     @Test
67     public void testVersion() {
68         result = new JsonResponse(ResultType.SUCCESS);
69         result.setMusicVersion("version");
70         assertTrue(result.getMusicVersion().equals("version"));
71         result.setMusicVersion("");
72         assertFalse(result.getMusicVersion().equals("version"));
73     }
74
75     @Test
76     public void testToMap() {
77         result = new JsonResponse(ResultType.SUCCESS).setError("error").setMusicVersion("1.0");
78         Map<String,Object> myMap = result.toMap();
79         assertTrue(myMap.containsKey("status"));
80         assertEquals(ResultType.SUCCESS, myMap.get("status"));
81         assertEquals("error", myMap.get("error"));
82         assertEquals("1.0", myMap.get("version"));
83         
84         result = new JsonResponse(ResultType.FAILURE);
85         myMap = result.toMap();
86         assertTrue(myMap.containsKey("status"));
87         assertEquals(ResultType.FAILURE, myMap.get("status"));
88     }
89
90     @Test
91     public void testMessage() {
92         result = new JsonResponse(ResultType.SUCCESS);
93         result.setMessage("message");
94         assertEquals("message", result.getMessage());
95         
96     }
97     
98     @Test
99     public void testDataResult() {
100         result = new JsonResponse(ResultType.SUCCESS);
101         Map<String, HashMap<String, Object>> dataResult= new HashMap<>();
102         result.setDataResult(dataResult);
103         assertEquals(dataResult, result.getDataResult());
104         
105     }
106     
107     @Test
108     public void testLock() {
109         result = new JsonResponse(ResultType.SUCCESS);
110         result.setLock("lock");
111         assertEquals("lock", result.getLock());
112         
113     }
114     
115     @Test
116     public void testLockLease() {
117         result = new JsonResponse(ResultType.SUCCESS);
118         result.setLockLease("lockLease");
119         assertEquals("lockLease", result.getLockLease());
120     }
121     
122     @Test
123     public void testMusicBuild() {
124         result = new JsonResponse(ResultType.SUCCESS);
125         result.setMusicBuild("Build");
126         assertEquals("Build", result.getMusicBuild());
127     }
128     
129     @Test
130     public void testLockHolder() {
131         result = new JsonResponse(ResultType.SUCCESS);
132         List<String> lockHolders = new ArrayList<>();
133         result.setLockHolder(lockHolders);
134         assertEquals(lockHolders, result.getLockHolder());
135     }
136     
137     @Test
138     public void testLockStatus() {
139         result = new JsonResponse(ResultType.SUCCESS);
140         LockStatus status = LockStatus.LOCKED;
141         result.setLockStatus(status);
142         assertEquals(status, result.getLockStatus());
143         
144     }
145     
146     @Test
147     public void testToString() {
148         result = new JsonResponse(ResultType.SUCCESS);
149         assertTrue(result.toString() instanceof String);
150         
151     }
152     
153     @Test
154     public void testLockHolders() {
155         result = new JsonResponse(ResultType.SUCCESS).setLock("lockName").setLockHolder("lockholder1");
156         Map<String, Object> lockMap = (Map<String, Object>) result.toMap().get("lock");
157         // assure that this is string for backwards compatibility
158         assertEquals("lockholder1", lockMap.get("lock-holder"));
159
160         List<String> lockholders = new ArrayList<>();
161         lockholders.add("lockholder1");
162         lockholders.add("lockholder2");
163         result.setLockHolder(lockholders);
164         lockMap = (Map<String, Object>) result.toMap().get("lock");
165         assertEquals(lockMap.get("lock-holder"), lockholders);
166     }
167 }