aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/DictionaryControllerTest.java
blob: 52b79b88fee7acc206470076c20040bcee842062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*-
 * ============LICENSE_START=======================================================
 * ONAP-PAP-REST
 * ================================================================================
 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.xacml.rest.controller;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
import org.onap.policy.rest.dao.CommonClassDao;
import org.onap.policy.rest.jpa.Attribute;
import org.onap.policy.rest.jpa.Category;
import org.onap.policy.rest.jpa.MicroServiceModels;
import org.onap.policy.rest.jpa.PolicyEditorScopes;
import org.springframework.mock.web.MockHttpServletResponse;

/**
 * The class <code>DictionaryControllerTest</code> contains tests
 * for the class {@link <code>DictionaryController</code>}*
 *
 * All JUnits are designed to run in the local development environment
 * where they have write privileges and can execute time-sensitive
 * tasks.
 */
public class DictionaryControllerTest {

    private static Logger logger = FlexLogger.getLogger(DictionaryControllerTest.class);
    private static CommonClassDao commonClassDao;
    private String jsonString = null;
    private HttpServletRequest request = null;
    private DictionaryController controller = null;

    @Before
    public void setUp() throws Exception {
        logger.info("setUp: Entering");
        commonClassDao = Mockito.mock(CommonClassDao.class);

        MicroServiceModels testData = new MicroServiceModels();
        testData.setVersion("1707.4.1.2-Junit");

        // --- mock the getDataByColumn() call
        List<String> microList = new ArrayList<String>();
        microList.add("123");
        List<Object> listId = new ArrayList<Object>();
        when(commonClassDao.getDataByColumn(Attribute.class, "xacmlId")).thenReturn(microList);
        List<Object> object = new ArrayList<>();
        object.add(new Category());
        when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
        PolicyEditorScopes editorScope = new PolicyEditorScopes();
        doNothing().when(commonClassDao).save(editorScope);
        doNothing().when(commonClassDao).update(editorScope);

        when(commonClassDao.getData(Attribute.class)).thenReturn(listId);

        request = mock(HttpServletRequest.class);
        controller = new DictionaryController(commonClassDao);
        new DictionaryUtils(commonClassDao);
        DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
        mock(DictionaryUtils.class);
        logger.info("setUp: exit");
    }

    @Test
    public void testGetAttributeDictionaryEntityDatabyAttributeName() {
        logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: Entering");

        MockHttpServletResponse response = new MockHttpServletResponse();

        controller.getAttributeDictionaryEntityDatabyAttributeName(response);

        try {
            assertTrue(response.getContentAsString() != null
                    && response.getContentAsString().contains("attributeDictionaryDatas"));
            logger.info("response.getContentAsString(): " + response.getContentAsString());
        } catch (UnsupportedEncodingException e) {
            fail("Exception: " + e);
        }

        logger.info("testGetAttributeDictionaryEntityDatabyAttributeName: exit");
    }

    @Test
    public void testGetAttributeDictionaryEntityData() {
        logger.info("testGetAttributeDictionaryEntityData: Entering");

        MockHttpServletResponse response = new MockHttpServletResponse();

        controller.getAttributeDictionaryEntityData(response);

        try {
            assertTrue(response.getContentAsString() != null
                    && response.getContentAsString().contains("attributeDictionaryDatas"));
            logger.info("response.getContentAsString(): " + response.getContentAsString());
        } catch (UnsupportedEncodingException e) {
            fail("Exception: " + e);
        }

        logger.info("testGetAttributeDictionaryEntityData: exit");
    }

    @Test
    public void testSaveAttributeDictionary() {
        logger.info("testSaveAttributeDictionary: Entering");

        MockHttpServletResponse response = new MockHttpServletResponse();
        request = mock(HttpServletRequest.class);

        try {
            jsonString =
                    "{\"attributeDictionaryData\":{\"datatypeBean\":{\"shortName\":\"string\"},\"description\":"
                            + "\"Qwerty\",\"priority\":\"High\",\"userDataTypeValues\":[{\"$$hashKey\":"
                            + "\"object:641\",\"attributeValues\":\"test\",\"id\":\"choice1\"},{\"$$hashKey\":"
                            + "\"object:646\",\"attributeValues\":\"test\",\"id\":\"choice2\"}],\"xacmlId\":"
                            + "\"Qwerty\"},\"userid\":\"demo\"}";
            BufferedReader br = new BufferedReader(new StringReader(jsonString));
            when(request.getReader()).thenReturn(br);
            controller.saveAttributeDictionary(request, response);
            logger.info("response.getContentAsString(): " + response.getContentAsString());
            assertTrue(response.getContentAsString() != null
                    && response.getContentAsString().contains("attributeDictionaryDatas"));

        } catch (Exception e) {
            fail("Exception: " + e);
        }

        logger.info("testSaveAttributeDictionary: exit");
    }

    @Test
    public void testRemoveAttributeDictionary() {
        logger.info("testRemoveAttributeDictionary: Entering");

        MockHttpServletResponse response = new MockHttpServletResponse();
        request = mock(HttpServletRequest.class);

        try {
            jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"xacmlId\":\"Test\"}}";
            BufferedReader br = new BufferedReader(new StringReader(jsonString));
            when(request.getReader()).thenReturn(br);
            controller.removeAttributeDictionary(request, response);
            logger.info("response.getContentAsString(): " + response.getContentAsString());
            assertTrue(response.getContentAsString() != null
                    && response.getContentAsString().contains("attributeDictionaryDatas"));

        } catch (Exception e) {
            fail("Exception: " + e);
        }

        logger.info("testRemoveAttributeDictionary: exit");
    }

    @Test
    public void testGetOnapNameDictionaryByNameEntityData() {
        logger.info("testGetOnapNameDictionaryByNameEntityData: Entering");

        MockHttpServletResponse response = new MockHttpServletResponse();

        controller.getOnapNameDictionaryByNameEntityData(response);

        try {
            assertTrue(response.getContentAsString() != null
                    && response.getContentAsString().contains("onapNameDictionaryDatas"));
            logger.info("response.getContentAsString(): " + response.getContentAsString());
        } catch (UnsupportedEncodingException e) {
            fail("Exception: " + e);
        }

        logger.info("testGetOnapNameDictionaryByNameEntityData: exit");
    }

    @Test
    public void testGetOnapNameDictionaryEntityData() {
        logger.info("testGetOnapNameDictionaryEntityData: Entering");

        MockHttpServletResponse response = new MockHttpServletResponse();

        controller.getOnapNameDictionaryEntityData(response);

        try {
            assertTrue(response.getContentAsString() != null
                    && response.getContentAsString().contains("onapNameDictionaryDatas"));
            logger.info("response.getContentAsString(): " + response.getContentAsString());
        } catch (UnsupportedEncodingException e) {
            fail("Exception: " + e);
        }

        logger.info("testGetOnapNameDictionaryEntityData: exit");
    }

    @Test
    public void testSaveOnapDictionary() {

        logger.info("testSaveOnapDictionary: Entering");

        MockHttpServletResponse response = new MockHttpServletResponse();
        request = mock(HttpServletRequest.class);

        try {
            jsonString =
                    "{\"userid\":\"demo\",\"onapNameDictionaryData\":{\"description\":\"test\",\"onapName\":\"Test\"}}";
            BufferedReader br = new BufferedReader(new StringReader(jsonString));
            when(request.getReader()).thenReturn(br);
            controller.saveOnapDictionary(request, response);
            logger.info("response.getContentAsString(): " + response.getContentAsString());
            assertTrue(response.getContentAsString() != null
                    && response.getContentAsString().contains("onapNameDictionaryDatas"));

        } catch (Exception e) {
            fail("Exception: " + e);
        }

        logger.info("testSaveOnapDictionary: exit");
    }

    @Test
    public void testRemoveOnapDictionary() {
        logger.info("testRemoveOnapDictionary: Entering");

        MockHttpServletResponse response = new MockHttpServletResponse();
        request = mock(HttpServletRequest.class);

        try {
            jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"onapName\":\"Test\"}}";
            BufferedReader br = new BufferedReader(new StringReader(jsonString));
            when(request.getReader()).thenReturn(br);
            controller.removeOnapDictionary(request, response);
            logger.info("response.getContentAsString(): " + response.getContentAsString());
            assertTrue(response.getContentAsString() != null
                    && response.getContentAsString().contains("onapNameDictionaryDatas"));

        } catch (Exception e) {
            fail("Exception: " + e);
        }

        logger.info("testRemoveOnapDictionary: exit");
    }

}