Skip to content

Latest commit

 

History

History
185 lines (135 loc) · 5.41 KB

File metadata and controls

185 lines (135 loc) · 5.41 KB

Mock Data Implementation Guide

📋 Overview

This project now includes an in-app mock data layer that allows you to develop and test without requiring a backend server or database connection.

✨ What Was Added

New Files Created:

  1. MockDataProvider.java - Configuration file

    • Toggle between mock and real API with a single boolean flag
    • Configure simulated network delay
  2. MockDataRepository.java - Mock data storage

    • Contains 8 pre-configured gaming products
    • Contains 3 mock users (admin, testuser, demo)
    • Mock JWT token for authentication
  3. MockApiWrapper.java - API wrapper

    • Intercepts API calls when mock mode is enabled
    • Returns mock data instead of making real network requests
    • Maintains the same interface as Retrofit

Modified Files:

  1. ApiClient.java - Initializes MockApiWrapper
  2. SignInActivity.java - Uses MockApiWrapper for login
  3. ListProductActivity.java - Uses MockApiWrapper for product list
  4. ProductDetailActivity.java - Uses MockApiWrapper for product details

🚀 How to Use

Toggle Between Mock and Real API

Open MockDataProvider.java and change the flag:

public static final boolean USE_MOCK_DATA = true;  // Use mock data
public static final boolean USE_MOCK_DATA = false; // Use real API

Mock User Credentials

You can login with these pre-configured accounts:

Username Password Email
admin admin123 admin@example.com
testuser password test@example.com
demo demo demo@example.com

Mock Products

The mock repository includes 8 gaming products:

  • Wireless Gaming Mouse RGB ($350,000)
  • Mechanical Gaming Keyboard ($850,000)
  • 7.1 Surround Gaming Headset ($450,000)
  • 27" 144Hz Gaming Monitor ($3,500,000)
  • Ergonomic Gaming Chair ($2,500,000)
  • Gaming Laptop RTX 3060 ($25,000,000)
  • Wireless Game Controller ($550,000)
  • Extended RGB Mouse Pad ($180,000)

Adjust Network Delay

Simulate real network conditions by adjusting the delay:

public static final long MOCK_DELAY_MS = 500;  // 500ms delay
public static final long MOCK_DELAY_MS = 0;    // Instant response

🔧 Architecture

┌─────────────────┐
│   UI Activity   │
└────────┬────────┘
         │
         ↓
┌─────────────────┐      Yes    ┌──────────────────┐
│ MockApiWrapper  │─────────────→│ MockDataRepository│
└────────┬────────┘              └──────────────────┘
         │
         │ No (USE_MOCK_DATA = false)
         ↓
┌─────────────────┐
│   Real API      │
│  (Retrofit)     │
└─────────────────┘

✅ Benefits

  • No Server Required - Develop without backend dependency
  • Faster Development - Instant responses, no network delays
  • Predictable Testing - Consistent data for UI testing
  • Offline Development - Work anywhere without internet
  • Easy Toggle - Switch between mock and real with one line
  • Non-Destructive - All original API code remains intact

🎯 Testing Scenarios

Success Flow

  1. Set USE_MOCK_DATA = true
  2. Run the app
  3. Login with admin / admin123
  4. Browse products (shows 8 mock products)
  5. View product details
  6. Add items to cart

Error Flow

  1. Set USE_MOCK_DATA = true
  2. Login with wrong credentials (e.g., wrong / wrong)
  3. Should see "Invalid credentials" error

Real API Flow

  1. Set USE_MOCK_DATA = false
  2. Ensure backend server is running
  3. App will use real API calls

📝 Adding More Mock Data

Add More Products

Edit MockDataRepository.javainitializeMockProducts():

Product p9 = new Product();
p9.productId = 9;
p9.name = "Your Product Name";
p9.description = "Description here";
p9.price = 999000;
p9.imageUrl = "https://example.com/image.jpg";
MOCK_PRODUCTS.add(p9);

Add More Users

Edit MockDataRepository.javainitializeMockUsers():

User user4 = new User("newuser", "password123", "new@example.com");
user4.setUserId(4);
MOCK_USERS.put("newuser", user4);

🔍 Troubleshooting

Issue: Still getting network errors

Solution: Make sure USE_MOCK_DATA = true in MockDataProvider.java

Issue: Login fails with mock credentials

Solution: Check that you're using exact credentials from the table above (case-sensitive)

Issue: Empty product list

Solution: Verify MockDataRepository.initializeMockProducts() is called

Issue: Want to use real API

Solution: Set USE_MOCK_DATA = false and ensure your backend server is accessible

📊 Current Status

  • ✅ Mock data layer implemented
  • ✅ Login authentication with mock users
  • ✅ Product list with mock products
  • ✅ Product details with mock data
  • ✅ Configurable mock/real API toggle
  • ✅ All original code preserved

🔄 Switching Back to Real API

Simply change one line in MockDataProvider.java:

public static final boolean USE_MOCK_DATA = false;

All API calls will automatically route to your real backend server.


Ready to develop! 🚀 Your app now works with mock data by default.