|
| 1 | +/* |
| 2 | + * Copyright 2016-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.dataflow.shell.command; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import org.springframework.cloud.dataflow.core.AppBootSchemaVersion; |
| 29 | +import org.springframework.cloud.dataflow.core.AppRegistration; |
| 30 | +import org.springframework.cloud.dataflow.core.ApplicationType; |
| 31 | +import org.springframework.cloud.dataflow.registry.service.AppRegistryService; |
| 32 | +import org.springframework.cloud.dataflow.shell.AbstractShellIntegrationTest; |
| 33 | +import org.springframework.cloud.dataflow.shell.ShellCommandRunner; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests for {@link AppRegistryCommands}. |
| 39 | + * |
| 40 | + * @author Chris Bono |
| 41 | + * @author Corneil du Plessis |
| 42 | + */ |
| 43 | +public class AppRegistryCommandsTests extends AbstractShellIntegrationTest { |
| 44 | + |
| 45 | + private static final Logger logger = LoggerFactory.getLogger(AppRegistryCommandsTests.class); |
| 46 | + |
| 47 | + private AppRegistryService registry; |
| 48 | + private ShellCommandRunner commandRunner; |
| 49 | + private List<AppRegistration> registeredApps; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void prepareForTest() { |
| 53 | + registeredApps = new ArrayList<>(); |
| 54 | + registry = applicationContext.getBean(AppRegistryService.class); |
| 55 | + commandRunner = commandRunner().withValidateCommandSuccess(); |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + public void unregisterApps() { |
| 60 | + registeredApps.forEach(this::safeDeleteAppRegistration); |
| 61 | + } |
| 62 | + |
| 63 | + private void safeDeleteAppRegistration(AppRegistration registration) { |
| 64 | + try { |
| 65 | + registry.delete(registration.getName(), registration.getType(), registration.getVersion()); |
| 66 | + } catch (Exception ex) { |
| 67 | + logger.error("Failed to delete app registration: " + registration, ex); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testRegisterTaskAppNoBootVersion() { |
| 73 | + AppRegistration registration = registerTimestampTask("timestamp", "3.2.0", "", false); |
| 74 | + assertThat(registration.getVersion()).isEqualTo("3.2.0"); |
| 75 | + assertThat(registration.getBootVersion()).isEqualTo(AppBootSchemaVersion.defaultVersion()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testRegisterTaskAppBootVersion2() { |
| 80 | + AppRegistration registration = registerTimestampTask("timestamp2", "3.2.0", "--bootVersion 2", false); |
| 81 | + assertThat(registration.getVersion()).isEqualTo("3.2.0"); |
| 82 | + assertThat(registration.getBootVersion()).isEqualTo(AppBootSchemaVersion.BOOT2); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testRegisterTaskAppBootVersion3() { |
| 87 | + AppRegistration registration = registerTimestampTask("timestamp3", "3.2.1", "-b 3", false); |
| 88 | + assertThat(registration.getVersion()).isEqualTo("3.2.1"); |
| 89 | + assertThat(registration.getBootVersion()).isEqualTo(AppBootSchemaVersion.BOOT3); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testRegisterTaskUpdateBootVersion3() { |
| 94 | + AppRegistration registration = registerTimestampTask("timestamp2to3", "3.2.0", "--bootVersion 2", false); |
| 95 | + assertThat(registration.getVersion()).isEqualTo("3.2.0"); |
| 96 | + assertThat(registration.getBootVersion()).isEqualTo(AppBootSchemaVersion.BOOT2); |
| 97 | + // The 'force=true' signals to udpate the existing 'timestamp2to3' app |
| 98 | + registration = registerTimestampTask("timestamp2to3", "3.2.1", "-b 3", true); |
| 99 | + assertThat(registration.getVersion()).isEqualTo("3.2.1"); |
| 100 | + assertThat(registration.getBootVersion()).isEqualTo(AppBootSchemaVersion.BOOT3); |
| 101 | + } |
| 102 | + |
| 103 | + private AppRegistration registerTimestampTask(String name, String timestampArtifactVersion, String bootVersionOption, boolean force) { |
| 104 | + String commandTemplate = "app register --type task --name %s %s %s --uri maven://org.springframework.cloud.task.app:task-timestamp:%s"; |
| 105 | + String command = String.format(commandTemplate, name, bootVersionOption, (force ? "--force" : ""), timestampArtifactVersion); |
| 106 | + logger.info("COMMAND -> {}", command); |
| 107 | + Object result = this.commandRunner.executeCommand(command); |
| 108 | + logger.info("RESULT <- {}", result); |
| 109 | + assertThat(registry.appExist(name, ApplicationType.task, timestampArtifactVersion)).isTrue(); |
| 110 | + AppRegistration registration = registry.find(name, ApplicationType.task, timestampArtifactVersion); |
| 111 | + registeredApps.add(registration); |
| 112 | + return registration; |
| 113 | + } |
| 114 | +} |
0 commit comments