Back to Repositories

Testing Qiniu Cloud Storage Integration in litemall

A comprehensive test suite for Qiniu cloud storage integration in the litemall application, validating file upload, resource loading, and URL generation functionality. This test ensures reliable cloud storage operations for the e-commerce platform.

Test Coverage Overview

The test suite covers core Qiniu storage operations including file upload, resource retrieval, and URL generation.

  • File upload validation using PNG image
  • Resource loading verification
  • URL generation testing
  • Integration with Spring resource handling

Implementation Analysis

The implementation uses Spring Boot test framework with JUnit4 integration. The test demonstrates a practical approach to cloud storage testing by:

  • Using Spring’s WebAppConfiguration for web context simulation
  • Leveraging autowired dependencies for QiniuStorage service
  • Implementing resource stream handling with FileInputStream

Technical Details

Testing infrastructure includes:

  • SpringRunner for test execution
  • Apache Commons Logging for operation tracking
  • Spring Boot Test context configuration
  • Resource handling through Spring’s Resource interface
  • File type and length validation

Best Practices Demonstrated

The test exemplifies several testing best practices:

  • Proper resource cleanup through Spring context management
  • Comprehensive logging of operations
  • Clear test method organization
  • Effective use of Spring’s dependency injection
  • Proper exception handling for IO operations

linlinjava/litemall

litemall-core/src/test/java/org/linlinjava/litemall/core/QiniuStorageTest.java

            
package org.linlinjava.litemall.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.linlinjava.litemall.core.storage.QiniuStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@WebAppConfiguration
@RunWith(SpringRunner.class)
@SpringBootTest
public class QiniuStorageTest {

    private final Log logger = LogFactory.getLog(QiniuStorageTest.class);
    @Autowired
    private QiniuStorage qiniuStorage;

    @Test
    public void test() throws IOException {
        String test = getClass().getClassLoader().getResource("litemall.png").getFile();
        File testFile = new File(test);
        qiniuStorage.store(new FileInputStream(test), testFile.length(), "image/png", "litemall.png");
        Resource resource = qiniuStorage.loadAsResource("litemall.png");
        String url = qiniuStorage.generateUrl("litemall.png");
        logger.info("test file " + test);
        logger.info("store file " + resource.getURI());
        logger.info("generate url " + url);
    }

}