Disable locking during deployment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / LockServletTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19 package org.openecomp.sdc.be.servlets;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response.Status;
27
28 import org.glassfish.hk2.utilities.binding.AbstractBinder;
29 import org.glassfish.jersey.server.ResourceConfig;
30 import org.glassfish.jersey.test.JerseyTest;
31 import org.glassfish.jersey.test.TestProperties;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.openecomp.sdc.be.components.validation.UserValidations;
38 import org.openecomp.sdc.be.config.ConfigurationManager;
39 import org.openecomp.sdc.be.config.SpringConfig;
40 import org.openecomp.sdc.be.impl.ComponentsUtils;
41 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
42 import org.openecomp.sdc.be.user.UserBusinessLogic;
43 import org.openecomp.sdc.common.api.ConfigurationSource;
44 import org.openecomp.sdc.common.api.Constants;
45 import org.openecomp.sdc.common.impl.ExternalConfiguration;
46 import org.openecomp.sdc.common.impl.FSConfigurationSource;
47 import org.springframework.context.ApplicationContext;
48 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
49
50 class LockServletTest extends JerseyTest {
51
52     @Mock
53     private HttpServletRequest request;
54     @Mock
55     private UserBusinessLogic userBusinessLogic;
56     @Mock
57     private ComponentsUtils componentsUtils;
58     @Mock
59     private IGraphLockOperation graphLockOperation;
60     @Mock
61     private UserValidations userValidations;
62
63     private static final String postUrl = "/v1/catalog/lock";
64     private static final String USER_ID = "cs0008";
65
66     @BeforeEach
67     void init() throws Exception {
68         super.setUp();
69         initConfig();
70     }
71
72     @AfterEach
73     void destory() throws Exception {
74         super.tearDown();
75     }
76
77     private void initConfig() {
78         final String appConfigDir = "src/test/resources/config/catalog-be";
79         final ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
80         final ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
81         final org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration();
82         configuration.setJanusGraphInMemoryGraph(true);
83         configurationManager.setConfiguration(configuration);
84         ExternalConfiguration.setAppName("catalog-be");
85     }
86
87     @Override
88     protected ResourceConfig configure() {
89         MockitoAnnotations.openMocks(this);
90         forceSet(TestProperties.CONTAINER_PORT, "0");
91         final ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
92         return new ResourceConfig(LockServlet.class)
93             .register(new AbstractBinder() {
94                 @Override
95                 protected void configure() {
96                     bind(request).to(HttpServletRequest.class);
97                     bind(userBusinessLogic).to(UserBusinessLogic.class);
98                     bind(componentsUtils).to(ComponentsUtils.class);
99                     bind(graphLockOperation).to(IGraphLockOperation.class);
100                     bind(userValidations).to(UserValidations.class);
101                 }
102             })
103             .property("contextConfig", context);
104     }
105
106     @Test
107     void disableLockTest() {
108         assertEquals(Status.OK.getStatusCode(), postDisableLock(true));
109         assertEquals(Status.OK.getStatusCode(), postDisableLock(false));
110         assertEquals(Status.OK.getStatusCode(), postDisableLock("true"));
111         assertEquals(Status.OK.getStatusCode(), postDisableLock("false"));
112         assertEquals(Status.BAD_REQUEST.getStatusCode(), postDisableLock("true1"));
113         assertEquals(Status.BAD_REQUEST.getStatusCode(), postDisableLock(null));
114     }
115
116     private int postDisableLock(Object disable) {
117         return target(postUrl).request(MediaType.APPLICATION_JSON)
118                 .header(Constants.USER_ID_HEADER, USER_ID)
119                 .post(Entity.json(disable)).getStatus();
120     }
121 }