aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java
blob: baf4673c1b3402b5a950f9654514190dc71575c1 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2017-2018 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.clamp.clds.it;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.NotFoundException;
import javax.xml.transform.TransformerException;

import org.apache.commons.codec.DecoderException;
import org.json.JSONException;
import org.json.simple.parser.ParseException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.onap.clamp.clds.dao.CldsDao;
import org.onap.clamp.clds.model.CldsEvent;
import org.onap.clamp.clds.model.CldsInfo;
import org.onap.clamp.clds.model.CldsModel;
import org.onap.clamp.clds.model.CldsServiceData;
import org.onap.clamp.clds.model.CldsTemplate;
import org.onap.clamp.clds.model.DcaeEvent;
import org.onap.clamp.clds.service.CldsService;
import org.onap.clamp.clds.util.LoggingUtils;
import org.onap.clamp.clds.util.ResourceFileUtil;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class CldsServiceItCase {

    @Autowired
    private CldsService cldsService;
    private String bpmnText;
    private String imageText;
    private String bpmnPropText;
    private String docText;

    @Autowired
    private CldsDao cldsDao;
    private Authentication authentication;
    private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
    private LoggingUtils util;

    /**
     * Setup the variable before the tests execution.
     *
     * @throws IOException
     *         In case of issues when opening the files
     */
    @Before
    public void setupBefore() throws IOException {
        bpmnText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-template.xml");
        imageText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/tca-img.xml");
        bpmnPropText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json");
        docText = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/doc-text.yaml");

        authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
        authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
        authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
        authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
        authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
        authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
        authList.add(new SimpleGrantedAuthority("permission-type-cl-event|dev|*"));
        authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);

        util = Mockito.mock(LoggingUtils.class);
        Mockito.doNothing().when(util).entering(Matchers.any(HttpServletRequest.class), Matchers.any(String.class));
        cldsService.setLoggingUtil(util);
    }

    @Test
    public void testCldsInfoNotAuthorized() {
        SecurityContext securityContext = Mockito.mock(SecurityContext.class);
        Authentication localAuth = Mockito.mock(Authentication.class);
        UserDetails userDetails = Mockito.mock(UserDetails.class);
        Mockito.when(userDetails.getUsername()).thenReturn("admin");
        Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth);
        Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails);

        cldsService.setSecurityContext(securityContext);
        CldsInfo cldsInfo = cldsService.getCldsInfo();
        assertFalse(cldsInfo.isPermissionReadCl());
        assertFalse(cldsInfo.isPermissionReadTemplate());
        assertFalse(cldsInfo.isPermissionUpdateCl());
        assertFalse(cldsInfo.isPermissionUpdateTemplate());
    }

    @Test
    public void testCldsInfoAuthorized() throws Exception {
        SecurityContext securityContext = Mockito.mock(SecurityContext.class);
        Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);

        cldsService.setSecurityContext(securityContext);
        CldsInfo cldsInfo = cldsService.getCldsInfo();
        assertTrue(cldsInfo.isPermissionReadCl());
        assertTrue(cldsInfo.isPermissionReadTemplate());
        assertTrue(cldsInfo.isPermissionUpdateCl());
        assertTrue(cldsInfo.isPermissionUpdateTemplate());
        Properties prop = new Properties();
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties");
        prop.load(in);
        in.close();
        assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version"));
        assertEquals(cldsInfo.getUserName(), "admin");
    }

    @Test
    public void testCompleteFlow() throws TransformerException, ParseException {
        SecurityContext securityContext = Mockito.mock(SecurityContext.class);
        Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);

        cldsService.setSecurityContext(securityContext);
        // Add the template first
        CldsTemplate newTemplate = new CldsTemplate();
        String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5);
        newTemplate.setName(randomNameTemplate);
        newTemplate.setBpmnText(bpmnText);
        newTemplate.setImageText(imageText);
        // Save the template in DB
        cldsDao.setTemplate(newTemplate, "user");
        // Test if it's well there
        CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate);
        assertEquals(bpmnText, newTemplateRead.getBpmnText());
        assertEquals(imageText, newTemplateRead.getImageText());
        // Save the model
        String randomNameModel = RandomStringUtils.randomAlphanumeric(5);
        CldsModel newModel = new CldsModel();
        newModel.setName(randomNameModel);
        newModel.setBpmnText(bpmnText);
        newModel.setImageText(imageText);
        newModel.setPropText(bpmnPropText);
        newModel.setControlNamePrefix("ClosedLoop-");
        newModel.setTemplateName(randomNameTemplate);
        newModel.setTemplateId(newTemplate.getId());
        newModel.setDocText(docText);
        // Test the PutModel method

        cldsService.putModel(randomNameModel, newModel);
        // Verify whether it has been added properly or not
        assertNotNull(cldsDao.getModel(randomNameModel));

        CldsModel model = cldsService.getModel(randomNameModel);
        // Verify with GetModel
        assertEquals(model.getTemplateName(), randomNameTemplate);
        assertEquals(model.getName(), randomNameModel);

        assertTrue(cldsService.getModelNames().size() >= 1);

        // Should fail
        ResponseEntity<?> responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_SUBMIT,
            randomNameModel, "false", cldsService.getModel(randomNameModel));
        assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
        assertNotNull(responseEntity.getBody());
        assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
        assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));

        responseEntity = cldsService.deployModel(randomNameModel, cldsService.getModel(randomNameModel));
        assertNotNull(responseEntity);
        assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
        assertNotNull(responseEntity.getBody());
        assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
        assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));

        responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_STOP,
            randomNameModel, "false", cldsService.getModel(randomNameModel));
        assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
        assertNotNull(responseEntity.getBody());
        assertTrue(CldsModel.STATUS_STOPPED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
        assertTrue(CldsModel.STATUS_STOPPED.equals(cldsService.getModel(randomNameModel).getStatus()));

        responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_RESTART,
            randomNameModel, "false", cldsService.getModel(randomNameModel));
        assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
        assertNotNull(responseEntity.getBody());
        assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
        assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));

        responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_UPDATE,
            randomNameModel, "false", cldsService.getModel(randomNameModel));
        assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
        assertNotNull(responseEntity.getBody());
        assertTrue(CldsModel.STATUS_ACTIVE.equals(((CldsModel) responseEntity.getBody()).getStatus()));
        assertTrue(CldsModel.STATUS_ACTIVE.equals(cldsService.getModel(randomNameModel).getStatus()));

        responseEntity = cldsService.unDeployModel(randomNameModel, cldsService.getModel(randomNameModel));
        assertNotNull(responseEntity);
        assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
        assertNotNull(responseEntity.getBody());
        assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(((CldsModel) responseEntity.getBody()).getStatus()));
        assertTrue(CldsModel.STATUS_DISTRIBUTED.equals(cldsService.getModel(randomNameModel).getStatus()));

        responseEntity = cldsService.putModelAndProcessAction(CldsEvent.ACTION_DELETE,
            randomNameModel, "false", cldsService.getModel(randomNameModel));
        assertNotNull(responseEntity);
        assertTrue(responseEntity.getStatusCode().equals(HttpStatus.OK));
        assertNotNull(responseEntity.getBody());
        try {
            cldsService.getModel(randomNameModel);
            fail("Should have raised an NotFoundException exception");
        } catch(NotFoundException ne) {

        }

    }

    @Test
    public void testDcaePost() {
        DcaeEvent dcaeEvent = new DcaeEvent();
        dcaeEvent.setArtifactName("ClosedLoop_with-enough-characters_TestArtifact.yml");
        dcaeEvent.setEvent(DcaeEvent.EVENT_CREATED);
        dcaeEvent.setResourceUUID("1");
        dcaeEvent.setServiceUUID("2");
        assertEquals(cldsService.postDcaeEvent("false", dcaeEvent),
            "event=created serviceUUID=2 resourceUUID=1 artifactName=ClosedLoop_with-enough-characters_TestArtifact.yml instance count=0 isTest=false");
    }

    @Test
    public void testGetSdcProperties() throws IOException {
        JSONAssert.assertEquals(
            ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-global.json"),
            cldsService.getSdcProperties(), true);
    }

    @Test
    public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException {
        String result = cldsService.getSdcServices();
        JSONAssert.assertEquals(
            ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result, true);
    }

    @Test
    public void testGetSdcPropertiesByServiceUuidForRefresh()
        throws GeneralSecurityException, DecoderException, JSONException, IOException {
        SecurityContext securityContext = Mockito.mock(SecurityContext.class);
        Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);

        cldsService.setSecurityContext(securityContext);
        // Test basic functionalities
        String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92",
            false);
        JSONAssert.assertEquals(
            ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"), result,
            true);
        // Now test the Cache effect
        CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
        // Should not be there, so should be null
        assertNull(cldsServiceDataCache);
        cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true);
        // Should be there now, so should NOT be null
        cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46");
        assertNotNull(cldsServiceDataCache);
        cldsDao.clearServiceCache();
    }
}