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