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;
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;
41 * Test the Deployment rest resource.
43 public class RestResourceTest {
45 private EngineServiceFacade engineServiceFacadeMock;
46 private ApexDeploymentRestResource restResource;
49 * Set up mocking of the engine service facade.
51 * @throws ApexException on engine service facade setup errors
54 public void initializeMocking() throws ApexException {
55 MockitoAnnotations.initMocks(this);
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]);
62 restResource = Mockito.spy(new ApexDeploymentRestResource());
63 Mockito.doReturn(engineServiceFacadeMock).when(restResource).getEngineServiceFacade("apexServer", 12345);
65 Mockito.doReturn(engineServiceKey).when(engineServiceFacadeMock).getKey();
66 Mockito.doReturn(engineServiceKeyArray).when(engineServiceFacadeMock).getEngineKeyArray();
67 Mockito.doReturn(engineModel).when(engineServiceFacadeMock).getEngineStatus(engineKey);
71 public void testRestResourceCreateSession() throws ApexException {
72 Response response = restResource.createSession("apexServer", 12345);
73 assertEquals(200, response.getStatus());
77 public void testRestResourceCreateSessionWithApexModelKey() throws ApexException {
78 Mockito.doReturn(new AxArtifactKey("ModelKey:0.0.1")).when(engineServiceFacadeMock).getApexModelKey();
80 Response response = restResource.createSession("apexServer", 12345);
81 assertEquals(200, response.getStatus());
85 public void testRestResourceCreateSessionConnectException() throws ApexException {
86 Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init();
88 Response response = restResource.createSession("apexServer", 12345);
89 assertEquals(500, response.getStatus());
90 assertTrue(((String) response.getEntity()).contains("Error connecting to Apex Engine Service"));
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);
98 Response response = restResource.createSession("apexServer", 12345);
99 assertEquals(200, response.getStatus());
103 public void testRestResourceCreateSessionInfo() throws ApexException {
104 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
105 Mockito.doReturn("{}").when(engineServiceFacadeMock).getEngineInfo(engineKey);
107 Response response = restResource.createSession("apexServer", 12345);
108 assertEquals(200, response.getStatus());
112 public void testRestResourceCreateSessionNullInfo() throws ApexException {
113 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
114 Mockito.doReturn(null).when(engineServiceFacadeMock).getEngineInfo(engineKey);
116 Response response = restResource.createSession("apexServer", 12345);
117 assertEquals(200, response.getStatus());
121 public void testRestResourceCreateSessionEmptyInfo() throws ApexException {
122 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
123 Mockito.doReturn(" ").when(engineServiceFacadeMock).getEngineInfo(engineKey);
125 Response response = restResource.createSession("apexServer", 12345);
126 assertEquals(200, response.getStatus());
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);
134 Response response = restResource.createSession("apexServer", 12345);
135 assertEquals(200, response.getStatus());
139 public void testRestResourcemodelUpload() throws ApexException {
140 InputStream uploadedInputStream =
141 new ByteArrayInputStream("src/test/resources/models/SmallModel.json".getBytes());
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"));
150 public void testRestResourcemodelUploadNoConnection() throws ApexException {
151 Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init();
153 InputStream uploadedInputStream =
154 new ByteArrayInputStream("src/test/resources/models/SmallModel.json".getBytes());
157 restResource.modelUpload("apexServer", 12345, uploadedInputStream, "SmallModel.json", false, false);
158 assertEquals(500, response.getStatus());
162 public void testRestResourcemodelUploadDeploy() throws ApexException {
164 InputStream uploadedInputStream =
165 new ByteArrayInputStream("src/test/resources/models/SmallModel.json".getBytes());
167 Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock)
168 .deployModel("SmallModel.json", uploadedInputStream, false, true);
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"));