Sunday, October 29, 2023

Testing methodologies and practices in Java development

Testing is a critical part of the software development process to ensure the reliability and correctness of your Java code.

Unit Testing:

    • JUnit: JUnit is a popular testing framework for writing and running unit tests in Java. It provides annotations and assertions for testing individual units of code, such as methods or classes.
  1. Integration Testing:

    • TestNG: TestNG is a testing framework that supports both unit and integration testing. It allows you to create test suites, set up test dependencies, and perform parallel testing.
  2. Functional Testing:

    • Selenium: Selenium is widely used for testing web applications. It automates browser actions and provides a way to test web functionality and user interactions.
  3. Performance Testing:

    • Apache JMeter: JMeter is an open-source tool for testing the performance of your Java applications, including load testing, stress testing, and performance analysis.
  4. Code Review:

    • Conducting code reviews is not an automated testing method but is an essential part of ensuring code quality. Reviewing code with peers helps catch bugs, improve coding standards, and share knowledge.
  5. Static Analysis:

    • Tools like FindBugs and PMD perform static code analysis to identify potential issues, code smells, and violations of coding standards.
  6. Continuous Integration (CI) and Continuous Delivery (CD):

    • Implement CI/CD pipelines to automatically build, test, and deploy your Java code whenever changes are made. Jenkins and Travis CI are popular CI/CD tools.
  7. Mocking and Test Doubles:

    • Libraries like Mockito help create mock objects to simulate the behavior of dependencies in unit tests. This is useful for isolating the unit under test.
  8. Test-Driven Development (TDD):

    • In TDD, you write tests before writing the actual code. This practice can help ensure that your code is designed with testability in mind and that it meets the expected requirements.
  9. Behavior-Driven Development (BDD):

    • BDD frameworks like Cucumber and JBehave allow you to write tests in plain language that non-technical stakeholders can understand. This promotes collaboration and clear requirements.
  10. Exploratory Testing:

    • Exploratory testing is an approach where testers explore the software, learn about it, and create test cases on the fly. It's particularly useful for finding unexpected issues.
  11. Regression Testing:

    • Regularly run regression tests to ensure that new code changes do not introduce new bugs or break existing functionality.
  12. Security Testing:

    • Use tools like OWASP ZAP and Nessus for security testing to identify vulnerabilities in your Java applications.
  13. Load Testing:

    • Use tools like Apache JMeter or Gatling to simulate a high load on your Java applications and assess their performance under heavy traffic.
  14. Continuous Monitoring:

    • Implement monitoring tools like New Relic or Prometheus to continuously monitor your application's performance in a production environment.

Thursday, October 26, 2023

REST VS SOAP

REST VS SOAP

Architectural Style

REST: REST is an architectural style that is based on a set of constraints, including statelessness, a uniform interface, and the use of standard HTTP methods (GET, POST, PUT, DELETE) for communication.

SOAP: SOAP is a protocol that defines a set of rules for structuring messages. It can be used over a variety of lower-level protocols, including HTTP, SMTP, and more.

Protocol

REST: It typically uses HTTP as the underlying protocol, making it easy to implement using existing web technologies. RESTful services often use JSON or XML for data exchange.

SOAP: It can use different transport protocols, including HTTP, SMTP, TCP, and more. SOAP messages are typically XML-based.

Complexity

REST: REST is often considered simpler and more lightweight, making it easier to understand and implement. It relies on the principles of the web, which include stateless communication and the use of URLs for resource identification.

SOAP: SOAP is considered more complex due to its extensive specification. It includes features like security and transactions, which can add complexity to implementations.

Flexibility

REST: REST is highly flexible and can be designed to fit the specific needs of an application. It allows for various data formats and doesn't prescribe a specific message structure.

SOAP: SOAP has a more rigid message structure defined by the XML Schema, which can be both an advantage (for strict contracts) and a disadvantage (for flexibility).

Interoperability

REST: REST is generally more interoperable across different platforms and programming languages because it uses standard HTTP methods and can work with various data formats.

SOAP: SOAP has built-in support for complex data types and can offer better support for strict contracts and security standards, which can be an advantage in enterprise environments.

Performance

REST: REST is often considered to be faster and more efficient because it is stateless and typically uses smaller message formats like JSON. It can be more suitable for resource-centric operations.

SOAP: SOAP messages, being XML-based and potentially larger, may have more overhead. However, this depends on the specific use case and how the service is implemented.

Caching

REST: REST encourages the use of HTTP caching, which can improve performance and reduce server load.

SOAP: SOAP lacks built-in support for caching.

The choice between REST and SOAP depends on the specific requirements of a project. 

REST is often preferred for simple, lightweight, and public-facing APIs, 

SOAP may be more suitable for enterprise-level applications with strict security and transaction requirements

Saturday, October 21, 2023

MCU - Code generation tools for microcontrollers

 Code generation tools for Microcontrollers

Microcontrollers are used in a wide range of embedded systems, from simple devices like thermostats to complex systems like automotive control units. One of the challenges of developing embedded systems is configuring the microcontroller's peripherals. This can be a time-consuming and error-prone process, especially for complex systems.

Code generation tools can help developers to save time and improve the quality of their code by automating the process of configuring microcontroller peripherals. These tools allow developers to specify the peripherals they need and the desired configuration. The tool then generates the necessary initialization code, which can be used in the developer's application.

There are a number of different code-generation tools available for microcontrollers. Some of the most popular tools include:

  • STM32CubeMX: This tool is provided by STMicroelectronics and supports a wide range of STM32 microcontrollers.
  • NXP MCUXpresso Config Tools: This tool is provided by NXP and supports a wide range of NXP microcontrollers.
  • Microchip MPLAB Code Configurator: This tool is provided by Microchip and supports a wide range of Microchip microcontrollers.
  • Texas Instruments Code Composer Studio Peripheral Explorer: This tool is provided by Texas Instruments and supports a wide range of Texas Instruments microcontrollers.
  • Renesas e²studio Code Generator: This tool is provided by Renesas and supports a wide range of Renesas microcontrollers.
  • Infineon DAVE IDE: This tool is provided by Infineon and supports a wide range of Infineon microcontrollers.

These tools all work in a similar way, allowing users to configure the peripherals on their microcontroller through a graphical user interface. The tools then generate the necessary initialization code, which can be used in the user's application.

In addition to the tools provided by the microcontroller manufacturers, there are also a number of third-party tools that can be used for code generation. One popular example is CubeMX.org, which is a community-driven project that provides configuration files for a wide range of microcontrollers from different manufacturers.

Benefits of using code generation tools

There are a number of benefits to using code-generation tools for microcontrollers. These benefits include:

  • Free to use
  • Reduced development time: Code generation tools can save developers a significant amount of time by automating the process of configuring microcontroller peripherals.
  • Improved code quality: Code generation tools can help to improve the quality of code by generating code that is well-organized and easy to maintain.
  • Reduced risk of errors: Code generation tools can help to reduce the risk of errors by generating code that is tested and verified.
  • Improved consistency: Code generation tools can help to improve the consistency of code by generating code that follows a set of coding standards.

Code generation tools are a valuable tool for developers of embedded systems. They can help to save time, improve code quality, reduce the risk of errors, and improve the consistency of code.

Saturday, October 7, 2023

Build the CPP-SMTPClient library on Windows

Build the CPP-SMTPClient library on Windows

  1. SMTPClient-library


  2. Getting OpenSSL Dependency:

    You need to obtain OpenSSL, which is a dependency for the CPP-SMTPClient library. There are two methods to get OpenSSL:

    Using Chocolatey:

    • Install Chocolatey on your system if you haven't already.
    • vedio tuto https://www.youtube.com/watch?v=7Eiuvy5_dh8

    • Open a command prompt as an administrator and run the following command:
      choco install openssl
      If you want the 32-bit version, use the following command:
      choco install --forceX86 openssl
    • After installation, the OpenSSL files will be in either C:\Program Files\OpenSSL-Win64 or C:\Program Files (x86)\OpenSSL-Win32 depending on the version you installed.
    • It's recommended to create an environment variable OPENSSL_ROOT_DIR that points to your OpenSSL installation folder, e.g., C:\Program Files\OpenSSL-Win64.
    • Use the command RefreshEnv to ensure your environment variables are updated.

    Using OpenSSL Binary Distributions:


    • Download one of the prebuilt OpenSSL binaries here.
    • Extract the downloaded files to a location on your hard drive.
    • Create an environment variable OPENSSL_ROOT_DIR pointing to the OpenSSL installation folder, e.g., C:\openssl\x64.
    • Close and reopen your Windows session to update environment variables.

  3. Preparing the Build Folder:

    Navigate to the installation folder where you have the CPP-SMTPClient library source code:


  4.     cd <path to SMTPClient repo> mkdir build
        mkdir build
        cd build

  5. Configuration and Generation:

    Use CMake to configure and generate the project files based on your desired build mode (Release/Debug) and architecture (64-bit/32-bit). Run one of the following commands:

    Release Mode:

    • 64 bits (x64):
    • cmake ..

    • 32 bits (x86):
    • cmake -DCMAKE_GENERATOR_PLATFORM=Win32 -DCMAKE_BUILD_TYPE=Release -T host=x86 ..

    Debug Mode:

    • 64 bits (x64):
       cmake -DCMAKE_BUILD_TYPE=Debug ..
    • 32 bits (x86):
       cmake -DCMAKE_GENERATOR_PLATFORM=Win32 -DCMAKE_BUILD_TYPE=Debug -T host=x86 ..
  6. Troubleshooting OpenSSL Folders and Library Files:

    During the configuration and generation phase, CMake should locate OpenSSL. You can verify this by checking the output. Look for lines that mention OpenSSL, and they should point to the correct folders and libraries.

    If OpenSSL was not found correctly, you can provide the necessary information using CMake variables:

    • OPENSSL_ROOT_DIR
    • OPENSSL_INCLUDE_DIRECTORY
    • OPENSSL_LIBRARY_DIRECTORY
    • OPENSSL_CRYPTO_LIBRARY
    • OPENSSL_SSL_LIBRARY

    For example:

     cmake -DOPENSSL_ROOT_DIR=C:\openssl\x64 -DOPENSSL_INCLUDE_DIRECTORY=C:\openssl\x64\include -DOPENSSL_LIBRARY_DIRECTORY=C:\openssl\x64\lib -DOPENSSL_CRYPTO_LIBRARY=crypto -DOPENSSL_SSL_LIBRARY=ssl ..
  7. Building the Library:

    To build the library, use the following commands based on your chosen build mode and architecture:

    Release Mode:

    • 64 bits (x64):
       cmake --build . --config Release
    • 32 bits (x86):
       cmake --build . --config Release

    Debug Mode:

    • 64 bits (x64):
       cmake --build .
    • 32 bits (x86):
      cmake --build .

    The resulting library, smtpclient.dll, will be created in the build\Debug or build\Release folder, depending on your build mode.


  8. Building with Visual Studio (Optional):

    If you prefer using Visual Studio, you can also build the project from within Visual Studio:

    • Click "File" -> "Open" -> "Folder..."
    • Select the folder where you have downloaded the CPP-SMTPClient library.
    • Build the project using Visual Studio.
    • make sure you have openssl include folder in your sln directory, copy and paste it from
    • C:\Program Files\OpenSSL-Win64

That's it! You should now have successfully built the CPP-SMTPClient library on Windows.


To run the example code


use smtp.gmail.com for the "<your smtp server address>"

Create a Google app password for "App Passwword"
"

OpportunisticSecureSMTPClient client("smtp.gmail.com", 587); client.setCredentials(Credential("my@gmail.com", "App Passwword"));

try { const MessageAddress from("sender@gmail.com", "Test Address Display"); const auto to = { MessageAddress("receiver@gmail.com") }; const auto subject = "This is a test (Subject)"; const auto body = "<html><body><h1>Hello,</h1><br/><br/>How are you?</body></html>"; const auto cc = { MessageAddress("receiver@gmail.com") }; const std::vector<MessageAddress> bcc = {}; const auto attachment = {Attachment("C:\\Pictures\\test.png") }; HTMLMessage msg(from, to, subject, body, cc, bcc, attachment); client.sendMail(msg); }

catch (std::invalid_argument& err) {

std::cerr << err.what() << std::endl;

}

App Password


An "App Password" is a specific type of password generated by some online service providers, like Google, to allow secure access to your account by applications or devices that don't support or are not compatible with the standard two-factor authentication (2FA) methods. Here's how it works:

  • Two-Factor Authentication (2FA): Many online services, including email providers like Gmail, offer 2FA as an added layer of security. With 2FA enabled, in addition to your regular password, you need to provide a second authentication factor, such as a one-time code sent to your mobile device, to access your account.
  • Problem with Some Apps: While 2FA enhances security, it can be problematic for certain applications or devices that don't support the 2FA process. These apps may not be able to handle the second authentication factor.
  • App Password Solution: To overcome this limitation, some services, including Google, provide the option to generate "App Passwords." An App Password is a randomly generated, long, and complex password that can be used by a specific application or device to access your account. You only need to enter this password once for each application or device.
  • Security: App Passwords are secure because they are not easily guessable, and they provide a way to access your account without revealing your primary account password.
  • Specific to Applications: Each App Password is typically specific to the application or device you generate it for. This means that if you have multiple apps or devices that need access, you can generate a unique App Password for each one.
  • Revocable: If you ever lose control of a device or suspect that an App Password has been compromised, you can simply revoke that specific App Password without affecting your main account password.
  • How to Generate: To generate an App Password, you usually need to go to your account settings (e.g., Google Account settings), find the "Security" or "Two-Factor Authentication" section, and look for the option to generate an App Password. You'll typically need to specify the app or device for which you're generating the password.

Remember that the process for generating and managing App Passwords may vary depending on the service provider.

LeetCode C++ Cheat Sheet June

🎯 Core Patterns & Representative Questions 1. Arrays & Hashing Two Sum – hash map → O(n) Contains Duplicate , Product of A...