e8625af5e80c6cc9c0dc59964f9d1a187d2e098e
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.deployment.rest;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import java.io.ByteArrayInputStream;
26 import java.io.InputStream;
27 import javax.ws.rs.core.Response;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.policy.apex.core.deployment.ApexDeploymentException;
34 import org.onap.policy.apex.core.deployment.EngineServiceFacade;
35 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel;
38
39 /**
40  * Test the Deployment rest resource.
41  */
42 public class RestResourceTest {
43     @Mock
44     private EngineServiceFacade engineServiceFacadeMock;
45     private ApexDeploymentRestResource restResource;
46
47     /**
48      * Set up mocking of the engine service facade.
49      *
50      * @throws ApexException on engine service facade setup errors
51      */
52     @Before
53     public void initializeMocking() throws ApexException {
54         MockitoAnnotations.initMocks(this);
55
56         final AxArtifactKey engineServiceKey = new AxArtifactKey("EngineServiceKey", "0.0.1");
57         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
58         final AxArtifactKey[] engineServiceKeyArray = {engineKey};
59         final AxEngineModel engineModel = new AxEngineModel(engineServiceKeyArray[0]);
60
61         restResource = Mockito.spy(new ApexDeploymentRestResource());
62         Mockito.doReturn(engineServiceFacadeMock).when(restResource).getEngineServiceFacade("apexServer", 12345);
63
64         Mockito.doReturn(engineServiceKey).when(engineServiceFacadeMock).getKey();
65         Mockito.doReturn(engineServiceKeyArray).when(engineServiceFacadeMock).getEngineKeyArray();
66         Mockito.doReturn(engineModel).when(engineServiceFacadeMock).getEngineStatus(engineKey);
67     }
68
69     @Test
70     public void testRestResourceCreateSession() throws ApexException {
71         Response response = restResource.createSession("apexServer", 12345);
72         assertEquals(200, response.getStatus());
73     }
74
75     @Test
76     public void testRestResourceCreateSessionWithApexModelKey() throws ApexException {
77         Mockito.doReturn(new AxArtifactKey("ModelKey:0.0.1")).when(engineServiceFacadeMock).getApexModelKey();
78
79         Response response = restResource.createSession("apexServer", 12345);
80         assertEquals(200, response.getStatus());
81     }
82
83     @Test
84     public void testRestResourceCreateSessionConnectException() throws ApexException {
85         Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init();
86
87         Response response = restResource.createSession("apexServer", 12345);
88         assertEquals(500, response.getStatus());
89         assertTrue(((String) response.getEntity()).contains("Error connecting to Apex Engine Service"));
90     }
91
92     @Test
93     public void testRestResourceCreateSessionGetException() throws ApexException {
94         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
95         Mockito.doThrow(new ApexException("Exception on get")).when(engineServiceFacadeMock).getEngineStatus(engineKey);
96
97         Response response = restResource.createSession("apexServer", 12345);
98         assertEquals(200, response.getStatus());
99     }
100
101     @Test
102     public void testRestResourceCreateSessionInfo() throws ApexException {
103         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
104         Mockito.doReturn("{}").when(engineServiceFacadeMock).getEngineInfo(engineKey);
105
106         Response response = restResource.createSession("apexServer", 12345);
107         assertEquals(200, response.getStatus());
108     }
109
110     @Test
111     public void testRestResourceCreateSessionNullInfo() throws ApexException {
112         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
113         Mockito.doReturn(null).when(engineServiceFacadeMock).getEngineInfo(engineKey);
114
115         Response response = restResource.createSession("apexServer", 12345);
116         assertEquals(200, response.getStatus());
117     }
118
119     @Test
120     public void testRestResourceCreateSessionEmptyInfo() throws ApexException {
121         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
122         Mockito.doReturn(" ").when(engineServiceFacadeMock).getEngineInfo(engineKey);
123
124         Response response = restResource.createSession("apexServer", 12345);
125         assertEquals(200, response.getStatus());
126     }
127
128     @Test
129     public void testRestResourceCreateSessionExceptionInfo() throws ApexException {
130         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
131         Mockito.doThrow(new ApexException("Exception on info")).when(engineServiceFacadeMock).getEngineInfo(engineKey);
132
133         Response response = restResource.createSession("apexServer", 12345);
134         assertEquals(200, response.getStatus());
135     }
136
137     @Test
138     public void testRestResourcemodelUpload() throws ApexException {
139         InputStream uploadedInputStream =
140                 new ByteArrayInputStream("src/test/resources/models/SmallModel.json".getBytes());
141
142         Response response = restResource.modelUpload("apexServer", 12345,
143                 uploadedInputStream, "SmallModel.json", false, false);
144         assertEquals(200, response.getStatus());
145         assertTrue(((String) response.getEntity()).contains("SmallModel.json"));
146     }
147
148     @Test
149     public void testRestResourcemodelUploadNoConnection() throws ApexException {
150         Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init();
151
152         InputStream uploadedInputStream =
153                 new ByteArrayInputStream("src/test/resources/models/SmallModel.json".getBytes());
154
155         Response response =
156                 restResource.modelUpload("apexServer", 12345, uploadedInputStream, "SmallModel.json", false, false);
157         assertEquals(500, response.getStatus());
158     }
159
160     @Test
161     public void testRestResourcemodelUploadDeploy() throws ApexException {
162
163         InputStream uploadedInputStream =
164                 new ByteArrayInputStream("src/test/resources/models/SmallModel.json".getBytes());
165
166         Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock)
167                 .deployModel("SmallModel.json", uploadedInputStream, false, true);
168
169
170         Response response =
171                 restResource.modelUpload("apexServer", 12345, uploadedInputStream, "SmallModel.json", false, true);
172         assertEquals(500, response.getStatus());
173         assertTrue(((String) response.getEntity()).contains("Error updating model on engine service"));
174     }
175 }