Skip to content

Commit

Permalink
Merge pull request #2 from y4cer/testing
Browse files Browse the repository at this point in the history
Add Unit tests
  • Loading branch information
y4cer committed Jul 18, 2023
2 parents c27d840 + 77823ed commit 0c25a88
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,16 @@ target_include_directories(driver PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/snmp ${_PR
add_dependencies(driver key_value_service_client)
target_sources(driver PRIVATE main.cc)
target_link_libraries(driver PUBLIC ${_REFLECTION} ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF} ${NETSNMP})


enable_testing()

find_package(GTest REQUIRED)
message(STATUS "Using Gtest ${GTest_VERSION}")
#find_package(GMock REQUIRED)

add_executable(unit_tests snmp/SNMP_client.h snmp/SNMP_client.cc $<TARGET_OBJECTS:key_value_service_client>)
target_include_directories(unit_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/snmp ${_PROTOBUF_INCLUDES})
add_dependencies(unit_tests key_value_service_client)
target_sources(unit_tests PRIVATE grpc/key_value_service_client_Unit_test.cpp)
target_link_libraries(unit_tests PUBLIC ${_REFLECTION} ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF} ${NETSNMP} GTest::Main GTest::GTest gmock)
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ RUN export PATH="~/.local/bin:$PATH"
# protoc
RUN apt-get install -y protobuf-compiler

# tests
RUN apt-get -y install libgtest-dev

WORKDIR /app

COPY grpc/ ./grpc/
Expand All @@ -58,7 +61,5 @@ COPY snmp/ ./snmp/
COPY main.cc .
COPY CMakeLists.txt ./

# RUN export CMAKE_PREFIX_PATH=~/.local/include:$CMAKE_PREFIX_PATH

RUN cmake -B build -DCMAKE_INSTALL_PREFIX=~/.local/ -S ./ -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-I/root/.local/include"
RUN cmake --build build --parallel --target driver
76 changes: 76 additions & 0 deletions grpc/key_value_service_client_Unit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// Created by anastasia on 12.07.23.
//
#include "key_value_service_client.h"
#include "data_transfer_api.grpc.pb.h"
#include "data_transfer_api.pb.h"

#include <gtest/gtest.h>
#include <iostream>
#include <gmock/gmock.h>
//#include <gmock/gmock.h>
//#include <string.h>


using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;


class MockKeyValueService : public data_transfer_api::KeyValueService::Service {
public:
MOCK_METHOD(grpc::Status, StoreValue, (grpc::ServerContext* context,
const data_transfer_api::StoreValueRequest* request,
data_transfer_api::StoreValueResponse* response), (override));
// Add mock methods for other service functions if needed
};

class MockTest : public ::testing::Test {
protected:
MockTest() {
int port = 8000;
server_address_ << "localhost:" << port;
// Setup server
grpc::ServerBuilder builder;
builder.AddListeningPort(server_address_.str(),
grpc::InsecureServerCredentials());
service_ = std::make_shared<MockKeyValueService>();
builder.RegisterService(service_.get());
server_ = builder.BuildAndStart();

std::shared_ptr<::grpc::Channel> channel = server_->InProcessChannel(::grpc::ChannelArguments());
client_ = std::make_unique<KeyValueService_client>(channel);

}
void TearDown() override { server_->Shutdown(); }
std::unique_ptr<KeyValueService_client> client_;
std::shared_ptr<MockKeyValueService> service_;
std::ostringstream server_address_;
std::unique_ptr<grpc::Server> server_;
};

TEST_F(MockTest, StoreValueTest){
std::string key = "SomeKey";
std::string payload = "FL=";
EXPECT_CALL(*service_, StoreValue(_,_,_))
.WillOnce([](grpc::ServerContext*, const data_transfer_api::StoreValueRequest* request, data_transfer_api::StoreValueResponse* response) {
// Perform any necessary checks on the request
EXPECT_EQ(request->key(), "SomeKey");
EXPECT_EQ(request->value().payload(), "FL=");

// Set response values if needed
response->set_message("Item has been successfully stored");
return grpc::Status::OK;
});
std::string result = client_->store_value(key, payload);

EXPECT_EQ(result, "Item has been successfully stored");
}

// Additional test cases can be added for other methods or scenarios

// Entry point for running the tests
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 0c25a88

Please sign in to comment.