Untitled
unknown
plain_text
2 years ago
3.5 kB
8
Indexable
@Configuration
public class CustomUserDetails extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("{noop}password").roles("USER") // Hardcoded user for demonstration
.and()
.withUser("admin").password("{noop}admin123").roles("ADMIN"); // Hardcoded admin
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/deals/**").authenticated() // Protect /deals endpoints
.anyRequest().permitAll() // Allow other endpoints without authentication
.and()
.httpBasic(); // Use basic authentication
}
}
@Service
public class DealServiceImpl implements DealService {
// Other autowired components, repositories, and methods
@Override
@Transactional
public Deal createDeal(Deal deal) {
validateDealInput(deal); // This method validates the Deal input
try {
Deal createdDeal = dealRepository.save(deal);
inventoryService.decreaseItemStock(createdDeal.getItemId(), createdDeal.getItemsAvailable());
return createdDeal;
} catch (Exception e) {
LOGGER.error("Error occurred while creating deal: {}", e.getMessage());
throw new RuntimeException("Failed to create deal");
}
}
private void validateDealInput(Deal deal) {
// Implement input validation for Deal object
// For example, check if price, start time, and end time are valid
if (deal.getPrice() <= 0 || deal.getStartTime().isAfter(deal.getEndTime())) {
throw new IllegalArgumentException("Invalid deal details");
}
}
// Other methods
}
@RunWith(MockitoJUnitRunner.class)
public class DealServiceTest {
@Mock
private DealRepository dealRepository;
@Mock
private PurchaseHistoryRepository purchaseHistoryRepository;
@Mock
private InventoryService inventoryService;
@InjectMocks
private DealServiceImpl dealService;
@Test
public void testCreateDeal_WithValidDeal_ShouldCreateDeal() {
// Create a sample valid deal
Deal deal = new Deal();
deal.setPrice(100);
deal.setStartTime(LocalDateTime.now());
deal.setEndTime(LocalDateTime.now().plusHours(2));
// Mock repository behavior
when(dealRepository.save(any(Deal.class))).thenReturn(deal);
// Perform the service method
Deal createdDeal = dealService.createDeal(deal);
// Validate
assertNotNull(createdDeal);
assertEquals(100, createdDeal.getPrice());
// Add more assertions based on expected behavior
}
@Test(expected = IllegalArgumentException.class)
public void testCreateDeal_WithInvalidDeal_ShouldThrowException() {
// Create a sample invalid deal (start time after end time)
Deal invalidDeal = new Deal();
invalidDeal.setStartTime(LocalDateTime.now());
invalidDeal.setEndTime(LocalDateTime.now().minusHours(2));
// Perform the service method (this should throw an exception)
dealService.createDeal(invalidDeal);
}
// Add more test cases for other service methods
}
Editor is loading...
Leave a Comment