Merge "Implement validation and hierarchical get"
[policy/models.git] / models-interactions / model-actors / actor.test / src / main / java / org / onap / policy / controlloop / actor / test / BasicHttpOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.test;
22
23 import static org.mockito.Mockito.when;
24
25 import java.util.Map;
26 import java.util.TreeMap;
27 import java.util.UUID;
28 import java.util.concurrent.CompletableFuture;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.client.InvocationCallback;
31 import javax.ws.rs.core.Response;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Captor;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.policy.common.endpoints.http.client.HttpClient;
37 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
38 import org.onap.policy.controlloop.VirtualControlLoopEvent;
39 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
41 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
42 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
43 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
44
45 /**
46  * Superclass for various operator tests.
47  *
48  * @param <Q> request type
49  */
50 public class BasicHttpOperation<Q> {
51     protected static final UUID REQ_ID = UUID.randomUUID();
52     protected static final String DEFAULT_ACTOR = "default-actor";
53     protected static final String DEFAULT_OPERATION = "default-operation";
54     protected static final String MY_CLIENT = "my-client";
55     protected static final String BASE_URI = "/base-uri";
56     protected static final String PATH = "/my-path";
57     protected static final String TARGET_ENTITY = "my-target";
58
59     protected final String actorName;
60     protected final String operationName;
61
62     @Captor
63     protected ArgumentCaptor<InvocationCallback<Response>> callbackCaptor;
64
65     @Captor
66     protected ArgumentCaptor<Entity<Q>> requestCaptor;
67
68     @Captor
69     protected ArgumentCaptor<Map<String, Object>> headerCaptor;
70
71     @Mock
72     protected ActorService service;
73
74     @Mock
75     protected HttpClient client;
76
77     @Mock
78     protected HttpClientFactory factory;
79
80     @Mock
81     protected Response rawResponse;
82
83     @Mock
84     protected HttpOperator operator;
85
86     protected CompletableFuture<Response> future;
87     protected ControlLoopOperationParams params;
88     protected Map<String, String> enrichment;
89     protected VirtualControlLoopEvent event;
90     protected ControlLoopEventContext context;
91     protected OperationOutcome outcome;
92
93     /**
94      * Constructs the object using a default actor and operation name.
95      */
96     public BasicHttpOperation() {
97         this.actorName = DEFAULT_ACTOR;
98         this.operationName = DEFAULT_OPERATION;
99     }
100
101     /**
102      * Constructs the object.
103      *
104      * @param actor actor name
105      * @param operation operation name
106      */
107     public BasicHttpOperation(String actor, String operation) {
108         this.actorName = actor;
109         this.operationName = operation;
110     }
111
112     /**
113      * Initializes mocks and sets up.
114      */
115     public void setUp() throws Exception {
116         MockitoAnnotations.initMocks(this);
117
118         when(factory.get(MY_CLIENT)).thenReturn(client);
119
120         when(rawResponse.getStatus()).thenReturn(200);
121
122         future = new CompletableFuture<>();
123         when(client.getBaseUrl()).thenReturn(BASE_URI);
124
125         makeContext();
126
127         outcome = params.makeOutcome();
128
129         initOperator();
130     }
131
132     /**
133      * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
134      * {@link #params}.
135      */
136     protected void makeContext() {
137         enrichment = new TreeMap<>(makeEnrichment());
138
139         event = new VirtualControlLoopEvent();
140         event.setRequestId(REQ_ID);
141         event.setAai(enrichment);
142
143         context = new ControlLoopEventContext(event);
144
145         params = ControlLoopOperationParams.builder().context(context).actorService(service).actor(actorName)
146                         .operation(operationName).targetEntity(TARGET_ENTITY).build();
147     }
148
149     /**
150      * Initializes an operator so that it is "alive" and has the given names.
151      */
152     protected void initOperator() {
153         when(operator.isAlive()).thenReturn(true);
154         when(operator.getFullName()).thenReturn(actorName + "." + operationName);
155         when(operator.getActorName()).thenReturn(actorName);
156         when(operator.getName()).thenReturn(operationName);
157         when(operator.getClient()).thenReturn(client);
158         when(operator.getPath()).thenReturn(PATH);
159     }
160
161     /**
162      * Makes enrichment data.
163      *
164      * @return enrichment data
165      */
166     protected Map<String, String> makeEnrichment() {
167         return new TreeMap<>();
168     }
169 }