Skip to content

Instantly share code, notes, and snippets.

@jmelchio
Last active October 29, 2018 00:46
Show Gist options
  • Save jmelchio/e203803336abcca2dadbde57b67d9d47 to your computer and use it in GitHub Desktop.
Save jmelchio/e203803336abcca2dadbde57b67d9d47 to your computer and use it in GitHub Desktop.
Sample for testing in project. Not complete, needs some mocks to be useful.
/*
* Copyright 2018 Pivotal, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.clouddriver.cloudfoundry.deploy.converters;
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.CloudFoundryClient;
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.Organizations;
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.Spaces;
import com.netflix.spinnaker.clouddriver.cloudfoundry.security.CloudFoundryCredentials;
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import static org.mockito.Mockito.mock;
@Profile("test")
@Configuration
public class ConverterTestConfiguration {
@Bean
@Primary
public AccountCredentialsProvider credentialsProvider() {
return mock(AccountCredentialsProvider.class);
}
@Bean
@Primary
public CloudFoundryCredentials credentials() {
return mock(CloudFoundryCredentials.class);
}
@Bean
@Primary
public CloudFoundryClient client() {
return mock(CloudFoundryClient.class);
}
@Bean
@Primary
public Organizations organizations() {
return mock(Organizations.class);
}
@Bean
@Primary
public Spaces spaces() {
return mock(Spaces.class);
}
}
/*
* Copyright 2018 Pivotal, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.clouddriver.cloudfoundry.deploy.converters;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.CloudFoundryClient;
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.Organizations;
import com.netflix.spinnaker.clouddriver.cloudfoundry.client.Spaces;
import com.netflix.spinnaker.clouddriver.cloudfoundry.deploy.description.DeployCloudFoundryServiceDescription;
import com.netflix.spinnaker.clouddriver.cloudfoundry.model.CloudFoundryOrganization;
import com.netflix.spinnaker.clouddriver.cloudfoundry.model.CloudFoundrySpace;
import com.netflix.spinnaker.clouddriver.cloudfoundry.security.CloudFoundryCredentials;
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(classes =
{
DeployCloudFoundryServiceAtomicOperationConverter.class
, ObjectMapper.class
, ConverterTestConfiguration.class
}
)
public class DeployCloudFoundryServiceAtomicOperationConverterTest {
@Autowired
private DeployCloudFoundryServiceAtomicOperationConverter deployCloudFoundryServiceAtomicOperationConverter;
@Autowired
private AccountCredentialsProvider credentialsProvider;
@Autowired
private CloudFoundryCredentials credentials;
@Autowired
private CloudFoundryClient client;
@Autowired
private Organizations organizations;
@Autowired
private Spaces spaces;
@Test
public void contextLoaded() {
assertNotNull(deployCloudFoundryServiceAtomicOperationConverter);
}
@Test
public void convertDescriptionWithoutParameters() {
when(credentialsProvider.getCredentials(anyString())).thenReturn(credentials);
when(credentials.getClient()).thenReturn(client);
when(client.getOrganizations()).thenReturn(organizations);
when(client.getSpaces()).thenReturn(spaces);
Optional<CloudFoundryOrganization> org = Optional.of(CloudFoundryOrganization.builder().id("org-id").name("actuarial").build());
when(organizations.findByName(anyString())).thenReturn(org);
CloudFoundrySpace space = CloudFoundrySpace.builder().id("space-id").name("prod").organization(org.get()).build();
when(spaces.findByName("org-id", "prod")).thenReturn(space);
DeployCloudFoundryServiceDescription expected = new DeployCloudFoundryServiceDescription();
expected.setService("service-name");
expected.setServicePlan("service-plan");
expected.setTags(null);
expected.setParameterMap(null);
Map<String, Object> inputMap = new HashMap<>();
inputMap.put("service", "service-name");
inputMap.put("servicePlan", "service-plan");
inputMap.put("credentials", "calabasas");
inputMap.put("region", "actuarial > prod");
DeployCloudFoundryServiceDescription output = deployCloudFoundryServiceAtomicOperationConverter.convertDescription(inputMap);
assertEquals(expected.getService(), output.getService());
assertEquals(expected.getServicePlan(), output.getServicePlan());
assertEquals(expected.getTags(), output.getTags());
assertEquals(expected.getParameterMap(), output.getParameterMap());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment