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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.deployment.rest;
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;
40 * Test the Deployment rest resource.
42 public class RestResourceTest {
44 private EngineServiceFacade engineServiceFacadeMock;
45 private ApexDeploymentRestResource restResource;
48 * Set up mocking of the engine service facade.
50 * @throws ApexException on engine service facade setup errors
53 public void initializeMocking() throws ApexException {
54 MockitoAnnotations.initMocks(this);
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]);
61 restResource = Mockito.spy(new ApexDeploymentRestResource());
62 Mockito.doReturn(engineServiceFacadeMock).when(restResource).getEngineServiceFacade("apexServer", 12345);
64 Mockito.doReturn(engineServiceKey).when(engineServiceFacadeMock).getKey();
65 Mockito.doReturn(engineServiceKeyArray).when(engineServiceFacadeMock).getEngineKeyArray();
66 Mockito.doReturn(engineModel).when(engineServiceFacadeMock).getEngineStatus(engineKey);
70 public void testRestResourceCreateSession() throws ApexException {
71 Response response = restResource.createSession("apexServer", 12345);
72 assertEquals(200, response.getStatus());
76 public void testRestResourceCreateSessionWithApexModelKey() throws ApexException {
77 Mockito.doReturn(new AxArtifactKey("ModelKey:0.0.1")).when(engineServiceFacadeMock).getApexModelKey();
79 Response response = restResource.createSession("apexServer", 12345);
80 assertEquals(200, response.getStatus());
84 public void testRestResourceCreateSessionConnectException() throws ApexException {
85 Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init();
87 Response response = restResource.createSession("apexServer", 12345);
88 assertEquals(500, response.getStatus());
89 assertTrue(((String) response.getEntity()).contains("Error connecting to Apex Engine Service"));
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);
97 Response response = restResource.createSession("apexServer", 12345);
98 assertEquals(200, response.getStatus());
102 public void testRestResourceCreateSessionInfo() throws ApexException {
103 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
104 Mockito.doReturn("{}").when(engineServiceFacadeMock).getEngineInfo(engineKey);
106 Response response = restResource.createSession("apexServer", 12345);
107 assertEquals(200, response.getStatus());
111 public void testRestResourceCreateSessionNullInfo() throws ApexException {
112 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
113 Mockito.doReturn(null).when(engineServiceFacadeMock).getEngineInfo(engineKey);
115 Response response = restResource.createSession("apexServer", 12345);
116 assertEquals(200, response.getStatus());
120 public void testRestResourceCreateSessionEmptyInfo() throws ApexException {
121 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
122 Mockito.doReturn(" ").when(engineServiceFacadeMock).getEngineInfo(engineKey);
124 Response response = restResource.createSession("apexServer", 12345);
125 assertEquals(200, response.getStatus());
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);
133 Response response = restResource.createSession("apexServer", 12345);
134 assertEquals(200, response.getStatus());
138 public void testRestResourcemodelUpload() throws ApexException {
139 InputStream uploadedInputStream =
140 new ByteArrayInputStream("src/test/resources/models/SmallModel.json".getBytes());
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"));
149 public void testRestResourcemodelUploadNoConnection() throws ApexException {
150 Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init();
152 InputStream uploadedInputStream =
153 new ByteArrayInputStream("src/test/resources/models/SmallModel.json".getBytes());
156 restResource.modelUpload("apexServer", 12345, uploadedInputStream, "SmallModel.json", false, false);
157 assertEquals(500, response.getStatus());
161 public void testRestResourcemodelUploadDeploy() throws ApexException {
163 InputStream uploadedInputStream =
164 new ByteArrayInputStream("src/test/resources/models/SmallModel.json".getBytes());
166 Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock)
167 .deployModel("SmallModel.json", uploadedInputStream, false, true);
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"));