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.jupiter.api.Assertions.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 org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.aai.modelloader.config.ModelLoaderConfig;
40 import org.onap.aai.modelloader.entity.Artifact;
41 import org.onap.aai.modelloader.restclient.AaiRestClient;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
46 * Test the Model Artifact Handler using Mocks
49 public class TestModelArtifactHandler {
52 private ModelLoaderConfig config;
55 private AaiRestClient aaiClient;
58 public void setupMocks() {
59 MockitoAnnotations.openMocks(this);
63 public void testEmptyLists() {
64 ModelArtifactHandler handler = new ModelArtifactHandler(config);
65 handler.pushArtifacts(Collections.emptyList(), "", Collections.emptyList(), aaiClient);
66 handler.rollback(Collections.emptyList(), "", aaiClient);
71 public void testPushExistingModelsWithRollback() {
72 when(config.getAaiBaseUrl()).thenReturn("");
73 when(config.getAaiModelUrl(any())).thenReturn("");
75 ResponseEntity operationResult = mock(ResponseEntity.class);
76 when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(operationResult);
77 when(operationResult.getStatusCode()).thenReturn(HttpStatus.OK);
79 List<Artifact> artifacts = new ArrayList<>();
80 Artifact artifact = new ModelArtifact();
81 artifacts.add(artifact);
83 ModelArtifactHandler handler = new ModelArtifactHandler(config);
84 boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
86 handler.rollback(artifacts, "", aaiClient);
90 public void testPushNewModelsWithRollback() {
91 when(config.getAaiBaseUrl()).thenReturn("");
92 when(config.getAaiModelUrl(any())).thenReturn("");
93 when(config.getAaiNamedQueryUrl(any())).thenReturn("");
95 ResponseEntity getResult = mock(ResponseEntity.class);
96 when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(getResult);
97 when(getResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
99 ResponseEntity putResult = mock(ResponseEntity.class);
100 when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(putResult);
101 when(putResult.getStatusCode()).thenReturn(HttpStatus.CREATED);
103 List<Artifact> artifacts = new ArrayList<>();
104 artifacts.add(new ModelArtifact());
105 NamedQueryArtifact namedQueryArtifact = new NamedQueryArtifact();
106 namedQueryArtifact.setNamedQueryUuid("fred");
107 namedQueryArtifact.setModelNamespace("http://org.onap.aai.inventory/v13");
108 artifacts.add(namedQueryArtifact);
110 List<Artifact> completedArtifacts = new ArrayList<>();
111 ModelArtifactHandler handler = new ModelArtifactHandler(config);
112 boolean pushed = handler.pushArtifacts(artifacts, "", completedArtifacts, aaiClient);
113 assertThat(pushed, is(true));
114 handler.rollback(artifacts, "", aaiClient);
118 public void testPushNewModelsBadRequest() {
119 when(config.getAaiBaseUrl()).thenReturn("");
120 when(config.getAaiModelUrl(any())).thenReturn("");
121 when(config.getAaiNamedQueryUrl(any())).thenReturn("");
123 ResponseEntity getResult = mock(ResponseEntity.class);
124 when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(getResult);
125 when(getResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
127 ResponseEntity putResult = mock(ResponseEntity.class);
128 when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(putResult);
129 when(putResult.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST);
131 checkRollback(Collections.singletonList(new ModelArtifact()));
135 public void testBadRequestResourceModelResult() {
136 when(config.getAaiBaseUrl()).thenReturn("");
137 when(config.getAaiModelUrl(any())).thenReturn("");
139 ResponseEntity operationResult = mock(ResponseEntity.class);
140 when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(operationResult);
141 when(operationResult.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST);
143 checkRollback(Collections.singletonList(new ModelArtifact()));
146 private void checkRollback(List<Artifact> artifacts) {
147 ModelArtifactHandler handler = new ModelArtifactHandler(config);
148 boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
149 assertThat(pushed, is(false));
150 handler.rollback(artifacts, "", aaiClient);