Skip to content

Commit

Permalink
Create only if it doesn't exits
Browse files Browse the repository at this point in the history
  • Loading branch information
drielenr committed Mar 12, 2024
1 parent da41a0e commit 2e3f6cc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,34 @@
import com.azure.developer.devcenter.models.DevCenterOperationDetails;
import com.azure.developer.devcenter.models.RemoteConnection;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Assertions;

class DevBoxTests extends DevCenterClientTestBase {
@Test
public void testCreateDevBox() {
createDevBox();
SyncPoller<DevCenterOperationDetails, DevBox> devBoxCreateResponse =
devBoxesClient.beginCreateDevBox(projectName, meUserId, new DevBox(devBoxName, poolName));
Assertions.assertEquals(
LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, devBoxCreateResponse.waitForCompletion().getStatus());
Assertions.assertEquals(devBoxName, devBoxCreateResponse.getFinalResult().getName());
}

@Test
public void testGetDevBox() {
createDevBox();
setupDevBox();

DevBox devBox = devBoxesClient.getDevBox(projectName, meUserId, devBoxName);
Assertions.assertEquals(devBoxName, devBox.getName());
}

@Test
public void testListDevBoxes() {
createDevBox();
setupDevBox();

List<DevBox> devBoxes = devBoxesClient.listDevBoxes(projectName, meUserId).stream().collect(Collectors.toList());
Assertions.assertEquals(1, devBoxes.size());
Expand All @@ -49,7 +52,7 @@ public void testListDevBoxes() {

@Test
public void testListAllDevBoxesByUser() {
createDevBox();
setupDevBox();

List<DevBox> devBoxes = devBoxesClient.listAllDevBoxesByUser(meUserId).stream().collect(Collectors.toList());
Assertions.assertEquals(1, devBoxes.size());
Expand All @@ -58,7 +61,7 @@ public void testListAllDevBoxesByUser() {

@Test
public void testListAllDevBoxes() {
createDevBox();
setupDevBox();

List<DevBox> devBoxes = devBoxesClient.listAllDevBoxes().stream().collect(Collectors.toList());
Assertions.assertEquals(1, devBoxes.size());
Expand All @@ -67,7 +70,7 @@ public void testListAllDevBoxes() {

@Test
public void testGetRemoteConnection() {
createDevBox();
setupDevBox();

RemoteConnection remoteConnection = devBoxesClient.getRemoteConnection(projectName, meUserId, devBoxName);
Assertions.assertNotNull(remoteConnection.getWebUrl());
Expand All @@ -76,7 +79,7 @@ public void testGetRemoteConnection() {

@Test
public void testStartAndStopDevBox() {
createDevBox();
setupDevBox();

SyncPoller<DevCenterOperationDetails, Void> devBoxStopResponse =
devBoxesClient.beginStopDevBox(projectName, meUserId, devBoxName);
Expand All @@ -91,7 +94,7 @@ public void testStartAndStopDevBox() {

@Test
public void testRestartDevBox() {
createDevBox();
setupDevBox();

SyncPoller<DevCenterOperationDetails, Void> devBoxRestartResponse =
devBoxesClient.beginRestartDevBox(projectName, meUserId, devBoxName);
Expand Down Expand Up @@ -127,7 +130,7 @@ public void testListSchedules() {

@Test
public void testGetAndDelayDevBoxAction() {
createDevBox();
setupDevBox();

DevBoxAction action = devBoxesClient.getDevBoxAction(projectName, meUserId, devBoxName, "schedule-default");
Assertions.assertEquals("schedule-default", action.getName());
Expand All @@ -143,7 +146,7 @@ public void testGetAndDelayDevBoxAction() {

@Test
public void testGetAndDelayAllDevBoxActions() {
createDevBox();
setupDevBox();

List<DevBoxAction> devBoxActions = devBoxesClient.listDevBoxActions(projectName, meUserId, devBoxName).stream().collect(Collectors.toList());
DevBoxAction action = devBoxActions.get(0);
Expand All @@ -164,7 +167,7 @@ public void testGetAndDelayAllDevBoxActions() {

@Test
public void testSkipActionAndDeleteDevBox() {
createDevBox();
setupDevBox();

devBoxesClient.skipAction(projectName, meUserId, devBoxName, "schedule-default");

Expand All @@ -174,13 +177,21 @@ public void testSkipActionAndDeleteDevBox() {
LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, devBoxDeleteResponse.waitForCompletion().getStatus());
}

public DevBox createDevBox() {
SyncPoller<DevCenterOperationDetails, DevBox> devBoxCreateResponse =
devBoxesClient.beginCreateDevBox(projectName, meUserId, new DevBox(devBoxName, poolName));
Assertions.assertEquals(
LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, devBoxCreateResponse.waitForCompletion().getStatus());

DevBox devBox = devBoxCreateResponse.getFinalResult();
public DevBox setupDevBox() {
//get dev box if exists. If not, NotFound error will be thrown, and then we create dev box

DevBox devBox;
try {
devBox = devBoxesClient.getDevBox(projectName, meUserId, devBoxName);
} catch (Exception e) {
SyncPoller<DevCenterOperationDetails, DevBox> devBoxCreateResponse =
devBoxesClient.beginCreateDevBox(projectName, meUserId, new DevBox(devBoxName, poolName));
Assertions.assertEquals(
LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, devBoxCreateResponse.waitForCompletion().getStatus());

devBox = devBoxCreateResponse.getFinalResult();
}
Assertions.assertNotNull(devBox);
Assertions.assertEquals(devBoxName, devBox.getName());

return devBox;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,35 @@
import com.azure.developer.devcenter.models.EnvironmentDefinition;
import com.azure.developer.devcenter.models.DevCenterCatalog;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Assertions;

class EnvironmentTests extends DevCenterClientTestBase {
@Test
public void testCreateEnvironment() {
createEnvironment();
DevCenterEnvironment environment = new DevCenterEnvironment(devEnvironmentName, envTypeName, catalogName, envDefinitionName);

SyncPoller<DevCenterOperationDetails, DevCenterEnvironment> environmentCreateResponse =
deploymentEnvironmentsClient.beginCreateOrUpdateEnvironment(projectName, meUserId, environment);
Assertions.assertEquals(
LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, environmentCreateResponse.waitForCompletion().getStatus());
Assertions.assertEquals(devEnvironmentName, environmentCreateResponse.getFinalResult().getName());
}

@Test
public void testGetEnvironment() {
createEnvironment();
setupEnvironment();

DevCenterEnvironment environment = deploymentEnvironmentsClient.getEnvironment(projectName, meUserId, devEnvironmentName);
Assertions.assertEquals(devEnvironmentName, environment.getName());
}

@Test
public void testListEnvironments() {
createEnvironment();
setupEnvironment();

List<DevCenterEnvironment> environments = deploymentEnvironmentsClient.listEnvironments(projectName, meUserId).stream().collect(Collectors.toList());
Assertions.assertEquals(1, environments.size());
Expand All @@ -44,7 +49,7 @@ public void testListEnvironments() {

@Test
public void testListAllEnvironments() {
createEnvironment();
setupEnvironment();

List<DevCenterEnvironment> environments = deploymentEnvironmentsClient.listAllEnvironments(projectName).stream().collect(Collectors.toList());
Assertions.assertEquals(1, environments.size());
Expand All @@ -53,7 +58,7 @@ public void testListAllEnvironments() {

@Test
public void testDeleteEnvironment() {
createEnvironment();
setupEnvironment();

SyncPoller<DevCenterOperationDetails, Void> environmentDeleteResponse =
deploymentEnvironmentsClient.beginDeleteEnvironment(projectName, meUserId, devEnvironmentName);
Expand Down Expand Up @@ -108,15 +113,23 @@ public void testListEnvironmentTypes() {
Assertions.assertEquals(envTypeName, envTypes.get(0).getName());
}

public DevCenterEnvironment createEnvironment() {
DevCenterEnvironment createEnvironment = new DevCenterEnvironment(devEnvironmentName, envTypeName, catalogName, envDefinitionName);

SyncPoller<DevCenterOperationDetails, DevCenterEnvironment> environmentCreateResponse =
deploymentEnvironmentsClient.beginCreateOrUpdateEnvironment(projectName, meUserId, createEnvironment);
Assertions.assertEquals(
LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, environmentCreateResponse.waitForCompletion().getStatus());

DevCenterEnvironment environment = environmentCreateResponse.getFinalResult();
public DevCenterEnvironment setupEnvironment() {
//get environment if exists. If not, NotFound error will be thrown, and then we create environment

DevCenterEnvironment environment;
try {
environment = deploymentEnvironmentsClient.getEnvironment(projectName, meUserId, devEnvironmentName);
} catch (Exception e) {
DevCenterEnvironment createEnvironment = new DevCenterEnvironment(devEnvironmentName, envTypeName, catalogName, envDefinitionName);

SyncPoller<DevCenterOperationDetails, DevCenterEnvironment> environmentCreateResponse =
deploymentEnvironmentsClient.beginCreateOrUpdateEnvironment(projectName, meUserId, createEnvironment);
Assertions.assertEquals(
LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, environmentCreateResponse.waitForCompletion().getStatus());

environment = environmentCreateResponse.getFinalResult();
}
Assertions.assertNotNull(environment);
Assertions.assertEquals(devEnvironmentName, environment.getName());

return environment;
Expand Down

0 comments on commit 2e3f6cc

Please sign in to comment.