1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
package org.openecomp.sdc.be.components.distribution.engine;
import com.att.nsa.apiClient.credentials.ApiCredential;
import com.att.nsa.apiClient.http.HttpException;
import com.att.nsa.cambria.client.CambriaClient.CambriaApiException;
import com.att.nsa.cambria.client.CambriaIdentityManager;
import fj.data.Either;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
import org.openecomp.sdc.common.api.ConfigurationSource;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.impl.FSConfigurationSource;
import java.io.IOException;
import java.net.MalformedURLException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
@RunWith(MockitoJUnitRunner.class)
public class CambriaHandlerTest {
@Spy
private CambriaHandler handler = new CambriaHandler();
@Mock
private CambriaIdentityManager createIdentityManager;
private ApiCredential apiCredential = new ApiCredential("apiKey", "apiSecret");
@BeforeClass
public static void beforeClass() {
String appConfigDir = "src/test/resources/config/catalog-be";
ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
new ConfigurationManager(configurationSource);
}
@Before
public void startUp() throws MalformedURLException, GeneralSecurityException {
doReturn(createIdentityManager).when(handler).buildCambriaClient(any());
}
@Test
public void testCreateUebKeys() throws HttpException, CambriaApiException, IOException {
Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString())).thenReturn(apiCredential);
Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler.createUebKeys(Arrays.asList("Myhost:1234") );
Mockito.verify(createIdentityManager).setApiCredentials(Mockito.anyString(), Mockito.anyString());
assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isLeft());
}
@Test
public void testCreateUebKeys_FAIL() throws HttpException, CambriaApiException, IOException {
Mockito.when(createIdentityManager.createApiKey(Mockito.anyString(), Mockito.anyString())).thenThrow(new CambriaApiException("Error Message"));
Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = handler.createUebKeys(Arrays.asList("Myhost:1234") );
Mockito.verify(createIdentityManager, Mockito.never()).setApiCredentials(Mockito.anyString(), Mockito.anyString());
assertTrue("Unexpected Operational Status", eitherCreateUebKeys.isRight());
CambriaErrorResponse response = eitherCreateUebKeys.right().value();
assertEquals("Unexpected Operational Status", CambriaOperationStatus.CONNNECTION_ERROR, response.getOperationStatus());
assertEquals("Unexpected HTTP Code", 500, response.getHttpCode().intValue());
}
}
|