Skip to content

Commit

Permalink
Added some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxianhjy committed Sep 16, 2024
1 parent de2a396 commit c63a2d6
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.ctrip.framework.apollo.common.dto;


import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ItemInfoDTOTest {

private ItemInfoDTO itemInfoDTO;

@Before
public void setUp() {
itemInfoDTO = new ItemInfoDTO("testAppId", "testClusterName", "testNamespaceName", "testKey", "testValue");
}

@Test
public void testGetAppId_ShouldReturnCorrectAppId() {
assertEquals("testAppId", itemInfoDTO.getAppId());
}

@Test
public void testGetClusterName_ShouldReturnCorrectClusterName() {
assertEquals("testClusterName", itemInfoDTO.getClusterName());
}

@Test
public void testGetNamespaceName_ShouldReturnCorrectNamespaceName() {
assertEquals("testNamespaceName", itemInfoDTO.getNamespaceName());
}

@Test
public void testGetKey_ShouldReturnCorrectKey() {
assertEquals("testKey", itemInfoDTO.getKey());
}

@Test
public void testGetValue_ShouldReturnCorrectValue() {
assertEquals("testValue", itemInfoDTO.getValue());
}

@Test
public void testToString_ShouldReturnExpectedString() {
assertEquals("ItemInfoDTO{appId='testAppId', clusterName='testClusterName', namespaceName='testNamespaceName', key='testKey', value='testValue'}", itemInfoDTO.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2024 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.ctrip.framework.apollo.common.http;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.HttpStatus;

import static org.junit.Assert.*;

@RunWith(MockitoJUnitRunner.class)
public class SearchResponseEntityTest {

@Test
public void testOk_WithValidBody_ShouldReturnOkResponse() {
String body = "test body";
SearchResponseEntity<String> response = SearchResponseEntity.ok(body);

assertEquals(HttpStatus.OK.value(), response.getCode());
assertEquals(HttpStatus.OK.getReasonPhrase(), response.getMessage());
assertEquals(body, response.getBody());
assertFalse(response.isHasMoreData());
}

@Test
public void testOkWithMessage_WithValidBodyAndMessage_ShouldReturnOkResponseWithMessage() {
String body = "test body";
String message = "test message";
SearchResponseEntity<String> response = SearchResponseEntity.okWithMessage(body, message);

assertEquals(HttpStatus.OK.value(), response.getCode());
assertEquals(message, response.getMessage());
assertEquals(body, response.getBody());
assertTrue(response.isHasMoreData());
}

@Test
public void testError_WithValidCodeAndMessage_ShouldReturnErrorResponse() {
HttpStatus httpCode = HttpStatus.BAD_REQUEST;
String message = "error message";
SearchResponseEntity<Object> response = SearchResponseEntity.error(httpCode, message);

assertEquals(httpCode.value(), response.getCode());
assertEquals(message, response.getMessage());
assertEquals(null, response.getBody());
assertFalse(response.isHasMoreData());
}
}

0 comments on commit c63a2d6

Please sign in to comment.