Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests and fix revealed problems #74

Merged
merged 2 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ApiResponse push(@PathVariable Long requestId, @PathVariable Long product
Optional<RemoteProductManager> remoteProductManager = Optional.ofNullable(remoteProductManagerRepo.findOne(rpmId));
ApiResponse response;

if (internalRequest.isPresent() && product.isPresent() && remoteProductManager.isPresent()) {
if (internalRequest.isPresent() && product.isPresent() && remoteProductManager.isPresent() && !scopeId.isEmpty()) {
FeatureRequest featureRequest = new FeatureRequest(
internalRequest.get().getTitle(), internalRequest.get().getDescription(), product.get().getId(), scopeId);

Expand All @@ -101,18 +101,20 @@ public ApiResponse push(@PathVariable Long requestId, @PathVariable Long product
response = new ApiResponse(SUCCESS, remoteProductManagerBean.push(featureRequest));
internalRequestRepo.delete(internalRequest.get());
} catch (Exception e) {
response = new ApiResponse(ERROR, "Error pushing request to " + remoteProductManager.get().getName()
+ " for product " + product.get().getName() + "!");
response = new ApiResponse(ERROR, "Error pushing Internal Request to " + remoteProductManager.get().getName()
+ " for Product " + product.get().getName() + "!");
}
} else if (!remoteProductManager.isPresent()) {
response = new ApiResponse(ERROR, "Remote Product Manager with id " + rpmId + " not found!");
} else if (!internalRequest.isPresent()) {
response = new ApiResponse(ERROR, "Internal Request with id " + requestId + " not found!");
} else {
} else if (!product.isPresent()) {
response = new ApiResponse(ERROR, "Product with id " + productId + " not found!");
} else {
response = new ApiResponse(ERROR, "Internal Request is missing the scope id!");
}

return response;
}

}
}
19 changes: 11 additions & 8 deletions src/main/java/edu/tamu/app/controller/ProductController.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,19 @@ public ApiResponse getRemoteProductByScopeId(@PathVariable Long remoteProductMan
}

private void reifyProductRemoteProductManager(Product product) {
List<RemoteProductInfo> remoteProducts = product.getRemoteProducts();
for (int i = 0; i < product.getRemoteProducts().size(); i++) {
Optional<RemoteProductManager> remoteProductManager = Optional.ofNullable(remoteProducts.get(i).getRemoteProductManager());
if (remoteProductManager.isPresent()) {
Long remoteProductManagerId = remoteProductManager.get().getId();
RemoteProductInfo remoteProduct = new RemoteProductInfo(remoteProducts.get(i).getScopeId(), remoteProductManagerRepo.findOne(remoteProductManagerId));
remoteProducts.set(i, remoteProduct);
Optional<List<RemoteProductInfo>> remoteProducts = Optional.ofNullable(product.getRemoteProducts());

if (remoteProducts.isPresent()) {
for (int i = 0; i < product.getRemoteProducts().size(); i++) {
Optional<RemoteProductManager> remoteProductManager = Optional.ofNullable(remoteProducts.get().get(i).getRemoteProductManager());
if (remoteProductManager.isPresent()) {
Long remoteProductManagerId = remoteProductManager.get().getId();
RemoteProductInfo remoteProduct = new RemoteProductInfo(remoteProducts.get().get(i).getScopeId(), remoteProductManagerRepo.findOne(remoteProductManagerId));
remoteProducts.get().set(i, remoteProduct);
}
}
product.setRemoteProducts(remoteProducts.get());
}
product.setRemoteProducts(remoteProducts);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,16 @@
@RunWith(SpringRunner.class)
public class ActiveSprintsScheduledCacheServiceTest {
private static final String TEST_PRODUCT_SCOPE1 = "0010";
private static final String TEST_PRODUCT_SCOPE2 = "0011";
private static final String TEST_PRODUCT_SCOPE3 = "0020";
private static final String TEST_PRODUCT_SCOPE2 = "0020";

private static final RemoteProductManager TEST_REMOTE_PRODUCT_MANAGER1 = new RemoteProductManager("Test Remote Product Manager 1", ServiceType.VERSION_ONE, new HashMap<String, String>());
private static final RemoteProductManager TEST_REMOTE_PRODUCT_MANAGER2 = new RemoteProductManager("Test Remote Product Manager 2", ServiceType.GITHUB, new HashMap<String, String>());

private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INFO_1 = new RemoteProductInfo(TEST_PRODUCT_SCOPE1, TEST_REMOTE_PRODUCT_MANAGER1);
private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INFO_2 = new RemoteProductInfo(TEST_PRODUCT_SCOPE2, TEST_REMOTE_PRODUCT_MANAGER1);
private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INFO_3 = new RemoteProductInfo(TEST_PRODUCT_SCOPE3, TEST_REMOTE_PRODUCT_MANAGER2);
private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INFO1 = new RemoteProductInfo(TEST_PRODUCT_SCOPE1, TEST_REMOTE_PRODUCT_MANAGER1);
private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INFO2 = new RemoteProductInfo(TEST_PRODUCT_SCOPE2, TEST_REMOTE_PRODUCT_MANAGER2);

private static final List<RemoteProductInfo> TEST_PRODUCT1_REMOTE_PRODUCT_INFO_LIST1 = new ArrayList<RemoteProductInfo>(Arrays.asList(TEST_REMOTE_PRODUCT_INFO_1, TEST_REMOTE_PRODUCT_INFO_2));
private static final List<RemoteProductInfo> TEST_PRODUCT1_REMOTE_PRODUCT_INFO_LIST2 = new ArrayList<RemoteProductInfo>(Arrays.asList(TEST_REMOTE_PRODUCT_INFO_3));
private static final List<RemoteProductInfo> TEST_PRODUCT1_REMOTE_PRODUCT_INFO_LIST1 = new ArrayList<RemoteProductInfo>(Arrays.asList(TEST_REMOTE_PRODUCT_INFO1));
private static final List<RemoteProductInfo> TEST_PRODUCT1_REMOTE_PRODUCT_INFO_LIST2 = new ArrayList<RemoteProductInfo>(Arrays.asList(TEST_REMOTE_PRODUCT_INFO2));

@Mock
private ProductRepo productRepo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@

@RunWith(SpringRunner.class)
public class ProductsStatsScheduledCacheServiceTest {
private static final String TEST_PRODUCT_NAME = "Test Product";

private static final String TEST_PRODUCT_SCOPE = "0010";

private static final RemoteProductManager TEST_REMOTE_PRODUCT_MANAGER = new RemoteProductManager("Test Remote Product Manager", ServiceType.VERSION_ONE, new HashMap<String, String>());

private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INFO = new RemoteProductInfo(TEST_PRODUCT_SCOPE, TEST_REMOTE_PRODUCT_MANAGER);

private static final List<RemoteProductInfo> TEST_PRODUCT_REMOTE_PRODUCT_INFO_LIST = new ArrayList<RemoteProductInfo>(Arrays.asList(TEST_REMOTE_PRODUCT_INFO));

@Mock
private ProductRepo productRepo;
Expand Down Expand Up @@ -84,7 +93,7 @@ public void testAddProduct() {
public void testUpdateProduct() {
Product product = getMockProduct();
productsStatsScheduledCacheService.addProduct(product);
product.setScopeId("1001");
product.setName("new Name");
productsStatsScheduledCacheService.updateProduct(product);
assertTrue(true);
}
Expand Down Expand Up @@ -116,18 +125,18 @@ private List<ProductStats> getMockProductsStatsCache() {
}

private ProductStats getMockProductStats() {
return new ProductStats("1000", "Test Product", 2, 3, 10, 3);
return new ProductStats("1000", TEST_PRODUCT_NAME, 2, 3, 10, 3);
}

private RemoteProduct getMockRemoteProduct() {
return new RemoteProduct("1000", "Test Product", 2, 3, 10, 3);
return new RemoteProduct("1000", TEST_PRODUCT_NAME, 2, 3, 10, 3);
}

private void assertProductsStats(List<ProductStats> productStatsCache) {
assertFalse(productStatsCache.isEmpty());
assertEquals(1, productStatsCache.size());
assertEquals("1000", productStatsCache.get(0).getId());
assertEquals("Test Product", productStatsCache.get(0).getName());
assertEquals(TEST_PRODUCT_NAME, productStatsCache.get(0).getName());
assertEquals(2, productStatsCache.get(0).getRequestCount());
assertEquals(3, productStatsCache.get(0).getIssueCount());
assertEquals(10, productStatsCache.get(0).getFeatureCount());
Expand All @@ -136,8 +145,7 @@ private void assertProductsStats(List<ProductStats> productStatsCache) {
}

private Product getMockProduct() {
RemoteProductManager remoteProductManager = new RemoteProductManager("Test Remote Product Manager", ServiceType.VERSION_ONE);
Product mockProduct = new Product("Test Product", "0001", remoteProductManager);
Product mockProduct = new Product(TEST_PRODUCT_NAME, TEST_PRODUCT_REMOTE_PRODUCT_INFO_LIST);
mockProduct.setId(1000L);
return mockProduct;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;

import java.time.Instant;
Expand Down Expand Up @@ -51,25 +52,17 @@ public class InternalRequestControllerTest {
private static final String TEST_PRODUCT1_SCOPE1 = "0010";
private static final String TEST_PRODUCT1_SCOPE2 = "0011";
private static final String TEST_PRODUCT2_NAME = "Test Product 2 Name";
private static final String TEST_PRODUCT_WITHOUT_RPM_NAME = "Test Product Without Remote Product Manager Name";

private static final String PUSH_ERROR_MESSAGE = "Error pushing request to Test Remote Product Manager for product Test Product 1 Name!";
private static final String NO_RPM_ERROR_MESSAGE = "Test Product Without Remote Product Manager Name product does not have a Remote Product Manager!";
private static final String NO_PRODUCT_ERROR_MESSAGE = "Product with id null not found!";

private static final RemoteProductManager TEST_REMOTE_PRODUCT_MANAGER = new RemoteProductManager("Test Remote Product Manager", ServiceType.VERSION_ONE, new HashMap<String, String>());

private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INFO_1 = new RemoteProductInfo(TEST_PRODUCT1_SCOPE1, TEST_REMOTE_PRODUCT_MANAGER);
private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INFO_2 = new RemoteProductInfo(TEST_PRODUCT1_SCOPE2, TEST_REMOTE_PRODUCT_MANAGER);
private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INVALID_RPM = new RemoteProductInfo(TEST_PRODUCT1_SCOPE2, null);
private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INVALID_SCOPE = new RemoteProductInfo(null, TEST_REMOTE_PRODUCT_MANAGER);
private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INFO1 = new RemoteProductInfo(TEST_PRODUCT1_SCOPE1, TEST_REMOTE_PRODUCT_MANAGER);
private static final RemoteProductInfo TEST_REMOTE_PRODUCT_INFO2 = new RemoteProductInfo(TEST_PRODUCT1_SCOPE2, TEST_REMOTE_PRODUCT_MANAGER);

private static final List<RemoteProductInfo> TEST_PRODUCT1_REMOTE_PRODUCT_INFO_LIST = new ArrayList<RemoteProductInfo>(Arrays.asList(TEST_REMOTE_PRODUCT_INFO_1, TEST_REMOTE_PRODUCT_INFO_2)
private static final List<RemoteProductInfo> TEST_PRODUCT1_REMOTE_PRODUCT_INFO_LIST = new ArrayList<RemoteProductInfo>(Arrays.asList(TEST_REMOTE_PRODUCT_INFO1, TEST_REMOTE_PRODUCT_INFO2)
);

private static Product TEST_PRODUCT1 = new Product(TEST_PRODUCT1_NAME, TEST_PRODUCT1_REMOTE_PRODUCT_INFO_LIST);
private static Product TEST_PRODUCT2 = new Product(TEST_PRODUCT2_NAME);
private static Product TEST_PRODUCT_WIHTOUT_RPM = new Product(TEST_PRODUCT_WITHOUT_RPM_NAME);

private static FeatureRequest TEST_FEATURE_REQUEST = new FeatureRequest(TEST_REQUEST_TITLE_BELLS, TEST_REQUEST_DESCRIPTION_BELLS, TEST_PRODUCT1.getId(), TEST_PRODUCT1_SCOPE1);

Expand Down Expand Up @@ -110,7 +103,7 @@ public class InternalRequestControllerTest {
private InternalRequestController internalRequestController;

@Before
public void setup() {
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);

TEST_PRODUCT1.setId(1L);
Expand All @@ -125,7 +118,7 @@ public void setup() {
when(internalRequestRepo.count()).thenReturn((long) mockRequestsRepo.size());
when(internalRequestRepo.create(any(InternalRequest.class))).thenReturn(TEST_REQUEST_BELLS);
when(internalRequestRepo.update(any(InternalRequest.class))).thenReturn(TEST_REQUEST_BELLS);
when(internalRequestRepo.findOne(any(Long.class))).thenReturn(TEST_REQUEST_BELLS);
when(remoteProductManagementBean.push(any(FeatureRequest.class))).thenReturn(TEST_FEATURE_REQUEST);
when(managementBeanRegistry.getService(any(String.class))).thenReturn(remoteProductManagementBean);

doAnswer(new Answer<ApiResponse>() {
Expand Down Expand Up @@ -162,6 +155,8 @@ public void testRead() {

@Test
public void testReadById() {
when(internalRequestRepo.findOne(TEST_REQUEST_BELLS.getId())).thenReturn(TEST_REQUEST_BELLS);

ApiResponse apiResponse = internalRequestController.read(TEST_REQUEST_BELLS.getId());

assertEquals("Request for Internal Request was unsuccessful", SUCCESS, apiResponse.getMeta().getStatus());
Expand Down Expand Up @@ -190,48 +185,78 @@ public void testDelete() {
}

@Test
public void testPush() throws Exception {
public void testPush() {
int initialCount = mockRequestsRepo.size();

when(remoteProductManagementBean.push(any(FeatureRequest.class))).thenReturn(TEST_FEATURE_REQUEST);
when(managementBeanRegistry.getService(any(String.class))).thenReturn(remoteProductManagementBean);
when(internalRequestRepo.findOne(any(Long.class))).thenReturn(TEST_REQUEST_BELLS);
when(productRepo.findOne(any(Long.class))).thenReturn(TEST_PRODUCT1);
when(remoteProductManagerRepo.findOne(any(Long.class))).thenReturn(TEST_REMOTE_PRODUCT_MANAGER);
doNothing().when(internalRequestRepo).delete(any(Long.class));

apiResponse = internalRequestController.push(TEST_REQUEST_BELLS.getId(), TEST_PRODUCT1.getId(), TEST_REMOTE_PRODUCT_INFO_1);
apiResponse = internalRequestController.push(TEST_REQUEST_BELLS.getId(), TEST_PRODUCT1.getId(), TEST_REMOTE_PRODUCT_MANAGER.getId(), TEST_REMOTE_PRODUCT_INFO1.getScopeId());

assertEquals("Product controller did not push request", SUCCESS, apiResponse.getMeta().getStatus());
assertEquals("Internal Request controller did not push request", SUCCESS, apiResponse.getMeta().getStatus());
assertEquals("InternalRequest should be deleted after successful push", initialCount - 1, mockRequestsRepo.size());
}

@Test
public void testPushToInvalidRemoteProductManager() {
when(internalRequestRepo.findOne(any(Long.class))).thenReturn(TEST_REQUEST_BELLS);
when(productRepo.findOne(any(Long.class))).thenReturn(TEST_PRODUCT1);
when(remoteProductManagerRepo.findOne(any(Long.class))).thenReturn(null);

apiResponse = internalRequestController.push(TEST_REQUEST_BELLS.getId(), TEST_PRODUCT1.getId(), TEST_REMOTE_PRODUCT_INVALID_RPM);
apiResponse = internalRequestController.push(TEST_REQUEST_BELLS.getId(), TEST_PRODUCT1.getId(), null, TEST_REMOTE_PRODUCT_INFO1.getScopeId());

assertEquals("Invalid push did not throw an exception", ERROR, apiResponse.getMeta().getStatus());
assertEquals("Push without Remote Product Manager did not result in the expected error", PUSH_ERROR_MESSAGE, apiResponse.getMeta().getMessage());
String expectedMessage = "Remote Product Manager with id null not found!";

assertEquals("Push without Remote Product Manage did not throw an exception", ERROR, apiResponse.getMeta().getStatus());
assertEquals("Push without Remote Product Manager did not result in the expected error", expectedMessage, apiResponse.getMeta().getMessage());
assertEquals("InternalRequest should not be deleted after failed push", 2, internalRequestRepo.count());
}

@Test
public void testPushInvalidScope() {
when(productRepo.findOne(any(Long.class))).thenReturn(TEST_PRODUCT_WIHTOUT_RPM);
when(internalRequestRepo.findOne(any(Long.class))).thenReturn(TEST_REQUEST_BELLS);
when(productRepo.findOne(any(Long.class))).thenReturn(TEST_PRODUCT1);
when(remoteProductManagerRepo.findOne(any(Long.class))).thenReturn(TEST_REMOTE_PRODUCT_MANAGER);

apiResponse = internalRequestController.push(TEST_REQUEST_BELLS.getId(), TEST_PRODUCT1.getId(), TEST_REMOTE_PRODUCT_MANAGER.getId(), "");

apiResponse = internalRequestController.push(TEST_REQUEST_BELLS.getId(), TEST_PRODUCT1.getId(), TEST_REMOTE_PRODUCT_INVALID_SCOPE);
String expectedMessage = "Internal Request is missing the scope id!";

assertEquals("Push without Remote Product Manager did not result in an error", ERROR, apiResponse.getMeta().getStatus());
assertEquals("Push without Remote Product Manager did not result in the expected error", NO_RPM_ERROR_MESSAGE, apiResponse.getMeta().getMessage());
assertEquals("Push with invalid Scope did not throw an exception", ERROR, apiResponse.getMeta().getStatus());
assertEquals("Push with invalid Scope did not result in the expected error", expectedMessage, apiResponse.getMeta().getMessage());
assertEquals("InternalRequest should not be deleted after failed push", 2, internalRequestRepo.count());
}

@Test
public void testPushInvalidProduct() {
apiResponse = internalRequestController.push(TEST_REQUEST_BELLS.getId(), 0L, TEST_REMOTE_PRODUCT_INFO_1);
when(internalRequestRepo.findOne(any(Long.class))).thenReturn(TEST_REQUEST_BELLS);
when(productRepo.findOne(any(Long.class))).thenReturn(null);
when(remoteProductManagerRepo.findOne(any(Long.class))).thenReturn(TEST_REMOTE_PRODUCT_MANAGER);

apiResponse = internalRequestController.push(TEST_REQUEST_BELLS.getId(), null, TEST_REMOTE_PRODUCT_MANAGER.getId(), TEST_REMOTE_PRODUCT_INFO1.getScopeId());

String expectedMessage = "Product with id null not found!";

assertEquals("Push with invalid Product did not throw an exception", ERROR, apiResponse.getMeta().getStatus());
assertEquals("Push with invalid Product did not result in the expected error", expectedMessage, apiResponse.getMeta().getMessage());
assertEquals("InternalRequest should not be deleted after failed push", 2, internalRequestRepo.count());
}

@Test
public void testPushInvalidInternalRequest() {
when(internalRequestRepo.findOne(any(Long.class))).thenReturn(null);
when(productRepo.findOne(any(Long.class))).thenReturn(TEST_PRODUCT1);
when(remoteProductManagerRepo.findOne(any(Long.class))).thenReturn(TEST_REMOTE_PRODUCT_MANAGER);

apiResponse = internalRequestController.push(null, TEST_PRODUCT1.getId(), TEST_REMOTE_PRODUCT_MANAGER.getId(), TEST_REMOTE_PRODUCT_INFO1.getScopeId());

String expectedMessage = "Internal Request with id null not found!";

assertEquals("Push without Product did not result in an error", ERROR, apiResponse.getMeta().getStatus());
assertEquals("Push without Product did not result in the expected error", NO_PRODUCT_ERROR_MESSAGE, apiResponse.getMeta().getMessage());
assertEquals("Push with invalid Internal Request did not throw an exception", ERROR, apiResponse.getMeta().getStatus());
assertEquals("Push with invalid Internal Request did not result in the expected error", expectedMessage, apiResponse.getMeta().getMessage());
assertEquals("InternalRequest should not be deleted after failed push", 2, internalRequestRepo.count());
}

}
}