2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.modelloader.entity.model;
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.hasSize;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
35 import javax.ws.rs.core.Response;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.aai.modelloader.config.ModelLoaderConfig;
42 import org.onap.aai.modelloader.entity.Artifact;
43 import org.onap.aai.modelloader.restclient.AaiRestClient;
44 import org.onap.aai.restclient.client.OperationResult;
47 * Test the Model Artifact Handler using Mocks
50 public class TestModelArtifactHandler {
53 private ModelLoaderConfig config;
56 private AaiRestClient aaiClient;
59 public void setupMocks() {
60 MockitoAnnotations.initMocks(this);
64 public void testEmptyLists() {
65 ModelArtifactHandler handler = new ModelArtifactHandler(config);
66 handler.pushArtifacts(Collections.emptyList(), "", Collections.emptyList(), aaiClient);
67 handler.rollback(Collections.emptyList(), "", aaiClient);
72 public void testSingleItemList() {
73 when(config.getAaiBaseUrl()).thenReturn("");
74 when(config.getAaiModelUrl(any())).thenReturn("");
76 ModelArtifactHandler handler = new ModelArtifactHandler(config);
77 List<Artifact> artifacts = Collections.singletonList(new ModelArtifact());
78 handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
79 handler.rollback(Collections.emptyList(), "", aaiClient);
80 assertThat(artifacts, hasSize(1));
84 public void testPushExistingModelsWithRollback() {
85 when(config.getAaiBaseUrl()).thenReturn("");
86 when(config.getAaiModelUrl(any())).thenReturn("");
88 OperationResult operationResult = mock(OperationResult.class);
89 when(aaiClient.getResource(any(), any(), any())).thenReturn(operationResult);
90 when(operationResult.getResultCode()).thenReturn(Response.Status.OK.getStatusCode());
92 List<Artifact> artifacts = new ArrayList<>();
93 Artifact artifact = new ModelArtifact();
94 artifacts.add(artifact);
96 ModelArtifactHandler handler = new ModelArtifactHandler(config);
97 boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
99 handler.rollback(artifacts, "", aaiClient);
103 public void testPushNewModelsWithRollback() {
104 when(config.getAaiBaseUrl()).thenReturn("");
105 when(config.getAaiModelUrl(any())).thenReturn("");
106 when(config.getAaiNamedQueryUrl(any())).thenReturn("");
108 OperationResult getResult = mock(OperationResult.class);
109 when(aaiClient.getResource(any(), any(), any())).thenReturn(getResult);
110 when(getResult.getResultCode()).thenReturn(Response.Status.NOT_FOUND.getStatusCode());
112 OperationResult putResult = mock(OperationResult.class);
113 when(aaiClient.putResource(any(), any(), any(), any())).thenReturn(putResult);
114 when(putResult.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode());
116 List<Artifact> artifacts = new ArrayList<>();
117 artifacts.add(new ModelArtifact());
118 NamedQueryArtifact namedQueryArtifact = new NamedQueryArtifact();
119 namedQueryArtifact.setNamedQueryUuid("fred");
120 namedQueryArtifact.setModelNamespace("http://org.onap.aai.inventory/v13");
121 artifacts.add(namedQueryArtifact);
123 List<Artifact> completedArtifacts = new ArrayList<>();
124 ModelArtifactHandler handler = new ModelArtifactHandler(config);
125 boolean pushed = handler.pushArtifacts(artifacts, "", completedArtifacts, aaiClient);
126 assertThat(pushed, is(true));
127 handler.rollback(artifacts, "", aaiClient);
131 public void testPushNewModelsBadRequest() {
132 when(config.getAaiBaseUrl()).thenReturn("");
133 when(config.getAaiModelUrl(any())).thenReturn("");
134 when(config.getAaiNamedQueryUrl(any())).thenReturn("");
136 OperationResult getResult = mock(OperationResult.class);
137 when(aaiClient.getResource(any(), any(), any())).thenReturn(getResult);
138 when(getResult.getResultCode()).thenReturn(Response.Status.NOT_FOUND.getStatusCode());
140 OperationResult putResult = mock(OperationResult.class);
141 when(aaiClient.putResource(any(), any(), any(), any())).thenReturn(putResult);
142 when(putResult.getResultCode()).thenReturn(Response.Status.BAD_REQUEST.getStatusCode());
144 checkRollback(Collections.singletonList(new ModelArtifact()));
148 public void testBadRequestResourceModelResult() {
149 when(config.getAaiBaseUrl()).thenReturn("");
150 when(config.getAaiModelUrl(any())).thenReturn("");
152 OperationResult operationResult = mock(OperationResult.class);
153 when(aaiClient.getResource(any(), any(), any())).thenReturn(operationResult);
154 when(operationResult.getResultCode()).thenReturn(Response.Status.BAD_REQUEST.getStatusCode());
156 checkRollback(Collections.singletonList(new ModelArtifact()));
160 public void testNullResourceModelResult() {
161 when(config.getAaiBaseUrl()).thenReturn("");
162 when(config.getAaiModelUrl(any())).thenReturn("");
163 when(aaiClient.getResource(any(), any(), any())).thenReturn(null);
165 checkRollback(Collections.singletonList(new ModelArtifact()));
168 private void checkRollback(List<Artifact> artifacts) {
169 ModelArtifactHandler handler = new ModelArtifactHandler(config);
170 boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
171 assertThat(pushed, is(false));
172 handler.rollback(artifacts, "", aaiClient);