Convert project from AJSC to Spring Boot
[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.junit.Assert.assertThat;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import javax.ws.rs.core.Response;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.aai.modelloader.config.ModelLoaderConfig;
38 import org.onap.aai.modelloader.entity.Artifact;
39 import org.onap.aai.modelloader.restclient.AaiRestClient;
40 import org.onap.aai.restclient.client.OperationResult;
41
42 /**
43  * Test the Model Artifact Handler using Mocks
44  *
45  */
46 public class TestModelArtifactHandler {
47
48     @Mock
49     private ModelLoaderConfig config;
50
51     @Mock
52     private AaiRestClient aaiClient;
53
54     @Before
55     public void setupMocks() {
56         MockitoAnnotations.initMocks(this);
57     }
58
59     @Test
60     public void testEmptyLists() {
61         ModelArtifactHandler handler = new ModelArtifactHandler(config);
62         handler.pushArtifacts(Collections.emptyList(), "", Collections.emptyList(), aaiClient);
63         handler.rollback(Collections.emptyList(), "", aaiClient);
64     }
65
66     @Test
67     public void testSingleItemList() {
68         when(config.getAaiBaseUrl()).thenReturn("");
69         when(config.getAaiModelUrl(any())).thenReturn("");
70
71         ModelArtifactHandler handler = new ModelArtifactHandler(config);
72         List<Artifact> artifacts = Collections.singletonList(new ModelArtifact());
73         handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
74         handler.rollback(Collections.emptyList(), "", aaiClient);
75     }
76
77     @Test
78     public void testPushExistingModelsWithRollback() {
79         when(config.getAaiBaseUrl()).thenReturn("");
80         when(config.getAaiModelUrl(any())).thenReturn("");
81
82         OperationResult operationResult = mock(OperationResult.class);
83         when(aaiClient.getResource(any(), any(), any())).thenReturn(operationResult);
84         when(operationResult.getResultCode()).thenReturn(Response.Status.OK.getStatusCode());
85
86         List<Artifact> artifacts = new ArrayList<>();
87         Artifact artifact = new ModelArtifact();
88         artifacts.add(artifact);
89
90         ModelArtifactHandler handler = new ModelArtifactHandler(config);
91         boolean pushed = handler.pushArtifacts(artifacts, "", Collections.emptyList(), aaiClient);
92         assertThat(pushed, is(true));
93         handler.rollback(artifacts, "", aaiClient);
94     }
95
96     @Test
97     public void testPushNewModelsWithRollback() {
98         when(config.getAaiBaseUrl()).thenReturn("");
99         when(config.getAaiModelUrl(any())).thenReturn("");
100         when(config.getAaiNamedQueryUrl(any())).thenReturn("");
101
102         OperationResult getResult = mock(OperationResult.class);
103         when(aaiClient.getResource(any(), any(), any())).thenReturn(getResult);
104         when(getResult.getResultCode()).thenReturn(Response.Status.NOT_FOUND.getStatusCode());
105
106         OperationResult putResult = mock(OperationResult.class);
107         when(aaiClient.putResource(any(), any(), any(), any())).thenReturn(putResult);
108         when(putResult.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode());
109
110         List<Artifact> artifacts = new ArrayList<>();
111         artifacts.add(new ModelArtifact());
112         NamedQueryArtifact namedQueryArtifact = new NamedQueryArtifact();
113         namedQueryArtifact.setNamedQueryUuid("fred");
114         namedQueryArtifact.setModelNamespace("http://org.onap.aai.inventory/v13");
115         artifacts.add(namedQueryArtifact);
116
117         List<Artifact> completedArtifacts = new ArrayList<>();
118         ModelArtifactHandler handler = new ModelArtifactHandler(config);
119         boolean pushed = handler.pushArtifacts(artifacts, "", completedArtifacts, aaiClient);
120         assertThat(pushed, is(true));
121         handler.rollback(artifacts, "", aaiClient);
122     }
123 }
124