990ea4f99bdb48f42826c124e0a9595a278c9042
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.be.components.distribution.engine;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26
27 import java.util.List;
28
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.openecomp.sdc.be.components.BaseConfDependentTest;
33 import org.openecomp.sdc.be.config.ConfigurationManager;
34 import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
35
36 import com.google.gson.Gson;
37 import com.google.gson.GsonBuilder;
38
39 public class DistributionEngineHealthCheckTest extends BaseConfDependentTest {
40
41         @Mock
42         private CambriaHandler cambriaHandler = Mockito.mock(CambriaHandler.class);
43
44         DistributionEngineClusterHealth distributionEngineClusterHealth = new DistributionEngineClusterHealth();
45
46         Gson gson = new Gson();
47
48         Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
49
50         //
51         // @Test
52         // public void validateDownWhenEnvAreDown() {
53         //
54         // Map<String, AtomicBoolean> envNamePerStatus = new HashMap<>();
55         // envNamePerStatus.put("PROD1", new AtomicBoolean(false));
56         // envNamePerStatus.put("PROD2", new AtomicBoolean(false));
57         //
58         // distributionEngineClusterHealth.startHealthCheckTask(envNamePerStatus);
59         //
60         // HealthCheckInfo healthCheckInfo =
61         // distributionEngineClusterHealth.getHealthCheckInfo();
62         // assertEquals("verify down", HealthCheckStatus.DOWN,
63         // healthCheckInfo.getHealthCheckStatus());
64         // assertEquals("verify DE component", HealthCheckComponent.DE,
65         // healthCheckInfo.getHealthCheckComponent());
66         //
67         // }
68
69         @Test
70         public void validateUpWhenQuerySucceed() {
71
72                 // Map<String, AtomicBoolean> envNamePerStatus = new HashMap<>();
73                 // envNamePerStatus.put("PROD1", new AtomicBoolean(true));
74                 // envNamePerStatus.put("PROD2", new AtomicBoolean(false));
75                 //
76                 // distributionEngineClusterHealth.startHealthCheckTask(envNamePerStatus,
77                 // false);
78
79                 CambriaErrorResponse cambriaOkResponse = new CambriaErrorResponse(CambriaOperationStatus.OK, 200);
80                 CambriaErrorResponse cambriaErrorResponse = new CambriaErrorResponse(CambriaOperationStatus.INTERNAL_SERVER_ERROR, 500);
81                 CambriaErrorResponse cambriaNotErrorResponse = new CambriaErrorResponse(CambriaOperationStatus.AUTHENTICATION_ERROR, 403);
82
83                 List<String> uebServers = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getUebServers();
84                 if (uebServers.size() >= 2) {
85                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(0)), Mockito.any(String.class))).thenReturn(cambriaOkResponse);
86                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(1)), Mockito.any(String.class))).thenReturn(cambriaOkResponse);
87                 }
88
89                 UebHealthCheckCall healthCheckCall1 = new UebHealthCheckCall(uebServers.get(0), "publicKey");
90                 healthCheckCall1.setCambriaHandler(cambriaHandler);
91                 Boolean call1 = healthCheckCall1.call();
92                 assertTrue("check response okay", call1);
93
94                 UebHealthCheckCall healthCheckCall2 = new UebHealthCheckCall(uebServers.get(1), "publicKey");
95                 healthCheckCall2.setCambriaHandler(cambriaHandler);
96
97                 Boolean call2 = healthCheckCall2.call();
98                 assertTrue("check response okay", call2);
99
100                 if (uebServers.size() >= 2) {
101                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(0)), Mockito.any(String.class))).thenReturn(cambriaErrorResponse);
102                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(1)), Mockito.any(String.class))).thenReturn(cambriaOkResponse);
103                 }
104                 healthCheckCall1 = new UebHealthCheckCall(uebServers.get(0), "publicKey");
105                 healthCheckCall1.setCambriaHandler(cambriaHandler);
106
107                 call1 = healthCheckCall1.call();
108                 assertFalse("check response okay", call1);
109
110                 healthCheckCall2 = new UebHealthCheckCall(uebServers.get(1), "publicKey");
111                 healthCheckCall2.setCambriaHandler(cambriaHandler);
112
113                 call2 = healthCheckCall2.call();
114                 assertTrue("check response okay", call2);
115
116                 if (uebServers.size() >= 2) {
117                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(0)), Mockito.any(String.class))).thenReturn(cambriaErrorResponse);
118                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(1)), Mockito.any(String.class))).thenReturn(cambriaNotErrorResponse);
119                 }
120                 healthCheckCall1 = new UebHealthCheckCall(uebServers.get(0), "publicKey");
121                 healthCheckCall1.setCambriaHandler(cambriaHandler);
122
123                 call1 = healthCheckCall1.call();
124                 assertFalse("check response okay", call1);
125
126                 healthCheckCall2 = new UebHealthCheckCall(uebServers.get(1), "publicKey");
127                 healthCheckCall2.setCambriaHandler(cambriaHandler);
128
129                 call2 = healthCheckCall2.call();
130                 assertTrue("check response okay", call2);
131
132         }
133
134 }