Implement Attributes/Outputs BE (part 3)
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / ComponentAttributeServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021, Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.servlets;
21
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.when;
25
26 import fj.data.Either;
27 import java.util.Arrays;
28 import java.util.List;
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpSession;
31 import javax.ws.rs.core.Response;
32 import org.assertj.core.api.Assertions;
33 import org.glassfish.grizzly.http.util.HttpStatus;
34 import org.junit.Assert;
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.Spy;
42 import org.mockito.junit.jupiter.MockitoExtension;
43 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
44 import org.openecomp.sdc.be.dao.api.ActionStatus;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
47 import org.openecomp.sdc.be.model.AttributeDefinition;
48 import org.openecomp.sdc.common.api.Constants;
49 import org.openecomp.sdc.exception.ResponseFormat;
50 import org.springframework.web.context.WebApplicationContext;
51
52 @ExtendWith(MockitoExtension.class)
53 class ComponentAttributeServletTest extends JerseySpringBaseTest {
54
55     @Mock
56     private ServletContext context;
57     @Mock
58     private WebAppContextWrapper wrapper;
59     @Mock
60     private WebApplicationContext webAppContext;
61     @Mock
62     private HttpSession session;
63     @Mock
64     private AttributeBusinessLogic attributeBusinessLogic;
65     @Mock
66     private ComponentsUtils componentsUtils;
67     @InjectMocks
68     @Spy
69     private ComponentAttributeServlet componentAttributeServlet;
70
71     private static final String SERVICE_ID = "service1";
72     private static final String RESOURCE_ID = "resource1";
73     private static final String USER_ID = "jh0003";
74     private static final String VALID_PROPERTY_NAME = "valid_name_123";
75     private static final String INVALID_PROPERTY_NAME = "invalid_name_$.&";
76     private static final String STRING_TYPE = "string";
77
78     @BeforeEach
79     public void initClass() throws Exception {
80         super.setUp();
81         when(request.getSession()).thenReturn(session);
82     }
83
84     @AfterEach
85     public void tearDown() throws Exception {
86         super.tearDown();
87     }
88
89     @Test
90     void getAttributeListInService_success() {
91         AttributeDefinition attributeDefinition = new AttributeDefinition();
92         attributeDefinition.setName(VALID_PROPERTY_NAME);
93         attributeDefinition.setType(STRING_TYPE);
94
95         List<AttributeDefinition> attributeDefinitionEntryData = Arrays.asList(attributeDefinition);
96         when(attributeBusinessLogic.getAttributesList(any(), any())).thenReturn(Either.left(attributeDefinitionEntryData));
97
98         Response attributeInService = componentAttributeServlet.getAttributeListInService(SERVICE_ID, request, USER_ID);
99
100         Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), attributeInService.getStatus());
101     }
102
103     @Test
104     void getAttributeListInService_fail() {
105         when(attributeBusinessLogic.getAttributesList(any(), any()))
106             .thenReturn(Either.right(new ResponseFormat(Response.Status.NOT_FOUND.getStatusCode())));
107
108         Response attributeInService = componentAttributeServlet.getAttributeListInService(SERVICE_ID, request, USER_ID);
109
110         Assertions.assertThat(attributeInService).isNotNull();
111         Assertions.assertThat(attributeInService.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
112     }
113
114     @Test
115     void getAttributeListInResource_success() {
116         AttributeDefinition attributeDefinition = new AttributeDefinition();
117         attributeDefinition.setName(VALID_PROPERTY_NAME);
118         attributeDefinition.setType(STRING_TYPE);
119
120         List<AttributeDefinition> attributeDefinitionEntryData = Arrays.asList(attributeDefinition);
121         when(attributeBusinessLogic.getAttributesList(any(), any())).thenReturn(Either.left(attributeDefinitionEntryData));
122         when(attributeBusinessLogic.getAttributesList(any(), any())).thenReturn(Either.left(attributeDefinitionEntryData));
123
124         Response attributeInService =
125             componentAttributeServlet.getAttributeListInResource(RESOURCE_ID, request, USER_ID);
126
127         Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), attributeInService.getStatus());
128     }
129
130     @Test
131     void getAttributeListInResource_fail() {
132         when(session.getServletContext()).thenReturn(context);
133         when(context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(wrapper);
134         when(wrapper.getWebAppContext(any())).thenReturn(webAppContext);
135         when(webAppContext.getBean(ComponentsUtils.class)).thenReturn(componentsUtils);
136         when(attributeBusinessLogic.getAttributesList(any(), any())).thenThrow(new RuntimeException());
137
138         ResponseFormat responseFormat = new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode());
139
140         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(responseFormat);
141
142         Response attributeInService = componentAttributeServlet.getAttributeListInResource(SERVICE_ID, request, USER_ID);
143
144         Assertions.assertThat(attributeInService).isNotNull();
145         Assertions.assertThat(attributeInService.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
146     }
147
148 }