2 * ============LICENSE_START==========================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=============================================
22 * ====================================================================
25 package org.onap.music.unittests;
27 import static org.junit.Assert.*;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
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;
38 public class JsonResponseTest {
40 JsonResponse result = null;
43 public void testJsonResponseBooleanStringString() {
44 result = new JsonResponse(ResultType.SUCCESS).setError("error").setMusicVersion("version");
45 assertEquals("error",result.getError());
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());
58 public void testError() {
59 result = new JsonResponse(ResultType.FAILURE);
60 result.setError("error");
61 assertTrue(result.getError().equals("error"));
63 assertFalse(result.getError().equals("error"));
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"));
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"));
84 result = new JsonResponse(ResultType.FAILURE);
85 myMap = result.toMap();
86 assertTrue(myMap.containsKey("status"));
87 assertEquals(ResultType.FAILURE, myMap.get("status"));
91 public void testMessage() {
92 result = new JsonResponse(ResultType.SUCCESS);
93 result.setMessage("message");
94 assertEquals("message", result.getMessage());
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());
108 public void testLock() {
109 result = new JsonResponse(ResultType.SUCCESS);
110 result.setLock("lock");
111 assertEquals("lock", result.getLock());
116 public void testLockLease() {
117 result = new JsonResponse(ResultType.SUCCESS);
118 result.setLockLease("lockLease");
119 assertEquals("lockLease", result.getLockLease());
123 public void testMusicBuild() {
124 result = new JsonResponse(ResultType.SUCCESS);
125 result.setMusicBuild("Build");
126 assertEquals("Build", result.getMusicBuild());
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());
138 public void testLockStatus() {
139 result = new JsonResponse(ResultType.SUCCESS);
140 LockStatus status = LockStatus.LOCKED;
141 result.setLockStatus(status);
142 assertEquals(status, result.getLockStatus());
147 public void testToString() {
148 result = new JsonResponse(ResultType.SUCCESS);
149 assertTrue(result.toString() instanceof String);
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"));
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);