Merge "Fix sonar issues"
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / entity / model / TestModelArtifactHandler.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.modelloader.entity.model;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.CoreMatchers.not;
25 import static org.hamcrest.Matchers.hasSize;
26 import static org.hamcrest.Matchers.isEmptyString;
27 import static org.junit.Assert.assertThat;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Matchers.any;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 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;
45
46 /**
47  * Test the Model Artifact Handler using Mocks
48  *
49  */
50 public class TestModelArtifactHandler {
51
52     @Mock
53     private ModelLoaderConfig config;
54
55     @Mock
56     private AaiRestClient aaiClient;
57
58     @Before
59     public void setupMocks() {
60         MockitoAnnotations.initMocks(this);
61     }
62
63     @Test
64     public void testEmptyLists() {
65         ModelArtifactHandler handler = new ModelArtifactHandler(config);
66         handler.pushArtifacts(Collections.emptyList(), "", Collections.emptyList(), aaiClient);
67         handler.rollback(Collections.emptyList(), "", aaiClient);
68         assertTrue(true);
69     }
70
71     @Test
72     public void testSingleItemList() {
73         when(config.getAaiBaseUrl()).thenReturn("");
74         when(config.getAaiModelUrl(any())).thenReturn("");
75
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));
81     }
82
83     @Test
84     public void testPushExistingModelsWithRollback() {
85         when(config.getAaiBaseUrl()).thenReturn("");
86         when(config.getAaiModelUrl(any())).thenReturn("");
87
88         OperationResult operationResult = mock(OperationResult.class);
89         when(aaiClient.getResource(any(), any(), any())).thenReturn(operationResult);
90         when(operationResult.getResultCode()).thenReturn(Response.Status.OK.getStatusCode());
91
92         List<Artifact> artifacts = new ArrayList<>();
93         Artifact artifact = new ModelArtifact();
94         artifacts.add(artifact);
95
96         ModelArtifactHandler handler = new ModelArtifactHandler(config);
97         boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
98         assertTrue(pushed);
99         handler.rollback(artifacts, "", aaiClient);
100     }
101
102     @Test
103     public void testPushNewModelsWithRollback() {
104         when(config.getAaiBaseUrl()).thenReturn("");
105         when(config.getAaiModelUrl(any())).thenReturn("");
106         when(config.getAaiNamedQueryUrl(any())).thenReturn("");
107
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());
111
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());
115
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);
122
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);
128     }
129
130     @Test
131     public void testPushNewModelsBadRequest() {
132         when(config.getAaiBaseUrl()).thenReturn("");
133         when(config.getAaiModelUrl(any())).thenReturn("");
134         when(config.getAaiNamedQueryUrl(any())).thenReturn("");
135
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());
139
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());
143
144         checkRollback(Collections.singletonList(new ModelArtifact()));
145     }
146
147     @Test
148     public void testBadRequestResourceModelResult() {
149         when(config.getAaiBaseUrl()).thenReturn("");
150         when(config.getAaiModelUrl(any())).thenReturn("");
151
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());
155
156         checkRollback(Collections.singletonList(new ModelArtifact()));
157     }
158
159     @Test
160     public void testNullResourceModelResult() {
161         when(config.getAaiBaseUrl()).thenReturn("");
162         when(config.getAaiModelUrl(any())).thenReturn("");
163         when(aaiClient.getResource(any(), any(), any())).thenReturn(null);
164
165         checkRollback(Collections.singletonList(new ModelArtifact()));
166     }
167
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);
173     }
174 }
175