Skip to content

Commit

Permalink
Add common methods to DeliveryState.
Browse files Browse the repository at this point in the history
  • Loading branch information
conniey committed Jun 7, 2021
1 parent 5c07227 commit ba29460
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.azure.core.util.ExpandableStringEnum;

import java.util.Collection;

/**
* States for a message delivery.
*
Expand Down Expand Up @@ -38,4 +40,24 @@ public final class DeliveryState extends ExpandableStringEnum<DeliveryState> {
* Indicates that this delivery is part of a transaction.
*/
public static final DeliveryState TRANSACTIONAL = fromString("TRANSACTIONAL", DeliveryState.class);

/**
* Gets the corresponding delivery state from its string representation.
*
* @param name The delivery state to convert.
*
* @return The corresponding delivery state.
*/
public static DeliveryState fromString(String name) {
return fromString(name, DeliveryState.class);
}

/**
* Gets all the current delivery states.
*
* @return Gets the current delivery states.
*/
public static Collection<DeliveryState> values() {
return values(DeliveryState.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.amqp.models;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Collection;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests {@link DeliveryState}
*/
public class DeliveryStateTest {
/**
* Tests that all the values are available.
*/
@Test
public void values() {
// Arrange
final DeliveryState[] expected = new DeliveryState[] {
DeliveryState.ACCEPTED, DeliveryState.MODIFIED, DeliveryState.RECEIVED, DeliveryState.REJECTED,
DeliveryState.RELEASED, DeliveryState.TRANSACTIONAL
};

// Act
final Collection<DeliveryState> actual = DeliveryState.values();

// Assert
for (DeliveryState state : expected) {
assertTrue(actual.contains(state));
}
}

/**
* Arguments for fromString.
* @return Test arguments.
*/
public static Stream<String> fromString() {
return Stream.of("MODIFIED", "FOO-BAR-NEW");
}

/**
* Tests that we can get the corresponding value and a new one if it does not exist.
*
* @param deliveryState Delivery states to test.
*/
@MethodSource
@ParameterizedTest
public void fromString(String deliveryState) {
// Act
final DeliveryState state = DeliveryState.fromString(deliveryState);

// Assert
assertNotNull(state);
assertEquals(deliveryState, state.toString());
}
}

0 comments on commit ba29460

Please sign in to comment.