Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/
package org.zowe.apiml.client.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.zowe.apiml.client.model.Registered;
import org.zowe.apiml.client.service.ApiMediationClientService;
import org.zowe.apiml.exception.ServiceDefinitionException;

@RestController
@RequestMapping("/api/v1/apiMediationClient")
@Api(
value = "/api/v1/apiMediationClient",
tags = {"API Mediation Client test call"}
)
public class ApiMediationClientTestController {
private final ApiMediationClientService apiMediationClientService;

public ApiMediationClientTestController(ApiMediationClientService apiMediationClientService) {
this.apiMediationClientService = apiMediationClientService;
}

@PostMapping
@ApiOperation(value = "Forward registration to discovery service via API mediation client")
public ResponseEntity<String> forwardRegistration() {
try {
apiMediationClientService.register();
return ResponseEntity.ok().build();
} catch (ServiceDefinitionException e) {
return ResponseEntity.status(500).body(e.getMessage());
}
}

@DeleteMapping
@ApiOperation(value = "Forward un-registration to discovery service via API mediation client")
public ResponseEntity<String> forwardUnRegistration() {
apiMediationClientService.unregister();
return ResponseEntity.ok().build();
}

@GetMapping
@ApiOperation(value = "Indicate if registration with discovery service via API mediation client was successful")
public Registered isRegistered() {
boolean isRegistered = apiMediationClientService.isRegistered();
return new Registered(isRegistered);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/
package org.zowe.apiml.client.model;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Data
@Configuration
public class DiscoverableClientConfig {
@Value("${apiml.service.discoveryServiceUrls}")
private List<String> discoveryServiceUrls;

@Value("${apiml.service.scheme}")
private String scheme;

@Value("${apiml.service.hostname}")
private String hostname;

@Value("${apiml.service.catalog.tile.id}")
private String catalogId;

@Value("${apiml.service.ssl.enabled:true}")
private boolean sslEnabled;

@Value("${apiml.service.ssl.verifySslCertificatesOfServices:false}")
private boolean verifyCerts;

@Value("${server.ssl.protocol}")
private String sslProtocol;

@Value("${server.ssl.keyStoreType}")
private String keyStoreType;

@Value("${server.ssl.trustStoreType}")
private String trustStoreType;

@Value("${server.ssl.keyAlias}")
private String keyAlias;

@Value("${server.ssl.keyPassword}")
private String keyPassword;

@Value("${server.ssl.keyStore}")
private String keyStore;

@Value("${server.ssl.keyStorePassword}")
private String keyStorePassword;

@Value("${server.ssl.trustStore}")
private String trustStore;

@Value("${server.ssl.trustStorePassword}")
private String trustStorePassword;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/
package org.zowe.apiml.client.model;

import com.fasterxml.jackson.annotation.JsonTypeName;

@JsonTypeName("Registered")
public class Registered {
private final boolean isRegistered;

public Registered(boolean isRegistered) {
this.isRegistered = isRegistered;
}

public boolean getIsRegistered() {
return isRegistered;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/
package org.zowe.apiml.client.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zowe.apiml.client.model.DiscoverableClientConfig;
import org.zowe.apiml.config.ApiInfo;
import org.zowe.apiml.eurekaservice.client.ApiMediationClient;
import org.zowe.apiml.eurekaservice.client.config.ApiMediationServiceConfig;
import org.zowe.apiml.eurekaservice.client.config.Authentication;
import org.zowe.apiml.eurekaservice.client.config.Route;
import org.zowe.apiml.eurekaservice.client.config.Ssl;
import org.zowe.apiml.eurekaservice.client.impl.ApiMediationClientImpl;
import org.zowe.apiml.exception.ServiceDefinitionException;

import java.util.Collections;

/**
* Service that allows a new {@link com.netflix.discovery.EurekaClient} to be registered and un-registered via ApiMediationClientImpl instance.
* This service uses its own {@link com.netflix.discovery.EurekaClient} so that registration can be tested without affecting other services in
* the Discoverable Client.
*/
@Service
public class ApiMediationClientService {
private static final String PORT = "10013";
private static final String SERVICE_ID = "registrationTest";
private static final String GATEWAY_URL = "api/v1";

private final DiscoverableClientConfig dcConfig;

private final ApiMediationClient apiMediationClient;

public ApiMediationClientService(@Autowired DiscoverableClientConfig dcConfig) {
apiMediationClient = new ApiMediationClientImpl();
this.dcConfig = dcConfig;
}

public boolean register() throws ServiceDefinitionException {
ApiInfo apiInfo = new ApiInfo(SERVICE_ID, GATEWAY_URL, "1.0.0", null, null);
Authentication authentication = new Authentication("bypass", null);
Ssl ssl = new Ssl(dcConfig.isSslEnabled(), dcConfig.isVerifyCerts(), dcConfig.getSslProtocol(), dcConfig.getKeyAlias(),
dcConfig.getKeyPassword().toCharArray(), dcConfig.getKeyStore(), dcConfig.getKeyStorePassword().toCharArray(),
dcConfig.getKeyStoreType(), dcConfig.getTrustStore(), dcConfig.getTrustStorePassword().toCharArray(), dcConfig.getTrustStoreType());
Route apiRoute = new Route(GATEWAY_URL, "/" + SERVICE_ID + "/" + GATEWAY_URL);

ApiMediationServiceConfig apiConfig = ApiMediationServiceConfig.builder()
.apiInfo(Collections.singletonList(apiInfo))
.authentication(authentication)
.routes(Collections.singletonList(apiRoute))
.description("Example for API Mediation Client registration")
.title("API Mediation Client Registration")
.serviceId(SERVICE_ID)
.baseUrl(dcConfig.getScheme() + "://" + dcConfig.getHostname() + ":" + PORT)
.healthCheckRelativeUrl("")
.homePageRelativeUrl("")
.statusPageRelativeUrl("")
.discoveryServiceUrls(dcConfig.getDiscoveryServiceUrls())
.ssl(ssl)
.preferIpAddress(false)
.serviceIpAddress("0.0.0.0") //use hostname instead of IP address
.build();
apiMediationClient.register(apiConfig);
return true; // indicates success, successful unless exception thrown. Used to assert success in unit tests.
}

public boolean unregister() {
apiMediationClient.unregister();
return true; // indicates success, successful unless exception thrown. Used to assert success in unit tests.
}

public boolean isRegistered() {
return apiMediationClient.isRegistered();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/
package org.zowe.apiml.client.api;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.zowe.apiml.client.service.ApiMediationClientService;

import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.core.Is.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = {ApiMediationClientTestController.class})
public class ApiMediationClientTestControllerTest {
private static final String MEDIATION_CLIENT_URI = "/api/v1/apiMediationClient";

@Autowired
private MockMvc mockMvc;

@MockBean
private ApiMediationClientService apiMediationClientService;

@Test
public void registrationTest_successful() throws Exception {
this.mockMvc.perform(
post(MEDIATION_CLIENT_URI))
.andExpect(status().isOk());
}

@Test
public void unregisterTest_successful() throws Exception {
apiMediationClientService.register();
this.mockMvc.perform(
delete(MEDIATION_CLIENT_URI))
.andExpect(status().isOk());
}

@Test
public void isRegisteredTest_notRegistered() throws Exception {
this.mockMvc.perform(
get(MEDIATION_CLIENT_URI))
.andExpect(status().isOk())
.andExpect(jsonPath("$.isRegistered", is(false)));
}

@Test
public void isRegisteredTestService_notRegistered() {
assertFalse(apiMediationClientService.isRegistered());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/
package org.zowe.apiml.client.service;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.zowe.apiml.client.model.DiscoverableClientConfig;
import org.zowe.apiml.exception.ServiceDefinitionException;

import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;

@RunWith(SpringRunner.class)
@TestPropertySource(locations = "/application.yml")
@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
@Import(ApiMediationClientServiceTest.TestConfig.class)
public class ApiMediationClientServiceTest {

private ApiMediationClientService apiMediationClientService;

@Autowired
private DiscoverableClientConfig discoverableClientConfig;

@Rule
public final ExpectedException exceptionRule = ExpectedException.none();

@Before
public void setup() {
apiMediationClientService = new ApiMediationClientService(discoverableClientConfig);
}

@Test
public void registerTest() throws ServiceDefinitionException {
assertTrue(apiMediationClientService.register());
}

@Test
public void registerTest_duplicate() throws ServiceDefinitionException {
exceptionRule.expect(ServiceDefinitionException.class);
apiMediationClientService.register();
apiMediationClientService.register();
}

@Test
public void isRegisteredTest_notRegistered() {
assertFalse(apiMediationClientService.isRegistered());
}

@Test
public void unregisterTest() {
assertTrue(apiMediationClientService.unregister());
}

@Test
public void unregisterTest_notRegistered() {
assertTrue(apiMediationClientService.unregister());
}

@Configuration
public static class TestConfig {
@Bean
public DiscoverableClientConfig discoverableClientConfig() {
return new DiscoverableClientConfig();
}
}
}
Loading