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.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.aai.modelloader.config.ModelLoaderConfig;
39 import org.onap.aai.modelloader.entity.Artifact;
40 import org.onap.aai.modelloader.restclient.AaiRestClient;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
45 * Test the Model Artifact Handler using Mocks
48 public class TestModelArtifactHandler {
51 private ModelLoaderConfig config;
54 private AaiRestClient aaiClient;
57 public void setupMocks() {
58 MockitoAnnotations.openMocks(this);
62 public void testEmptyLists() {
63 ModelArtifactHandler handler = new ModelArtifactHandler(config);
64 handler.pushArtifacts(Collections.emptyList(), "", Collections.emptyList(), aaiClient);
65 handler.rollback(Collections.emptyList(), "", aaiClient);
70 public void testPushExistingModelsWithRollback() {
71 when(config.getAaiBaseUrl()).thenReturn("");
72 when(config.getAaiModelUrl(any())).thenReturn("");
74 ResponseEntity operationResult = mock(ResponseEntity.class);
75 when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(operationResult);
76 when(operationResult.getStatusCode()).thenReturn(HttpStatus.OK);
78 List<Artifact> artifacts = new ArrayList<>();
79 Artifact artifact = new ModelArtifact();
80 artifacts.add(artifact);
82 ModelArtifactHandler handler = new ModelArtifactHandler(config);
83 boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
85 handler.rollback(artifacts, "", aaiClient);
89 public void testPushNewModelsWithRollback() {
90 when(config.getAaiBaseUrl()).thenReturn("");
91 when(config.getAaiModelUrl(any())).thenReturn("");
92 when(config.getAaiNamedQueryUrl(any())).thenReturn("");
94 ResponseEntity getResult = mock(ResponseEntity.class);
95 when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(getResult);
96 when(getResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
98 ResponseEntity putResult = mock(ResponseEntity.class);
99 when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(putResult);
100 when(putResult.getStatusCode()).thenReturn(HttpStatus.CREATED);
102 List<Artifact> artifacts = new ArrayList<>();
103 artifacts.add(new ModelArtifact());
104 NamedQueryArtifact namedQueryArtifact = new NamedQueryArtifact();
105 namedQueryArtifact.setNamedQueryUuid("fred");
106 namedQueryArtifact.setModelNamespace("http://org.onap.aai.inventory/v13");
107 artifacts.add(namedQueryArtifact);
109 List<Artifact> completedArtifacts = new ArrayList<>();
110 ModelArtifactHandler handler = new ModelArtifactHandler(config);
111 boolean pushed = handler.pushArtifacts(artifacts, "", completedArtifacts, aaiClient);
112 assertThat(pushed, is(true));
113 handler.rollback(artifacts, "", aaiClient);
117 public void testPushNewModelsBadRequest() {
118 when(config.getAaiBaseUrl()).thenReturn("");
119 when(config.getAaiModelUrl(any())).thenReturn("");
120 when(config.getAaiNamedQueryUrl(any())).thenReturn("");
122 ResponseEntity getResult = mock(ResponseEntity.class);
123 when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(getResult);
124 when(getResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
126 ResponseEntity putResult = mock(ResponseEntity.class);
127 when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(putResult);
128 when(putResult.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST);
130 checkRollback(Collections.singletonList(new ModelArtifact()));
134 public void testBadRequestResourceModelResult() {
135 when(config.getAaiBaseUrl()).thenReturn("");
136 when(config.getAaiModelUrl(any())).thenReturn("");
138 ResponseEntity operationResult = mock(ResponseEntity.class);
139 when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(operationResult);
140 when(operationResult.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST);
142 checkRollback(Collections.singletonList(new ModelArtifact()));
145 private void checkRollback(List<Artifact> artifacts) {
146 ModelArtifactHandler handler = new ModelArtifactHandler(config);
147 boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
148 assertThat(pushed, is(false));
149 handler.rollback(artifacts, "", aaiClient);